@@ -3,15 +3,26 @@ import { assertEquals, assertRejects } from "@std/assert";
3
3
import { abortable } from "./abortable.ts" ;
4
4
import { delay } from "./delay.ts" ;
5
5
6
- Deno . test ( "abortable() handles promise" , async ( ) => {
6
+ Deno . test ( "abortable() handles resolved promise" , async ( ) => {
7
7
const c = new AbortController ( ) ;
8
8
const { promise, resolve } = Promise . withResolvers < string > ( ) ;
9
9
setTimeout ( ( ) => resolve ( "Hello" ) , 10 ) ;
10
10
const result = await abortable ( promise , c . signal ) ;
11
11
assertEquals ( result , "Hello" ) ;
12
12
} ) ;
13
13
14
- Deno . test ( "abortable() handles promise with aborted signal after delay" , async ( ) => {
14
+ Deno . test ( "abortable() handles rejected promise" , async ( ) => {
15
+ const c = new AbortController ( ) ;
16
+ const { promise, reject } = Promise . withResolvers < string > ( ) ;
17
+ setTimeout ( ( ) => reject ( new Error ( "This is my error" ) ) , 10 ) ;
18
+ await assertRejects (
19
+ ( ) => abortable ( promise , c . signal ) ,
20
+ Error ,
21
+ "This is my error" ,
22
+ ) ;
23
+ } ) ;
24
+
25
+ Deno . test ( "abortable() handles resolved promise with aborted signal after delay" , async ( ) => {
15
26
const c = new AbortController ( ) ;
16
27
const { promise, resolve } = Promise . withResolvers < string > ( ) ;
17
28
setTimeout ( ( ) => resolve ( "Hello" ) , 10 ) ;
@@ -25,7 +36,22 @@ Deno.test("abortable() handles promise with aborted signal after delay", async (
25
36
await delay ( 5 ) ; // wait for the promise to resolve
26
37
} ) ;
27
38
28
- Deno . test ( "abortable() handles promise with aborted signal after delay with reason" , async ( ) => {
39
+ Deno . test ( "abortable() handles rejected promise with aborted signal after delay" , async ( ) => {
40
+ const c = new AbortController ( ) ;
41
+ const { promise, reject } = Promise . withResolvers < string > ( ) ;
42
+ setTimeout ( ( ) => reject ( new Error ( "This is my error" ) ) , 10 ) ;
43
+ setTimeout ( ( ) => c . abort ( ) , 5 ) ;
44
+ const error = await assertRejects (
45
+ ( ) => abortable ( promise , c . signal ) ,
46
+ DOMException ,
47
+ "The signal has been aborted" ,
48
+ ) ;
49
+ assertEquals ( error . name , "AbortError" ) ;
50
+ await delay ( 5 ) ; // wait for the promise to reject
51
+ // an uncaught error should not occur
52
+ } ) ;
53
+
54
+ Deno . test ( "abortable() handles resolved promise with aborted signal after delay with reason" , async ( ) => {
29
55
const c = new AbortController ( ) ;
30
56
const { promise, resolve } = Promise . withResolvers < string > ( ) ;
31
57
setTimeout ( ( ) => resolve ( "Hello" ) , 10 ) ;
@@ -38,7 +64,7 @@ Deno.test("abortable() handles promise with aborted signal after delay with reas
38
64
await delay ( 5 ) ; // wait for the promise to resolve
39
65
} ) ;
40
66
41
- Deno . test ( "abortable() handles promise with already aborted signal" , async ( ) => {
67
+ Deno . test ( "abortable() handles resolved promise with already aborted signal" , async ( ) => {
42
68
const c = new AbortController ( ) ;
43
69
const { promise, resolve } = Promise . withResolvers < string > ( ) ;
44
70
setTimeout ( ( ) => resolve ( "Hello" ) , 10 ) ;
@@ -54,7 +80,24 @@ Deno.test("abortable() handles promise with already aborted signal", async () =>
54
80
await delay ( 10 ) ; // wait for the promise to resolve
55
81
} ) ;
56
82
57
- Deno . test ( "abortable() handles promise with already aborted signal with reason" , async ( ) => {
83
+ Deno . test ( "abortable() handles rejected promise with already aborted signal" , async ( ) => {
84
+ const c = new AbortController ( ) ;
85
+ const { promise, reject } = Promise . withResolvers < string > ( ) ;
86
+ setTimeout ( ( ) => reject ( new Error ( "This is my error" ) ) , 10 ) ;
87
+ c . abort ( ) ;
88
+ const error = await assertRejects (
89
+ async ( ) => {
90
+ await abortable ( promise , c . signal ) ;
91
+ } ,
92
+ DOMException ,
93
+ "The signal has been aborted" ,
94
+ ) ;
95
+ assertEquals ( error . name , "AbortError" ) ;
96
+ await delay ( 10 ) ; // wait for the promise to reject
97
+ // an uncaught error should not occur
98
+ } ) ;
99
+
100
+ Deno . test ( "abortable() handles resolved promise with already aborted signal and reason" , async ( ) => {
58
101
const c = new AbortController ( ) ;
59
102
const { promise, resolve } = Promise . withResolvers < string > ( ) ;
60
103
setTimeout ( ( ) => resolve ( "Hello" ) , 10 ) ;
0 commit comments