Skip to content

Commit b8f8f4b

Browse files
committed
[squash] cleanup tests
1 parent c0e5464 commit b8f8f4b

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

test/async-hooks/test-promise.chain-promise-before-init-hooks.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,34 @@ const assert = require('assert');
55
const initHooks = require('./init-hooks');
66
const { checkInvocations } = require('./hook-checks');
77

8-
const p = (new Promise(common.mustCall(executor)));
9-
p.then(afterresolution);
8+
const p = new Promise(common.mustCall(function executor(resolve, reject) {
9+
resolve(5);
10+
}));
11+
12+
p.then(function afterresolution(val) {
13+
assert.strictEqual(val, 5);
14+
return val;
15+
});
1016

1117
// init hooks after chained promise is created
1218
const hooks = initHooks();
1319
hooks._allowNoInit = true;
1420
hooks.enable();
1521

16-
function executor(resolve, reject) {
17-
resolve(5);
18-
}
19-
20-
function afterresolution(val) {
21-
assert.strictEqual(val, 5);
22-
return val;
23-
}
2422

25-
process.on('exit', onexit);
26-
function onexit() {
23+
process.on('exit', function onexit() {
2724
hooks.disable();
2825
hooks.sanityCheck('PROMISE');
2926

27+
// Because the init event was never emitted the hooks listener doesn't
28+
// know what the type was. Thus check for Unknown rather than PROMISE.
3029
const as = hooks.activitiesOfTypes('PROMISE');
3130
const unknown = hooks.activitiesOfTypes('Unknown');
32-
assert.strictEqual(as.length, 0, 'no activities with PROMISE type');
33-
assert.strictEqual(unknown.length, 1,
34-
'one activity with Unknown type which in fact is PROMISE');
31+
assert.strictEqual(as.length, 0);
32+
assert.strictEqual(unknown.length, 1);
3533

3634
const a0 = unknown[0];
37-
assert.strictEqual(a0.type, 'Unknown',
38-
'unknown request which in fact is PROMISE');
39-
assert.strictEqual(typeof a0.uid, 'number', 'uid is a number');
35+
assert.strictEqual(a0.type, 'Unknown');
36+
assert.strictEqual(typeof a0.uid, 'number');
4037
checkInvocations(a0, { before: 1, after: 1 }, 'when process exits');
41-
}
38+
});

test/async-hooks/test-promise.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ p.then(afterresolution);
1414

1515
function executor(resolve, reject) {
1616
const as = hooks.activitiesOfTypes('PROMISE');
17-
assert.strictEqual(as.length, 1, 'one activity');
17+
assert.strictEqual(as.length, 1, 'one activities');
1818
const a = as[0];
1919
checkInvocations(a, { init: 1 }, 'while in promise executor');
2020
resolve(5);
@@ -46,8 +46,7 @@ function onexit() {
4646
const a1 = as[1];
4747
assert.strictEqual(a1.type, 'PROMISE', 'promise request');
4848
assert.strictEqual(typeof a1.uid, 'number', 'uid is a number');
49-
assert.strictEqual(a1.triggerId, a0.uid,
50-
'child promise should use parent uid as triggerId');
49+
assert.strictEqual(a1.triggerId, a0.uid);
5150
// We expect a destroy hook as well but we cannot guarentee predictable gc.
5251
checkInvocations(a1, { init: 1, before: 1, after: 1 }, 'when process exits');
5352
}

test/async-hooks/test-promise.promise-before-init-hooks.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,38 @@ const assert = require('assert');
55
const initHooks = require('./init-hooks');
66
const { checkInvocations } = require('./hook-checks');
77

8-
const p = (new Promise(common.mustCall(executor)));
8+
const p = new Promise(common.mustCall(function executor(resolve, reject) {
9+
resolve(5);
10+
}));
911

1012
// init hooks after promise was created
11-
const hooks = initHooks();
12-
hooks._allowNoInit = true;
13+
const hooks = initHooks({allowNoInit: true});
1314
hooks.enable();
1415

15-
p.then(afterresolution);
16-
17-
function executor(resolve, reject) {
18-
resolve(5);
19-
}
20-
21-
function afterresolution(val) {
16+
p.then(function afterresolution(val) {
2217
assert.strictEqual(val, 5);
2318
const as = hooks.activitiesOfTypes('PROMISE');
2419
assert.strictEqual(as.length, 1, 'one activity');
2520
checkInvocations(as[0], { init: 1, before: 1 },
2621
'after resolution child promise');
2722
return val;
28-
}
23+
});
2924

30-
process.on('exit', onexit);
31-
function onexit() {
25+
process.on('exit', function onexit() {
3226
hooks.disable();
3327
hooks.sanityCheck('PROMISE');
3428

3529
const as = hooks.activitiesOfTypes('PROMISE');
36-
assert.strictEqual(as.length, 1,
37-
'should only get one activity(child promise)');
30+
assert.strictEqual(as.length, 1);
3831

3932
const a0 = as[0];
40-
assert.strictEqual(a0.type, 'PROMISE', 'promise request');
41-
assert.strictEqual(typeof a0.uid, 'number', 'uid is a number');
42-
// we can't get the triggerId dynamically, we have to use static number here
43-
assert.strictEqual(a0.triggerId - (a0.uid - 3), 2,
44-
'child promise should use parent uid as triggerId');
33+
assert.strictEqual(a0.type, 'PROMISE');
34+
assert.strictEqual(typeof a0.uid, 'number');
35+
// we can't get the asyncId from the parent dynamically, since init was
36+
// never called. However, it is known that the parent promise was created
37+
// immediately before the child promise, thus there should only be one
38+
// difference in id.
39+
assert.strictEqual(a0.triggerId, a0.uid - 1);
4540
// We expect a destroy hook as well but we cannot guarentee predictable gc.
4641
checkInvocations(a0, { init: 1, before: 1, after: 1 }, 'when process exits');
47-
}
42+
});

0 commit comments

Comments
 (0)