forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-runner-custom-assertions.js
More file actions
63 lines (53 loc) · 1.51 KB
/
test-runner-custom-assertions.js
File metadata and controls
63 lines (53 loc) · 1.51 KB
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
'use strict';
require('../common');
const assert = require('node:assert');
const { test, assert: testAssertions } = require('node:test');
testAssertions.register('isOdd', (n) => {
assert.strictEqual(n % 2, 1);
});
testAssertions.register('ok', () => {
return 'ok';
});
testAssertions.register('snapshot', () => {
return 'snapshot';
});
testAssertions.register('deepStrictEqual', () => {
return 'deepStrictEqual';
});
testAssertions.register('context', function() {
return this;
});
test('throws if name is not a string', () => {
assert.throws(() => {
testAssertions.register(5);
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "name" argument must be of type string. Received type number (5)'
});
});
test('throws if fn is not a function', () => {
assert.throws(() => {
testAssertions.register('foo', 5);
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "fn" argument must be of type function. Received type number (5)'
});
});
test('invokes a custom assertion as part of the test plan', (t) => {
t.plan(2);
t.assert.isOdd(5);
assert.throws(() => {
t.assert.isOdd(4);
}, {
code: 'ERR_ASSERTION',
message: /Expected values to be strictly equal/
});
});
test('can override existing assertions', (t) => {
assert.strictEqual(t.assert.ok(), 'ok');
assert.strictEqual(t.assert.snapshot(), 'snapshot');
assert.strictEqual(t.assert.deepStrictEqual(), 'deepStrictEqual');
});
test('"this" is set to the TestContext', (t) => {
assert.strictEqual(t.assert.context(), t);
});