diff --git a/plugins/job-handler/src/job.handler.spec.ts b/plugins/job-handler/src/job.handler.spec.ts index 31c50240d..5c9459e8a 100644 --- a/plugins/job-handler/src/job.handler.spec.ts +++ b/plugins/job-handler/src/job.handler.spec.ts @@ -1,3 +1,4 @@ +import api, { Flatfile } from '@flatfile/api' import { deleteSpace, setupListener, setupSpace } from '@flatfile/utils-testing' import { afterAll, @@ -12,52 +13,54 @@ import { jobHandler } from '.' describe('JobHandler plugin e2e tests', () => { describe('jobHandler() successful', () => { + const logErrorSpy = spyOn(global.console, 'error') const listener = setupListener() const mockFn = mock() + const mockErrorFn = mock(() => { + throw new Error('trigger job:failed') + }) let spaceId: string beforeAll(async () => { - listener.use(jobHandler('space:configure', mockFn)) - const space = await setupSpace() spaceId = space.id - }) - - afterAll(async () => { - await deleteSpace(spaceId) - }) - test('jobHandler()', async () => { - await listener.waitFor('job:ready', 1, 'space:configure') - - expect(mockFn).toHaveBeenCalled() - }) - }) + listener.use(jobHandler({ operation: 'job-handler-success' }, mockFn)) - describe('jobHandler() failure', () => { - const logErrorSpy = spyOn(global.console, 'error') - const listener = setupListener() - const mockErrorFn = mock(() => { - throw new Error('trigger job:failed') - }) - let spaceId: string - - beforeAll(async () => { listener.on('job:failed', (event) => { console.log(event.topic, event.payload.job) }) - listener.use(jobHandler('space:configure', mockErrorFn)) + listener.use(jobHandler({ operation: 'job-handler-failure' }, mockErrorFn)) - const space = await setupSpace() - spaceId = space.id + const { data: successJob } = await api.jobs.create({ + type: Flatfile.JobType.Space, + operation: 'job-handler-success', + source: spaceId, + environmentId: space.environmentId, + }) + await api.jobs.execute(successJob.id) + + const { data: failedJob } = await api.jobs.create({ + type: Flatfile.JobType.Space, + operation: 'job-handler-failure', + source: spaceId, + environmentId: space.environmentId, + }) + await api.jobs.execute(failedJob.id) }) afterAll(async () => { await deleteSpace(spaceId) }) - test('job:failed', async () => { - await listener.waitFor('job:failed', 1, 'space:configure') + test('success', async () => { + await listener.waitFor('job:ready', 1, { operation: 'job-handler-success' }) + + expect(mockFn).toHaveBeenCalled() + }) + + test('failure', async () => { + await listener.waitFor('job:ready', 1, { operation: 'job-handler-failure' }) expect(logErrorSpy).toHaveBeenCalledWith( '[@flatfile/plugin-job-handler]:[FATAL] trigger job:failed' diff --git a/plugins/job-handler/src/job.handler.ts b/plugins/job-handler/src/job.handler.ts index dcd7d26e1..0388f5d7e 100644 --- a/plugins/job-handler/src/job.handler.ts +++ b/plugins/job-handler/src/job.handler.ts @@ -42,7 +42,7 @@ export function jobHandler( ) { return (listener: FlatfileListener) => { const filter = typeof job === 'string' ? { job } : job - listener.on('job:ready', filter, async (event) => { + listener.on('job:ready', filter, async (event: FlatfileEvent) => { const { jobId } = event.context await api.jobs.ack(jobId, { diff --git a/plugins/rollout/package.json b/plugins/rollout/package.json index 9d15c0435..6619d79cd 100644 --- a/plugins/rollout/package.json +++ b/plugins/rollout/package.json @@ -21,7 +21,7 @@ "build:watch": "parcel watch", "build:prod": "NODE_ENV=production parcel build", "check": "tsc ./**/*.ts --noEmit --esModuleInterop", - "test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand" + "test": "bun --env-file=../../.env.defaults test" }, "keywords": [ "flatfile-plugins", diff --git a/plugins/view-mapped/package.json b/plugins/view-mapped/package.json index 370bc0553..f2e1e7856 100644 --- a/plugins/view-mapped/package.json +++ b/plugins/view-mapped/package.json @@ -37,7 +37,7 @@ "build:watch": "rollup -c --watch", "build:prod": "NODE_ENV=production rollup -c", "check": "tsc ./**/*.ts --noEmit --esModuleInterop", - "test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand" + "test": "bun --env-file=../../.env.defaults test" }, "keywords": [ "flatfile-plugins", diff --git a/test/toBePendingMatcher.ts b/test/toBePendingMatcher.ts index 72c9eb95f..f6d83488f 100644 --- a/test/toBePendingMatcher.ts +++ b/test/toBePendingMatcher.ts @@ -1,18 +1,19 @@ +import { expect } from 'bun:test' import type { MatcherFunction } from 'expect' import * as util from 'node:util' declare global { - namespace jest { + namespace bun { interface Matchers { toBePending(): R } interface Expect { - toBePending(): JestMatchers + toBePending(): Matchers } } } -const toBePending: MatcherFunction<[recieved: unknown]> = (received) => { +const toBePending: MatcherFunction<[received: unknown]> = (received) => { const isPending = (promise: Promise) => util.inspect(promise).includes('pending') diff --git a/utils/testing/src/test.listener.spec.ts b/utils/testing/src/test.listener.spec.ts index 89c390ab3..e4f83ba10 100644 --- a/utils/testing/src/test.listener.spec.ts +++ b/utils/testing/src/test.listener.spec.ts @@ -1,5 +1,7 @@ +import { beforeEach, describe, expect, it, mock } from 'bun:test' import { TestListener } from './index' +import { FlatfileEvent } from '@flatfile/listener' import '../../../test/toBePendingMatcher' const currentEventLoopEnd = () => @@ -18,13 +20,13 @@ describe('TestListener', () => { { topic: 'first' }, { topic: 'second' }, { topic: 'second', payload: { job: 'somethingSpecific' } }, - ] + ] as FlatfileEvent[]; await Promise.all(events.map((event) => listener.dispatchEvent(event))) expect(listener.invocations.get('first')).toEqual([events[0]]) expect(listener.invocations.get('second')).toEqual([events[1], events[2]]) - expect(listener.invocations.get('third')).toBe(undefined) + expect(listener.invocations.get('third')).toBeUndefined() }) it('increments the `executedCount` on any matching watchers', async () => { @@ -40,8 +42,8 @@ describe('TestListener', () => { }) it('fulfills pending promises that match the event', async () => { - const exampleReady = jest.fn() - const exampleCompleted = jest.fn() + const exampleReady = mock() + const exampleCompleted = mock() listener.waitFor('example:ready').then(exampleReady) listener @@ -77,7 +79,7 @@ describe('TestListener', () => { await currentEventLoopEnd() expect(promisedEvent).not.toBePending() - expect(promisedEvent).resolves.not.toThrow() + expect(promisedEvent).resolves.toBe(1) }) it('does not resolve before the specified number of dispatches is met', async () => { @@ -97,7 +99,7 @@ describe('TestListener', () => { await currentEventLoopEnd() expect(promisedEvent).not.toBePending() - expect(promisedEvent).resolves.not.toThrow() + expect(promisedEvent).resolves.toBe(2) }) it('accounts for past invocations matching the provided filters', async () => { @@ -110,9 +112,9 @@ describe('TestListener', () => { await currentEventLoopEnd() expect(initialPromise).not.toBePending() - expect(initialPromise).resolves.not.toThrow() + expect(initialPromise).resolves.toBe(1) expect(secondPromise).not.toBePending() - expect(secondPromise).resolves.not.toThrow() + expect(secondPromise).resolves.toBe(2) }) it('does not resolve before an event matching the filter is dispatched', async () => { @@ -138,7 +140,7 @@ describe('TestListener', () => { await currentEventLoopEnd() expect(promisedEvent).not.toBePending() - expect(promisedEvent).resolves.not.toThrow() + expect(promisedEvent).resolves.toBe(1) }) it('supports passing a string for the job filter', async () => {