Skip to content

feat: Implement waitFor #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: alpha
Choose a base branch
from
Prev Previous commit
Next Next commit
Gate AbortController tests
We also test Node.js 14 which doesn't support AbortController
  • Loading branch information
eps1lon authored and Sebastian Silbermann committed Oct 3, 2023
commit 28ee0ba6f648adf6b571d99793d41c14db08066d
38 changes: 22 additions & 16 deletions src/__tests__/waitForNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ test('ensures the given callback is a function', () => {
)
})

const testAbortController =
typeof AbortController === 'undefined' ? test.skip : test

describe('using fake modern timers', () => {
beforeEach(() => {
jest.useFakeTimers('modern')
Expand Down Expand Up @@ -220,7 +223,7 @@ describe('using fake modern timers', () => {
`)
})

test('can be aborted with an AbortSignal', async () => {
testAbortController('can be aborted with an AbortSignal', async () => {
const callback = jest.fn(() => {
throw new Error('not done')
})
Expand All @@ -238,21 +241,24 @@ describe('using fake modern timers', () => {
expect(callback).toHaveBeenCalledTimes(2)
})

test('does not even ping if the signal is already aborted', async () => {
const callback = jest.fn(() => {
throw new Error('not done')
})
const controller = new AbortController()
controller.abort('Bailing out')
testAbortController(
'does not even ping if the signal is already aborted',
async () => {
const callback = jest.fn(() => {
throw new Error('not done')
})
const controller = new AbortController()
controller.abort('Bailing out')

const waitForError = waitFor(callback, {
signal: controller.signal,
})
const waitForError = waitFor(callback, {
signal: controller.signal,
})

await expect(waitForError).rejects.toThrowErrorMatchingInlineSnapshot(
`Aborted: Bailing out`,
)
// Just the initial check
expect(callback).toHaveBeenCalledTimes(1)
})
await expect(waitForError).rejects.toThrowErrorMatchingInlineSnapshot(
`Aborted: Bailing out`,
)
// Just the initial check
expect(callback).toHaveBeenCalledTimes(1)
},
)
})