9
9
import { setTimeout } from 'node:timers/promises' ;
10
10
import { promiseWithAbort } from '../../src/utils/promise' ;
11
11
12
+ function expectRejectedWithError ( promise : Promise < any > ) {
13
+ if ( typeof expectAsync === 'function' ) {
14
+ return expectAsync ( promise ) . toBeRejectedWithError ( ) ;
15
+ } else {
16
+ // @ts -expect-error
17
+ return expect ( promise ) . rejects . toThrowError ( ) ;
18
+ }
19
+ }
20
+
21
+ function expectResolved ( promise : Promise < any > ) {
22
+ if ( typeof expectAsync === 'function' ) {
23
+ return expectAsync ( promise ) . toBeResolved ( ) ;
24
+ } else {
25
+ // @ts -expect-error
26
+ return expect ( promise ) . resolves ;
27
+ }
28
+ }
29
+
12
30
describe ( 'promiseWithAbort' , ( ) => {
13
31
it ( 'should reject with an AbortError when the signal is aborted' , async ( ) => {
14
32
const abortController = new AbortController ( ) ;
15
33
const promise = promiseWithAbort ( setTimeout ( 500 ) , abortController . signal , 'Test operation' ) ;
16
34
35
+ console . error ( 'queueMicrotask to abort the signal' ) ;
17
36
queueMicrotask ( ( ) => {
18
37
abortController . abort ( 'Test reason' ) ;
19
38
} ) ;
20
39
21
- await expectAsync ( promise ) . toBeRejectedWithError ( ) ;
40
+ console . error ( 'expectAsync to be rejected with AbortError' ) ;
41
+ await expectRejectedWithError ( promise ) ;
22
42
} ) ;
23
43
24
44
it ( 'should not reject if the signal is not aborted' , async ( ) => {
@@ -31,6 +51,6 @@ describe('promiseWithAbort', () => {
31
51
// Wait briefly to ensure no rejection occurs
32
52
await setTimeout ( 20 ) ;
33
53
34
- await expectAsync ( promise ) . toBeResolved ( ) ;
54
+ await expectResolved ( promise ) ;
35
55
} ) ;
36
56
} ) ;
0 commit comments