diff --git a/packages/workflow-core/src/bun-temporal-plugin.test.ts b/packages/workflow-core/src/bun-temporal-plugin.test.ts new file mode 100644 index 00000000..ffc6ac84 --- /dev/null +++ b/packages/workflow-core/src/bun-temporal-plugin.test.ts @@ -0,0 +1,50 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { temporalWorkflow } from './bun-temporal-plugin' +import * as worker from '@temporalio/worker' + +vi.mock('@temporalio/worker', () => ({ + bundleWorkflowCode: vi.fn().mockResolvedValue({ code: 'mock-code', sourceMap: '' }) +})) + +describe('temporalWorkflow plugin', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + it('should register onLoad and configure webpackConfigHook with @temporalio/workflow alias', async () => { + const plugin = temporalWorkflow() + + let onLoadCallback: Function | null = null + + const mockBuild = { + onResolve: vi.fn(), + onLoad: vi.fn((opts, callback) => { + onLoadCallback = callback + }) + } + + // Run setup + // @ts-ignore + plugin.setup(mockBuild) + + expect(onLoadCallback).toBeDefined() + + // Trigger onLoad callback + await onLoadCallback!({ path: 'dummy-path' }) + + // Verify bundleWorkflowCode was called + expect(worker.bundleWorkflowCode).toHaveBeenCalled() + + // Inspect the webpackConfigHook that was passed to bundleWorkflowCode + const callArgs = vi.mocked(worker.bundleWorkflowCode).mock.calls[0][0] + expect(callArgs.webpackConfigHook).toBeDefined() + + // Invoke the hook with a dummy config + const config = { resolve: { alias: {} } } + const updatedConfig = callArgs.webpackConfigHook(config) + + // Check that aliases are set + expect(updatedConfig.resolve.alias['@shumai/workflow-core']).toBeDefined() + expect(updatedConfig.resolve.alias['@temporalio/workflow']).toBeDefined() + }) +}) diff --git a/packages/workflow-core/src/bun-temporal-plugin.ts b/packages/workflow-core/src/bun-temporal-plugin.ts index 2cec9cc2..92d1537f 100644 --- a/packages/workflow-core/src/bun-temporal-plugin.ts +++ b/packages/workflow-core/src/bun-temporal-plugin.ts @@ -1,6 +1,6 @@ import { bundleWorkflowCode } from '@temporalio/worker' import type { BunPlugin } from 'bun' -import { resolve } from 'node:path' +import { resolve, dirname } from 'node:path' export interface TemporalWorkflowPluginOptions { /** Query param identifier. Default: 'workflow' */ @@ -47,6 +47,9 @@ export function temporalWorkflow(options: TemporalWorkflowPluginOptions = {}): B ...config.resolve.alias, '@shumai/workflow-core': require.resolve('@shumai/workflow-core/src/workflow-utils.ts'), + '@temporalio/workflow': dirname( + require.resolve('@temporalio/workflow/package.json'), + ), } console.log('Webpack config aliases:', config.resolve.alias) if (originalHook) {