1
1
'use strict' ;
2
2
const common = require ( '../common' ) ;
3
3
4
+ // This test checks that the semantics of `util.callbackify` are as described in
5
+ // the API docs
6
+
4
7
const assert = require ( 'assert' ) ;
5
8
const { callbackify } = require ( 'util' ) ;
6
9
const { join } = require ( 'path' ) ;
7
10
const { execFile } = require ( 'child_process' ) ;
8
11
const fixtureDir = join ( common . fixturesDir , 'uncaught-exceptions' ) ;
9
- const sentinalValues = [
12
+ const sentinelValues = [
10
13
'hello world' ,
11
14
null ,
12
15
undefined ,
@@ -18,8 +21,8 @@ const sentinalValues = [
18
21
] ;
19
22
20
23
{
21
- // Just works
22
- for ( const sentinel of sentinalValues ) {
24
+ // Test that the resolution value is passed as second argument to callback
25
+ for ( const sentinel of sentinelValues ) {
23
26
async function asyncFn ( ) {
24
27
return await Promise . resolve ( sentinel ) ;
25
28
}
@@ -45,8 +48,8 @@ const sentinalValues = [
45
48
}
46
49
47
50
{
48
- // Rejections
49
- for ( const sentinel of sentinalValues ) {
51
+ // Test that rejection reason is passed as first argument to callback
52
+ for ( const sentinel of sentinelValues ) {
50
53
async function asyncFn ( ) {
51
54
return await Promise . reject ( sentinel ) ;
52
55
}
@@ -56,16 +59,19 @@ const sentinalValues = [
56
59
cbAsyncFn ( common . mustCall ( ( err , ret ) => {
57
60
assert . strictEqual ( ret , undefined ) ;
58
61
if ( err instanceof Error ) {
59
- assert . strictEqual ( String ( sentinel ) . endsWith ( err . message ) , true ) ;
60
- if ( 'cause' in err )
61
- assert . strictEqual ( err . cause , sentinel ) ;
62
+ if ( 'reason' in err ) {
63
+ assert . strictEqual ( err . code , 'NULL_REJECTION' ) ;
64
+ assert . strictEqual ( err . reason , sentinel ) ;
65
+ } else {
66
+ assert . strictEqual ( String ( sentinel ) . endsWith ( err . message ) , true ) ;
67
+ }
62
68
} else {
63
69
assert . strictEqual ( err , sentinel ) ;
64
70
}
65
71
} ) ) ;
66
72
}
67
73
68
- for ( const sentinel of sentinalValues ) {
74
+ for ( const sentinel of sentinelValues ) {
69
75
function promiseFn ( ) {
70
76
return Promise . reject ( sentinel ) ;
71
77
}
@@ -75,9 +81,12 @@ const sentinalValues = [
75
81
cbPromiseFn ( common . mustCall ( ( err , ret ) => {
76
82
assert . strictEqual ( ret , undefined ) ;
77
83
if ( err instanceof Error ) {
78
- assert . strictEqual ( String ( sentinel ) . endsWith ( err . message ) , true ) ;
79
- if ( 'cause' in err )
80
- assert . strictEqual ( err . cause , sentinel ) ;
84
+ if ( 'reason' in err ) {
85
+ assert . strictEqual ( err . code , 'NULL_REJECTION' ) ;
86
+ assert . strictEqual ( err . reason , sentinel ) ;
87
+ } else {
88
+ assert . strictEqual ( String ( sentinel ) . endsWith ( err . message ) , true ) ;
89
+ }
81
90
} else {
82
91
assert . strictEqual ( err , sentinel ) ;
83
92
}
@@ -86,8 +95,8 @@ const sentinalValues = [
86
95
}
87
96
88
97
{
89
- // 3. args work
90
- for ( const sentinel of sentinalValues ) {
98
+ // Test that arguments passed to callbackified function are passed to original
99
+ for ( const sentinel of sentinelValues ) {
91
100
async function asyncFn ( arg ) {
92
101
assert . strictEqual ( arg , sentinel ) ;
93
102
return await Promise . resolve ( arg ) ;
@@ -115,8 +124,8 @@ const sentinalValues = [
115
124
}
116
125
117
126
{
118
- // `this` binding
119
- for ( const sentinel of sentinalValues ) {
127
+ // Test that `this` binding is the same for callbackified and original
128
+ for ( const sentinel of sentinelValues ) {
120
129
const iAmThis = {
121
130
fn ( arg ) {
122
131
assert . strictEqual ( this , iAmThis ) ;
@@ -148,7 +157,7 @@ const sentinalValues = [
148
157
}
149
158
150
159
{
151
- // `uncaughtException` aborts process
160
+ // Test that callback that throws emits an `uncaughtException` event
152
161
const fixture = join ( fixtureDir , 'callbackify1.js' ) ;
153
162
execFile (
154
163
process . argv [ 0 ] ,
@@ -165,7 +174,7 @@ const sentinalValues = [
165
174
}
166
175
167
176
{
168
- // handled `uncaughtException` works and passes rejection reason
177
+ // Test that handled `uncaughtException` works and passes rejection reason
169
178
const fixture = join ( fixtureDir , 'callbackify2.js' ) ;
170
179
execFile (
171
180
process . argv [ 0 ] ,
0 commit comments