forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-assert-async.js
73 lines (64 loc) · 1.98 KB
/
test-assert-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
const common = require('../common');
const assert = require('assert');
const { promisify } = require('util');
const wait = promisify(setTimeout);
/* eslint-disable prefer-common-expectserror, no-restricted-properties */
// Test assert.rejects() and assert.doesNotReject() by checking their
// expected output and by verifying that they do not work sync
common.crashOnUnhandledRejection();
(async () => {
await assert.rejects(
async () => assert.fail(),
common.expectsError({
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'Failed'
})
);
await assert.doesNotReject(() => {});
{
const promise = assert.doesNotReject(async () => {
await wait(1);
throw new Error();
});
await assert.rejects(
() => promise,
(err) => {
assert(err instanceof assert.AssertionError,
`${err.name} is not instance of AssertionError`);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert(/^Got unwanted rejection\.\n$/.test(err.message));
assert.strictEqual(err.operator, 'doesNotReject');
assert.ok(!err.stack.includes('at Function.doesNotReject'));
return true;
}
);
}
{
const promise = assert.rejects(() => {});
await assert.rejects(
() => promise,
(err) => {
assert(err instanceof assert.AssertionError,
`${err.name} is not instance of AssertionError`);
assert.strictEqual(err.code, 'ERR_ASSERTION');
assert(/^Missing expected rejection\.$/.test(err.message));
assert.strictEqual(err.operator, 'rejects');
assert.ok(!err.stack.includes('at Function.rejects'));
return true;
}
);
}
{
const THROWN_ERROR = new Error();
await assert.rejects(() => {
throw THROWN_ERROR;
}).then(common.mustNotCall())
.catch(
common.mustCall((err) => {
assert.strictEqual(err, THROWN_ERROR);
})
);
}
})().then(common.mustCall());