|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { Assert } = require('assert'); |
| 6 | +const { test } = require('node:test'); |
| 7 | + |
| 8 | +// Disable colored output to prevent color codes from breaking assertion |
| 9 | +// message comparisons. This should only be an issue when process.stdout |
| 10 | +// is a TTY. |
| 11 | +if (process.stdout.isTTY) { |
| 12 | + process.env.NODE_DISABLE_COLORS = '1'; |
| 13 | +} |
| 14 | + |
| 15 | +test('Assert class basic instance', () => { |
| 16 | + const assertInstance = new Assert(); |
| 17 | + |
| 18 | + assertInstance.ok(assert.AssertionError.prototype instanceof Error, |
| 19 | + 'assert.AssertionError instanceof Error'); |
| 20 | + assertInstance.ok(true); |
| 21 | + assertInstance.throws( |
| 22 | + () => { assertInstance.fail(); }, |
| 23 | + { |
| 24 | + code: 'ERR_ASSERTION', |
| 25 | + name: 'AssertionError', |
| 26 | + message: 'Failed', |
| 27 | + operator: 'fail', |
| 28 | + actual: undefined, |
| 29 | + expected: undefined, |
| 30 | + generatedMessage: true, |
| 31 | + stack: /Failed/ |
| 32 | + } |
| 33 | + ); |
| 34 | + assertInstance.equal(undefined, undefined); |
| 35 | + assertInstance.notEqual(true, false); |
| 36 | + assertInstance.throws( |
| 37 | + () => assertInstance.deepEqual(/a/), |
| 38 | + { code: 'ERR_MISSING_ARGS' } |
| 39 | + ); |
| 40 | + assertInstance.throws( |
| 41 | + () => assertInstance.notDeepEqual('test'), |
| 42 | + { code: 'ERR_MISSING_ARGS' } |
| 43 | + ); |
| 44 | + assertInstance.notStrictEqual(2, '2'); |
| 45 | + assertInstance.throws(() => assertInstance.strictEqual(2, '2'), |
| 46 | + assertInstance.AssertionError, 'strictEqual(2, \'2\')'); |
| 47 | + assertInstance.throws( |
| 48 | + () => { |
| 49 | + assertInstance.partialDeepStrictEqual({ a: true }, { a: false }, 'custom message'); |
| 50 | + }, |
| 51 | + { |
| 52 | + code: 'ERR_ASSERTION', |
| 53 | + name: 'AssertionError', |
| 54 | + message: 'custom message\n+ actual - expected\n\n {\n+ a: true\n- a: false\n }\n' |
| 55 | + } |
| 56 | + ); |
| 57 | + assertInstance.throws( |
| 58 | + () => assertInstance.match(/abc/, 'string'), |
| 59 | + { |
| 60 | + code: 'ERR_INVALID_ARG_TYPE', |
| 61 | + message: 'The "regexp" argument must be an instance of RegExp. ' + |
| 62 | + "Received type string ('string')" |
| 63 | + } |
| 64 | + ); |
| 65 | + assertInstance.throws( |
| 66 | + () => assertInstance.doesNotMatch(/abc/, 'string'), |
| 67 | + { |
| 68 | + code: 'ERR_INVALID_ARG_TYPE', |
| 69 | + message: 'The "regexp" argument must be an instance of RegExp. ' + |
| 70 | + "Received type string ('string')" |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + /* eslint-disable no-restricted-syntax */ |
| 75 | + { |
| 76 | + function thrower(errorConstructor) { |
| 77 | + throw new errorConstructor({}); |
| 78 | + } |
| 79 | + |
| 80 | + let threw = false; |
| 81 | + try { |
| 82 | + assertInstance.doesNotThrow(() => thrower(TypeError), assertInstance.AssertionError); |
| 83 | + } catch (e) { |
| 84 | + threw = true; |
| 85 | + assertInstance.ok(e instanceof TypeError); |
| 86 | + } |
| 87 | + assertInstance.ok(threw, 'assertInstance.doesNotThrow with an explicit error is eating extra errors'); |
| 88 | + } |
| 89 | + { |
| 90 | + let threw = false; |
| 91 | + const rangeError = new RangeError('my range'); |
| 92 | + |
| 93 | + try { |
| 94 | + assertInstance.doesNotThrow(() => { |
| 95 | + throw new TypeError('wrong type'); |
| 96 | + }, TypeError, rangeError); |
| 97 | + } catch (e) { |
| 98 | + threw = true; |
| 99 | + assertInstance.ok(e.message.includes(rangeError.message)); |
| 100 | + assertInstance.ok(e instanceof assertInstance.AssertionError); |
| 101 | + assertInstance.ok(!e.stack.includes('doesNotThrow'), e); |
| 102 | + } |
| 103 | + assertInstance.ok(threw); |
| 104 | + } |
| 105 | + /* eslint-enable no-restricted-syntax */ |
| 106 | +}); |
0 commit comments