Skip to content

Commit

Permalink
Add bun test to new plugins and fix job-handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger committed Sep 16, 2024
1 parent fb655cd commit 669d2b2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 42 deletions.
57 changes: 30 additions & 27 deletions plugins/job-handler/src/job.handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import api, { Flatfile } from '@flatfile/api'
import { deleteSpace, setupListener, setupSpace } from '@flatfile/utils-testing'
import {
afterAll,
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion plugins/job-handler/src/job.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
2 changes: 1 addition & 1 deletion plugins/rollout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugins/view-mapped/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions test/toBePendingMatcher.ts
Original file line number Diff line number Diff line change
@@ -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<R> {
toBePending(): R
}
interface Expect {
toBePending<T>(): JestMatchers<T>
toBePending<T>(): Matchers<T>
}
}
}

const toBePending: MatcherFunction<[recieved: unknown]> = (received) => {
const toBePending: MatcherFunction<[received: unknown]> = (received) => {
const isPending = (promise: Promise<any>) =>
util.inspect(promise).includes('pending')

Expand Down
20 changes: 11 additions & 9 deletions utils/testing/src/test.listener.spec.ts
Original file line number Diff line number Diff line change
@@ -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 = () =>
Expand All @@ -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 () => {
Expand All @@ -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
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 669d2b2

Please sign in to comment.