|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('node:assert'); |
| 4 | +const { test, assert: testAssertions } = require('node:test'); |
| 5 | + |
| 6 | +testAssertions.register('isOdd', (n) => { |
| 7 | + assert.strictEqual(n % 2, 1); |
| 8 | +}); |
| 9 | + |
| 10 | +testAssertions.register('ok', () => { |
| 11 | + return 'ok'; |
| 12 | +}); |
| 13 | + |
| 14 | +testAssertions.register('snapshot', () => { |
| 15 | + return 'snapshot'; |
| 16 | +}); |
| 17 | + |
| 18 | +testAssertions.register('deepStrictEqual', () => { |
| 19 | + return 'deepStrictEqual'; |
| 20 | +}); |
| 21 | + |
| 22 | +testAssertions.register('context', function() { |
| 23 | + return this; |
| 24 | +}); |
| 25 | + |
| 26 | +test('throws if name is not a string', () => { |
| 27 | + assert.throws(() => { |
| 28 | + testAssertions.register(5); |
| 29 | + }, { |
| 30 | + code: 'ERR_INVALID_ARG_TYPE', |
| 31 | + message: 'The "name" argument must be of type string. Received type number (5)' |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +test('throws if fn is not a function', () => { |
| 36 | + assert.throws(() => { |
| 37 | + testAssertions.register('foo', 5); |
| 38 | + }, { |
| 39 | + code: 'ERR_INVALID_ARG_TYPE', |
| 40 | + message: 'The "fn" argument must be of type function. Received type number (5)' |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +test('invokes a custom assertion as part of the test plan', (t) => { |
| 45 | + t.plan(2); |
| 46 | + t.assert.isOdd(5); |
| 47 | + assert.throws(() => { |
| 48 | + t.assert.isOdd(4); |
| 49 | + }, { |
| 50 | + code: 'ERR_ASSERTION', |
| 51 | + message: /Expected values to be strictly equal/ |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('can override existing assertions', (t) => { |
| 56 | + assert.strictEqual(t.assert.ok(), 'ok'); |
| 57 | + assert.strictEqual(t.assert.snapshot(), 'snapshot'); |
| 58 | + assert.strictEqual(t.assert.deepStrictEqual(), 'deepStrictEqual'); |
| 59 | +}); |
| 60 | + |
| 61 | +test('"this" is set to the TestContext', (t) => { |
| 62 | + assert.strictEqual(t.assert.context(), t); |
| 63 | +}); |
0 commit comments