@@ -156,20 +156,6 @@ describe('using fake modern timers', () => {
156
156
expect ( testStackFrame ) . toMatch ( fileLocationRegexp )
157
157
const [ , fileLocation ] = testStackFrame . match ( fileLocationRegexp )
158
158
expect ( fileLocation ) . toBe ( __filename )
159
-
160
- expect ( waitForError . stack ) . toMatchInlineSnapshot ( `
161
- Error: Timed out in waitFor.
162
- at waitFor (<PROJECT_ROOT>/src/waitFor.ts:163:27)
163
- at waitFor (<PROJECT_ROOT>/src/__tests__/waitForNode.js:52:20)
164
- at Object.<anonymous> (<PROJECT_ROOT>/src/__tests__/waitForNode.js:142:13)
165
- at Promise.then.completed (<PROJECT_ROOT>/node_modules/jest-circus/build/utils.js:391:28)
166
- at new Promise (<anonymous>)
167
- at callAsyncCircusFn (<PROJECT_ROOT>/node_modules/jest-circus/build/utils.js:316:10)
168
- at _callCircusTest (<PROJECT_ROOT>/node_modules/jest-circus/build/run.js:218:40)
169
- at processTicksAndRejections (node:internal/process/task_queues:96:5)
170
- at _runTest (<PROJECT_ROOT>/node_modules/jest-circus/build/run.js:155:3)
171
- at _runTestsForDescribeBlock (<PROJECT_ROOT>/node_modules/jest-circus/build/run.js:66:9)
172
- ` )
173
159
} )
174
160
175
161
test ( 'does not crash in runtimes without Error.prototype.stack' , async ( ) => {
@@ -221,16 +207,52 @@ describe('using fake modern timers', () => {
221
207
// An actual test would not have any frames pointing to this test.
222
208
expect ( waitForError . stack ) . toMatchInlineSnapshot ( `
223
209
Error: Timed out in waitFor.
224
- at handleTimeout (<PROJECT_ROOT>/src/waitFor.ts:147 :17)
210
+ at handleTimeout (<PROJECT_ROOT>/src/waitFor.ts:146 :17)
225
211
at callTimer (<PROJECT_ROOT>/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:729:24)
226
212
at doTickInner (<PROJECT_ROOT>/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1289:29)
227
213
at doTick (<PROJECT_ROOT>/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1370:20)
228
214
at Object.tick (<PROJECT_ROOT>/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js:1378:20)
229
215
at FakeTimers.advanceTimersByTime (<PROJECT_ROOT>/node_modules/@jest/fake-timers/build/modernFakeTimers.js:101:19)
230
216
at Object.advanceTimersByTime (<PROJECT_ROOT>/node_modules/jest-runtime/build/index.js:2228:26)
231
217
at Object.advanceTimersByTime (<PROJECT_ROOT>/src/__tests__/waitForNode.js:41:12)
232
- at <PROJECT_ROOT>/src/waitFor.ts:75 :15
218
+ at <PROJECT_ROOT>/src/waitFor.ts:80 :15
233
219
at new Promise (<anonymous>)
234
220
` )
235
221
} )
222
+
223
+ test ( 'can be aborted with an AbortSignal' , async ( ) => {
224
+ const callback = jest . fn ( ( ) => {
225
+ throw new Error ( 'not done' )
226
+ } )
227
+ const controller = new AbortController ( )
228
+ const waitForError = waitFor ( callback , {
229
+ signal : controller . signal ,
230
+ } )
231
+
232
+ controller . abort ( 'Bailing out' )
233
+
234
+ await expect ( waitForError ) . rejects . toThrowErrorMatchingInlineSnapshot (
235
+ `Aborted: Bailing out` ,
236
+ )
237
+ // Initial check + one ping (after which we yield which gives us a chance to advance to the controller.abort call)
238
+ expect ( callback ) . toHaveBeenCalledTimes ( 2 )
239
+ } )
240
+
241
+ test ( 'does not even ping if the signal is already aborted' , async ( ) => {
242
+ const callback = jest . fn ( ( ) => {
243
+ throw new Error ( 'not done' )
244
+ } )
245
+ const controller = new AbortController ( )
246
+ controller . abort ( 'Bailing out' )
247
+
248
+ const waitForError = waitFor ( callback , {
249
+ signal : controller . signal ,
250
+ } )
251
+
252
+ await expect ( waitForError ) . rejects . toThrowErrorMatchingInlineSnapshot (
253
+ `Aborted: Bailing out` ,
254
+ )
255
+ // Just the initial check
256
+ expect ( callback ) . toHaveBeenCalledTimes ( 1 )
257
+ } )
236
258
} )
0 commit comments