11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 31cb0ef41Sopenharmony_ci// 41cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 51cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 61cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 71cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 81cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 91cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 101cb0ef41Sopenharmony_ci// following conditions: 111cb0ef41Sopenharmony_ci// 121cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 131cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 141cb0ef41Sopenharmony_ci// 151cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 161cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 171cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 181cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 191cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 201cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 211cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci'use strict'; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst common = require('../common'); 261cb0ef41Sopenharmony_ciconst assert = require('assert'); 271cb0ef41Sopenharmony_ciconst { inspect } = require('util'); 281cb0ef41Sopenharmony_ciconst vm = require('vm'); 291cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding'); 301cb0ef41Sopenharmony_ciconst a = assert; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Disable colored output to prevent color codes from breaking assertion 331cb0ef41Sopenharmony_ci// message comparisons. This should only be an issue when process.stdout 341cb0ef41Sopenharmony_ci// is a TTY. 351cb0ef41Sopenharmony_ciif (process.stdout.isTTY) 361cb0ef41Sopenharmony_ci process.env.NODE_DISABLE_COLORS = '1'; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciconst strictEqualMessageStart = 'Expected values to be strictly equal:\n'; 391cb0ef41Sopenharmony_ciconst start = 'Expected values to be strictly deep-equal:'; 401cb0ef41Sopenharmony_ciconst actExp = '+ actual - expected'; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciassert.ok(a.AssertionError.prototype instanceof Error, 431cb0ef41Sopenharmony_ci 'a.AssertionError instanceof Error'); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ciassert.throws(() => a(false), a.AssertionError, 'ok(false)'); 461cb0ef41Sopenharmony_ciassert.throws(() => a.ok(false), a.AssertionError, 'ok(false)'); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci// Throw message if the message is instanceof Error. 491cb0ef41Sopenharmony_ci{ 501cb0ef41Sopenharmony_ci let threw = false; 511cb0ef41Sopenharmony_ci try { 521cb0ef41Sopenharmony_ci assert.ok(false, new Error('ok(false)')); 531cb0ef41Sopenharmony_ci } catch (e) { 541cb0ef41Sopenharmony_ci threw = true; 551cb0ef41Sopenharmony_ci assert.ok(e instanceof Error); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci assert.ok(threw, 'Error: ok(false)'); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_cia(true); 621cb0ef41Sopenharmony_cia('test', 'ok(\'test\')'); 631cb0ef41Sopenharmony_cia.ok(true); 641cb0ef41Sopenharmony_cia.ok('test'); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciassert.throws(() => a.equal(true, false), 671cb0ef41Sopenharmony_ci a.AssertionError, 'equal(true, false)'); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cia.equal(null, null); 701cb0ef41Sopenharmony_cia.equal(undefined, undefined); 711cb0ef41Sopenharmony_cia.equal(null, undefined); 721cb0ef41Sopenharmony_cia.equal(true, true); 731cb0ef41Sopenharmony_cia.equal(2, '2'); 741cb0ef41Sopenharmony_cia.notEqual(true, false); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciassert.throws(() => a.notEqual(true, true), 771cb0ef41Sopenharmony_ci a.AssertionError, 'notEqual(true, true)'); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ciassert.throws(() => a.strictEqual(2, '2'), 801cb0ef41Sopenharmony_ci a.AssertionError, 'strictEqual(2, \'2\')'); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci/* eslint-disable no-restricted-syntax */ 831cb0ef41Sopenharmony_ciassert.throws(() => a.strictEqual(null, undefined), 841cb0ef41Sopenharmony_ci a.AssertionError, 'strictEqual(null, undefined)'); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ciassert.throws( 871cb0ef41Sopenharmony_ci () => a.notStrictEqual(2, 2), 881cb0ef41Sopenharmony_ci { 891cb0ef41Sopenharmony_ci message: 'Expected "actual" to be strictly unequal to: 2', 901cb0ef41Sopenharmony_ci name: 'AssertionError' 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciassert.throws( 951cb0ef41Sopenharmony_ci () => a.notStrictEqual('a '.repeat(30), 'a '.repeat(30)), 961cb0ef41Sopenharmony_ci { 971cb0ef41Sopenharmony_ci message: 'Expected "actual" to be strictly unequal to:\n\n' + 981cb0ef41Sopenharmony_ci `'${'a '.repeat(30)}'`, 991cb0ef41Sopenharmony_ci name: 'AssertionError' 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciassert.throws( 1041cb0ef41Sopenharmony_ci () => a.notEqual(1, 1), 1051cb0ef41Sopenharmony_ci { 1061cb0ef41Sopenharmony_ci message: '1 != 1', 1071cb0ef41Sopenharmony_ci operator: '!=' 1081cb0ef41Sopenharmony_ci } 1091cb0ef41Sopenharmony_ci); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_cia.notStrictEqual(2, '2'); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci// Testing the throwing. 1141cb0ef41Sopenharmony_cifunction thrower(errorConstructor) { 1151cb0ef41Sopenharmony_ci throw new errorConstructor({}); 1161cb0ef41Sopenharmony_ci} 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci// The basic calls work. 1191cb0ef41Sopenharmony_ciassert.throws(() => thrower(a.AssertionError), a.AssertionError, 'message'); 1201cb0ef41Sopenharmony_ciassert.throws(() => thrower(a.AssertionError), a.AssertionError); 1211cb0ef41Sopenharmony_ciassert.throws(() => thrower(a.AssertionError)); 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci// If not passing an error, catch all. 1241cb0ef41Sopenharmony_ciassert.throws(() => thrower(TypeError)); 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci// When passing a type, only catch errors of the appropriate type. 1271cb0ef41Sopenharmony_ciassert.throws( 1281cb0ef41Sopenharmony_ci () => a.throws(() => thrower(TypeError), a.AssertionError), 1291cb0ef41Sopenharmony_ci { 1301cb0ef41Sopenharmony_ci generatedMessage: true, 1311cb0ef41Sopenharmony_ci actual: new TypeError({}), 1321cb0ef41Sopenharmony_ci expected: a.AssertionError, 1331cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1341cb0ef41Sopenharmony_ci name: 'AssertionError', 1351cb0ef41Sopenharmony_ci operator: 'throws', 1361cb0ef41Sopenharmony_ci message: 'The error is expected to be an instance of "AssertionError". ' + 1371cb0ef41Sopenharmony_ci 'Received "TypeError"\n\nError message:\n\n[object Object]' 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci); 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci// doesNotThrow should pass through all errors. 1421cb0ef41Sopenharmony_ci{ 1431cb0ef41Sopenharmony_ci let threw = false; 1441cb0ef41Sopenharmony_ci try { 1451cb0ef41Sopenharmony_ci a.doesNotThrow(() => thrower(TypeError), a.AssertionError); 1461cb0ef41Sopenharmony_ci } catch (e) { 1471cb0ef41Sopenharmony_ci threw = true; 1481cb0ef41Sopenharmony_ci assert.ok(e instanceof TypeError); 1491cb0ef41Sopenharmony_ci } 1501cb0ef41Sopenharmony_ci assert(threw, 'a.doesNotThrow with an explicit error is eating extra errors'); 1511cb0ef41Sopenharmony_ci} 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci// Key difference is that throwing our correct error makes an assertion error. 1541cb0ef41Sopenharmony_ci{ 1551cb0ef41Sopenharmony_ci let threw = false; 1561cb0ef41Sopenharmony_ci try { 1571cb0ef41Sopenharmony_ci a.doesNotThrow(() => thrower(TypeError), TypeError); 1581cb0ef41Sopenharmony_ci } catch (e) { 1591cb0ef41Sopenharmony_ci threw = true; 1601cb0ef41Sopenharmony_ci assert.ok(e instanceof a.AssertionError); 1611cb0ef41Sopenharmony_ci assert.ok(!e.stack.includes('at Function.doesNotThrow')); 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci assert.ok(threw, 'a.doesNotThrow is not catching type matching errors'); 1641cb0ef41Sopenharmony_ci} 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ciassert.throws( 1671cb0ef41Sopenharmony_ci () => a.doesNotThrow(() => thrower(Error), 'user message'), 1681cb0ef41Sopenharmony_ci { 1691cb0ef41Sopenharmony_ci name: 'AssertionError', 1701cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1711cb0ef41Sopenharmony_ci operator: 'doesNotThrow', 1721cb0ef41Sopenharmony_ci message: 'Got unwanted exception: user message\n' + 1731cb0ef41Sopenharmony_ci 'Actual message: "[object Object]"' 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci); 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ciassert.throws( 1781cb0ef41Sopenharmony_ci () => a.doesNotThrow(() => thrower(Error)), 1791cb0ef41Sopenharmony_ci { 1801cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1811cb0ef41Sopenharmony_ci message: 'Got unwanted exception.\nActual message: "[object Object]"' 1821cb0ef41Sopenharmony_ci } 1831cb0ef41Sopenharmony_ci); 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ciassert.throws( 1861cb0ef41Sopenharmony_ci () => a.doesNotThrow(() => thrower(Error), /\[[a-z]{6}\s[A-z]{6}\]/g, 'user message'), 1871cb0ef41Sopenharmony_ci { 1881cb0ef41Sopenharmony_ci name: 'AssertionError', 1891cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 1901cb0ef41Sopenharmony_ci operator: 'doesNotThrow', 1911cb0ef41Sopenharmony_ci message: 'Got unwanted exception: user message\n' + 1921cb0ef41Sopenharmony_ci 'Actual message: "[object Object]"' 1931cb0ef41Sopenharmony_ci } 1941cb0ef41Sopenharmony_ci); 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci// Make sure that validating using constructor really works. 1971cb0ef41Sopenharmony_ci{ 1981cb0ef41Sopenharmony_ci let threw = false; 1991cb0ef41Sopenharmony_ci try { 2001cb0ef41Sopenharmony_ci assert.throws( 2011cb0ef41Sopenharmony_ci () => { 2021cb0ef41Sopenharmony_ci throw ({}); // eslint-disable-line no-throw-literal 2031cb0ef41Sopenharmony_ci }, 2041cb0ef41Sopenharmony_ci Array 2051cb0ef41Sopenharmony_ci ); 2061cb0ef41Sopenharmony_ci } catch { 2071cb0ef41Sopenharmony_ci threw = true; 2081cb0ef41Sopenharmony_ci } 2091cb0ef41Sopenharmony_ci assert.ok(threw, 'wrong constructor validation'); 2101cb0ef41Sopenharmony_ci} 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci// Use a RegExp to validate the error message. 2131cb0ef41Sopenharmony_ci{ 2141cb0ef41Sopenharmony_ci a.throws(() => thrower(TypeError), /\[object Object\]/); 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci const symbol = Symbol('foo'); 2171cb0ef41Sopenharmony_ci a.throws(() => { 2181cb0ef41Sopenharmony_ci throw symbol; 2191cb0ef41Sopenharmony_ci }, /foo/); 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci a.throws(() => { 2221cb0ef41Sopenharmony_ci a.throws(() => { 2231cb0ef41Sopenharmony_ci throw symbol; 2241cb0ef41Sopenharmony_ci }, /abc/); 2251cb0ef41Sopenharmony_ci }, { 2261cb0ef41Sopenharmony_ci message: 'The input did not match the regular expression /abc/. ' + 2271cb0ef41Sopenharmony_ci "Input:\n\n'Symbol(foo)'\n", 2281cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 2291cb0ef41Sopenharmony_ci operator: 'throws', 2301cb0ef41Sopenharmony_ci actual: symbol, 2311cb0ef41Sopenharmony_ci expected: /abc/ 2321cb0ef41Sopenharmony_ci }); 2331cb0ef41Sopenharmony_ci} 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci// Use a fn to validate the error object. 2361cb0ef41Sopenharmony_cia.throws(() => thrower(TypeError), (err) => { 2371cb0ef41Sopenharmony_ci if ((err instanceof TypeError) && /\[object Object\]/.test(err)) { 2381cb0ef41Sopenharmony_ci return true; 2391cb0ef41Sopenharmony_ci } 2401cb0ef41Sopenharmony_ci}); 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/3188 2431cb0ef41Sopenharmony_ci{ 2441cb0ef41Sopenharmony_ci let actual; 2451cb0ef41Sopenharmony_ci assert.throws( 2461cb0ef41Sopenharmony_ci () => { 2471cb0ef41Sopenharmony_ci const ES6Error = class extends Error {}; 2481cb0ef41Sopenharmony_ci const AnotherErrorType = class extends Error {}; 2491cb0ef41Sopenharmony_ci 2501cb0ef41Sopenharmony_ci assert.throws(() => { 2511cb0ef41Sopenharmony_ci actual = new AnotherErrorType('foo'); 2521cb0ef41Sopenharmony_ci throw actual; 2531cb0ef41Sopenharmony_ci }, ES6Error); 2541cb0ef41Sopenharmony_ci }, 2551cb0ef41Sopenharmony_ci (err) => { 2561cb0ef41Sopenharmony_ci assert.strictEqual( 2571cb0ef41Sopenharmony_ci err.message, 2581cb0ef41Sopenharmony_ci 'The error is expected to be an instance of "ES6Error". ' + 2591cb0ef41Sopenharmony_ci 'Received "AnotherErrorType"\n\nError message:\n\nfoo' 2601cb0ef41Sopenharmony_ci ); 2611cb0ef41Sopenharmony_ci assert.strictEqual(err.actual, actual); 2621cb0ef41Sopenharmony_ci return true; 2631cb0ef41Sopenharmony_ci } 2641cb0ef41Sopenharmony_ci ); 2651cb0ef41Sopenharmony_ci} 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ci// Check messages from assert.throws(). 2681cb0ef41Sopenharmony_ci{ 2691cb0ef41Sopenharmony_ci const noop = () => {}; 2701cb0ef41Sopenharmony_ci assert.throws( 2711cb0ef41Sopenharmony_ci () => { a.throws((noop)); }, 2721cb0ef41Sopenharmony_ci { 2731cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 2741cb0ef41Sopenharmony_ci message: 'Missing expected exception.', 2751cb0ef41Sopenharmony_ci operator: 'throws', 2761cb0ef41Sopenharmony_ci actual: undefined, 2771cb0ef41Sopenharmony_ci expected: undefined 2781cb0ef41Sopenharmony_ci }); 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci assert.throws( 2811cb0ef41Sopenharmony_ci () => { a.throws(noop, TypeError); }, 2821cb0ef41Sopenharmony_ci { 2831cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 2841cb0ef41Sopenharmony_ci message: 'Missing expected exception (TypeError).', 2851cb0ef41Sopenharmony_ci actual: undefined, 2861cb0ef41Sopenharmony_ci expected: TypeError 2871cb0ef41Sopenharmony_ci }); 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci assert.throws( 2901cb0ef41Sopenharmony_ci () => { a.throws(noop, 'fhqwhgads'); }, 2911cb0ef41Sopenharmony_ci { 2921cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 2931cb0ef41Sopenharmony_ci message: 'Missing expected exception: fhqwhgads', 2941cb0ef41Sopenharmony_ci actual: undefined, 2951cb0ef41Sopenharmony_ci expected: undefined 2961cb0ef41Sopenharmony_ci }); 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ci assert.throws( 2991cb0ef41Sopenharmony_ci () => { a.throws(noop, TypeError, 'fhqwhgads'); }, 3001cb0ef41Sopenharmony_ci { 3011cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 3021cb0ef41Sopenharmony_ci message: 'Missing expected exception (TypeError): fhqwhgads', 3031cb0ef41Sopenharmony_ci actual: undefined, 3041cb0ef41Sopenharmony_ci expected: TypeError 3051cb0ef41Sopenharmony_ci }); 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ci let threw = false; 3081cb0ef41Sopenharmony_ci try { 3091cb0ef41Sopenharmony_ci a.throws(noop); 3101cb0ef41Sopenharmony_ci } catch (e) { 3111cb0ef41Sopenharmony_ci threw = true; 3121cb0ef41Sopenharmony_ci assert.ok(e instanceof a.AssertionError); 3131cb0ef41Sopenharmony_ci assert.ok(!e.stack.includes('at Function.throws')); 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci assert.ok(threw); 3161cb0ef41Sopenharmony_ci} 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ciconst circular = { y: 1 }; 3191cb0ef41Sopenharmony_cicircular.x = circular; 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_cifunction testAssertionMessage(actual, expected, msg) { 3221cb0ef41Sopenharmony_ci assert.throws( 3231cb0ef41Sopenharmony_ci () => assert.strictEqual(actual, ''), 3241cb0ef41Sopenharmony_ci { 3251cb0ef41Sopenharmony_ci generatedMessage: true, 3261cb0ef41Sopenharmony_ci message: msg || strictEqualMessageStart + 3271cb0ef41Sopenharmony_ci `+ actual - expected\n\n+ ${expected}\n- ''` 3281cb0ef41Sopenharmony_ci } 3291cb0ef41Sopenharmony_ci ); 3301cb0ef41Sopenharmony_ci} 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_cifunction testShortAssertionMessage(actual, expected) { 3331cb0ef41Sopenharmony_ci testAssertionMessage(actual, expected, strictEqualMessageStart + 3341cb0ef41Sopenharmony_ci `\n${inspect(actual)} !== ''\n`); 3351cb0ef41Sopenharmony_ci} 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_citestShortAssertionMessage(null, 'null'); 3381cb0ef41Sopenharmony_citestShortAssertionMessage(true, 'true'); 3391cb0ef41Sopenharmony_citestShortAssertionMessage(false, 'false'); 3401cb0ef41Sopenharmony_citestShortAssertionMessage(100, '100'); 3411cb0ef41Sopenharmony_citestShortAssertionMessage(NaN, 'NaN'); 3421cb0ef41Sopenharmony_citestShortAssertionMessage(Infinity, 'Infinity'); 3431cb0ef41Sopenharmony_citestShortAssertionMessage('a', '"a"'); 3441cb0ef41Sopenharmony_citestShortAssertionMessage('foo', '\'foo\''); 3451cb0ef41Sopenharmony_citestShortAssertionMessage(0, '0'); 3461cb0ef41Sopenharmony_citestShortAssertionMessage(Symbol(), 'Symbol()'); 3471cb0ef41Sopenharmony_citestShortAssertionMessage(undefined, 'undefined'); 3481cb0ef41Sopenharmony_citestShortAssertionMessage(-Infinity, '-Infinity'); 3491cb0ef41Sopenharmony_citestAssertionMessage([], '[]'); 3501cb0ef41Sopenharmony_citestAssertionMessage(/a/, '/a/'); 3511cb0ef41Sopenharmony_citestAssertionMessage(/abc/gim, '/abc/gim'); 3521cb0ef41Sopenharmony_citestAssertionMessage({}, '{}'); 3531cb0ef41Sopenharmony_citestAssertionMessage([1, 2, 3], '[\n+ 1,\n+ 2,\n+ 3\n+ ]'); 3541cb0ef41Sopenharmony_citestAssertionMessage(function f() {}, '[Function: f]'); 3551cb0ef41Sopenharmony_citestAssertionMessage(function() {}, '[Function (anonymous)]'); 3561cb0ef41Sopenharmony_citestAssertionMessage(circular, 3571cb0ef41Sopenharmony_ci '<ref *1> {\n+ x: [Circular *1],\n+ y: 1\n+ }'); 3581cb0ef41Sopenharmony_citestAssertionMessage({ a: undefined, b: null }, 3591cb0ef41Sopenharmony_ci '{\n+ a: undefined,\n+ b: null\n+ }'); 3601cb0ef41Sopenharmony_citestAssertionMessage({ a: NaN, b: Infinity, c: -Infinity }, 3611cb0ef41Sopenharmony_ci '{\n+ a: NaN,\n+ b: Infinity,\n+ c: -Infinity\n+ }'); 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/5292 3641cb0ef41Sopenharmony_ciassert.throws( 3651cb0ef41Sopenharmony_ci () => assert.strictEqual(1, 2), 3661cb0ef41Sopenharmony_ci { 3671cb0ef41Sopenharmony_ci message: `${strictEqualMessageStart}\n1 !== 2\n`, 3681cb0ef41Sopenharmony_ci generatedMessage: true 3691cb0ef41Sopenharmony_ci } 3701cb0ef41Sopenharmony_ci); 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ciassert.throws( 3731cb0ef41Sopenharmony_ci () => assert.strictEqual(1, 2, 'oh no'), 3741cb0ef41Sopenharmony_ci { 3751cb0ef41Sopenharmony_ci message: 'oh no', 3761cb0ef41Sopenharmony_ci generatedMessage: false 3771cb0ef41Sopenharmony_ci } 3781cb0ef41Sopenharmony_ci); 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci{ 3811cb0ef41Sopenharmony_ci let threw = false; 3821cb0ef41Sopenharmony_ci const rangeError = new RangeError('my range'); 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci // Verify custom errors. 3851cb0ef41Sopenharmony_ci try { 3861cb0ef41Sopenharmony_ci assert.strictEqual(1, 2, rangeError); 3871cb0ef41Sopenharmony_ci } catch (e) { 3881cb0ef41Sopenharmony_ci assert.strictEqual(e, rangeError); 3891cb0ef41Sopenharmony_ci threw = true; 3901cb0ef41Sopenharmony_ci assert.ok(e instanceof RangeError, 'Incorrect error type thrown'); 3911cb0ef41Sopenharmony_ci } 3921cb0ef41Sopenharmony_ci assert.ok(threw); 3931cb0ef41Sopenharmony_ci threw = false; 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci // Verify AssertionError is the result from doesNotThrow with custom Error. 3961cb0ef41Sopenharmony_ci try { 3971cb0ef41Sopenharmony_ci a.doesNotThrow(() => { 3981cb0ef41Sopenharmony_ci throw new TypeError('wrong type'); 3991cb0ef41Sopenharmony_ci }, TypeError, rangeError); 4001cb0ef41Sopenharmony_ci } catch (e) { 4011cb0ef41Sopenharmony_ci threw = true; 4021cb0ef41Sopenharmony_ci assert.ok(e.message.includes(rangeError.message)); 4031cb0ef41Sopenharmony_ci assert.ok(e instanceof assert.AssertionError); 4041cb0ef41Sopenharmony_ci assert.ok(!e.stack.includes('doesNotThrow'), e); 4051cb0ef41Sopenharmony_ci } 4061cb0ef41Sopenharmony_ci assert.ok(threw); 4071cb0ef41Sopenharmony_ci} 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ci{ 4101cb0ef41Sopenharmony_ci // Verify that throws() and doesNotThrow() throw on non-functions. 4111cb0ef41Sopenharmony_ci const testBlockTypeError = (method, fn) => { 4121cb0ef41Sopenharmony_ci assert.throws( 4131cb0ef41Sopenharmony_ci () => method(fn), 4141cb0ef41Sopenharmony_ci { 4151cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 4161cb0ef41Sopenharmony_ci name: 'TypeError', 4171cb0ef41Sopenharmony_ci message: 'The "fn" argument must be of type function.' + 4181cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(fn) 4191cb0ef41Sopenharmony_ci } 4201cb0ef41Sopenharmony_ci ); 4211cb0ef41Sopenharmony_ci }; 4221cb0ef41Sopenharmony_ci 4231cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, 'string'); 4241cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, 'string'); 4251cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, 1); 4261cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, 1); 4271cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, true); 4281cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, true); 4291cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, false); 4301cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, false); 4311cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, []); 4321cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, []); 4331cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, {}); 4341cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, {}); 4351cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, /foo/); 4361cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, /foo/); 4371cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, null); 4381cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, null); 4391cb0ef41Sopenharmony_ci testBlockTypeError(assert.throws, undefined); 4401cb0ef41Sopenharmony_ci testBlockTypeError(assert.doesNotThrow, undefined); 4411cb0ef41Sopenharmony_ci} 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/3275 4441cb0ef41Sopenharmony_ci// eslint-disable-next-line no-throw-literal 4451cb0ef41Sopenharmony_ciassert.throws(() => { throw 'error'; }, (err) => err === 'error'); 4461cb0ef41Sopenharmony_ciassert.throws(() => { throw new Error(); }, (err) => err instanceof Error); 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ci// Long values should be truncated for display. 4491cb0ef41Sopenharmony_ciassert.throws(() => { 4501cb0ef41Sopenharmony_ci assert.strictEqual('A'.repeat(1000), ''); 4511cb0ef41Sopenharmony_ci}, (err) => { 4521cb0ef41Sopenharmony_ci assert.strictEqual(err.code, 'ERR_ASSERTION'); 4531cb0ef41Sopenharmony_ci assert.strictEqual(err.message, 4541cb0ef41Sopenharmony_ci `${strictEqualMessageStart}+ actual - expected\n\n` + 4551cb0ef41Sopenharmony_ci `+ '${'A'.repeat(1000)}'\n- ''`); 4561cb0ef41Sopenharmony_ci assert.strictEqual(err.actual.length, 1000); 4571cb0ef41Sopenharmony_ci assert.ok(inspect(err).includes(`actual: '${'A'.repeat(488)}...'`)); 4581cb0ef41Sopenharmony_ci return true; 4591cb0ef41Sopenharmony_ci}); 4601cb0ef41Sopenharmony_ci 4611cb0ef41Sopenharmony_ci// Output that extends beyond 10 lines should also be truncated for display. 4621cb0ef41Sopenharmony_ci{ 4631cb0ef41Sopenharmony_ci const multilineString = 'fhqwhgads\n'.repeat(15); 4641cb0ef41Sopenharmony_ci assert.throws(() => { 4651cb0ef41Sopenharmony_ci assert.strictEqual(multilineString, ''); 4661cb0ef41Sopenharmony_ci }, (err) => { 4671cb0ef41Sopenharmony_ci assert.strictEqual(err.code, 'ERR_ASSERTION'); 4681cb0ef41Sopenharmony_ci assert.strictEqual(err.message.split('\n').length, 19); 4691cb0ef41Sopenharmony_ci assert.strictEqual(err.actual.split('\n').length, 16); 4701cb0ef41Sopenharmony_ci assert.ok(inspect(err).includes( 4711cb0ef41Sopenharmony_ci "actual: 'fhqwhgads\\n' +\n" + 4721cb0ef41Sopenharmony_ci " 'fhqwhgads\\n' +\n".repeat(9) + 4731cb0ef41Sopenharmony_ci " '...'")); 4741cb0ef41Sopenharmony_ci return true; 4751cb0ef41Sopenharmony_ci }); 4761cb0ef41Sopenharmony_ci} 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_ci{ 4791cb0ef41Sopenharmony_ci // Bad args to AssertionError constructor should throw TypeError. 4801cb0ef41Sopenharmony_ci const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined]; 4811cb0ef41Sopenharmony_ci args.forEach((input) => { 4821cb0ef41Sopenharmony_ci assert.throws( 4831cb0ef41Sopenharmony_ci () => new assert.AssertionError(input), 4841cb0ef41Sopenharmony_ci { 4851cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 4861cb0ef41Sopenharmony_ci name: 'TypeError', 4871cb0ef41Sopenharmony_ci message: 'The "options" argument must be of type object.' + 4881cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(input) 4891cb0ef41Sopenharmony_ci }); 4901cb0ef41Sopenharmony_ci }); 4911cb0ef41Sopenharmony_ci} 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ciassert.throws( 4941cb0ef41Sopenharmony_ci () => assert.strictEqual(new Error('foo'), new Error('foobar')), 4951cb0ef41Sopenharmony_ci { 4961cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 4971cb0ef41Sopenharmony_ci name: 'AssertionError', 4981cb0ef41Sopenharmony_ci message: 'Expected "actual" to be reference-equal to "expected":\n' + 4991cb0ef41Sopenharmony_ci '+ actual - expected\n\n' + 5001cb0ef41Sopenharmony_ci '+ [Error: foo]\n- [Error: foobar]' 5011cb0ef41Sopenharmony_ci } 5021cb0ef41Sopenharmony_ci); 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_cia.equal(NaN, NaN); 5051cb0ef41Sopenharmony_cia.throws( 5061cb0ef41Sopenharmony_ci () => a.notEqual(NaN, NaN), 5071cb0ef41Sopenharmony_ci a.AssertionError 5081cb0ef41Sopenharmony_ci); 5091cb0ef41Sopenharmony_ci 5101cb0ef41Sopenharmony_ci// Test strict assert. 5111cb0ef41Sopenharmony_ci{ 5121cb0ef41Sopenharmony_ci const a = require('assert'); 5131cb0ef41Sopenharmony_ci const assert = require('assert').strict; 5141cb0ef41Sopenharmony_ci /* eslint-disable no-restricted-properties */ 5151cb0ef41Sopenharmony_ci assert.throws(() => assert.equal(1, true), assert.AssertionError); 5161cb0ef41Sopenharmony_ci assert.notEqual(0, false); 5171cb0ef41Sopenharmony_ci assert.throws(() => assert.deepEqual(1, true), assert.AssertionError); 5181cb0ef41Sopenharmony_ci assert.notDeepEqual(0, false); 5191cb0ef41Sopenharmony_ci assert.equal(assert.strict, assert.strict.strict); 5201cb0ef41Sopenharmony_ci assert.equal(assert.equal, assert.strictEqual); 5211cb0ef41Sopenharmony_ci assert.equal(assert.deepEqual, assert.deepStrictEqual); 5221cb0ef41Sopenharmony_ci assert.equal(assert.notEqual, assert.notStrictEqual); 5231cb0ef41Sopenharmony_ci assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual); 5241cb0ef41Sopenharmony_ci assert.equal(Object.keys(assert).length, Object.keys(a).length); 5251cb0ef41Sopenharmony_ci assert(7); 5261cb0ef41Sopenharmony_ci assert.throws( 5271cb0ef41Sopenharmony_ci () => assert(...[]), 5281cb0ef41Sopenharmony_ci { 5291cb0ef41Sopenharmony_ci message: 'No value argument passed to `assert.ok()`', 5301cb0ef41Sopenharmony_ci name: 'AssertionError', 5311cb0ef41Sopenharmony_ci generatedMessage: true 5321cb0ef41Sopenharmony_ci } 5331cb0ef41Sopenharmony_ci ); 5341cb0ef41Sopenharmony_ci assert.throws( 5351cb0ef41Sopenharmony_ci () => a(), 5361cb0ef41Sopenharmony_ci { 5371cb0ef41Sopenharmony_ci message: 'No value argument passed to `assert.ok()`', 5381cb0ef41Sopenharmony_ci name: 'AssertionError' 5391cb0ef41Sopenharmony_ci } 5401cb0ef41Sopenharmony_ci ); 5411cb0ef41Sopenharmony_ci 5421cb0ef41Sopenharmony_ci // Test setting the limit to zero and that assert.strict works properly. 5431cb0ef41Sopenharmony_ci const tmpLimit = Error.stackTraceLimit; 5441cb0ef41Sopenharmony_ci Error.stackTraceLimit = 0; 5451cb0ef41Sopenharmony_ci assert.throws( 5461cb0ef41Sopenharmony_ci () => { 5471cb0ef41Sopenharmony_ci assert.ok( 5481cb0ef41Sopenharmony_ci typeof 123 === 'string' 5491cb0ef41Sopenharmony_ci ); 5501cb0ef41Sopenharmony_ci }, 5511cb0ef41Sopenharmony_ci { 5521cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 5531cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 5541cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 5551cb0ef41Sopenharmony_ci "assert.ok(\n typeof 123 === 'string'\n )\n" 5561cb0ef41Sopenharmony_ci } 5571cb0ef41Sopenharmony_ci ); 5581cb0ef41Sopenharmony_ci Error.stackTraceLimit = tmpLimit; 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_ci // Test error diffs. 5611cb0ef41Sopenharmony_ci let message = [ 5621cb0ef41Sopenharmony_ci start, 5631cb0ef41Sopenharmony_ci `${actExp} ... Lines skipped`, 5641cb0ef41Sopenharmony_ci '', 5651cb0ef41Sopenharmony_ci ' [', 5661cb0ef41Sopenharmony_ci ' [', 5671cb0ef41Sopenharmony_ci ' [', 5681cb0ef41Sopenharmony_ci ' 1,', 5691cb0ef41Sopenharmony_ci ' 2,', 5701cb0ef41Sopenharmony_ci '+ 3', 5711cb0ef41Sopenharmony_ci "- '3'", 5721cb0ef41Sopenharmony_ci ' ]', 5731cb0ef41Sopenharmony_ci '...', 5741cb0ef41Sopenharmony_ci ' 4,', 5751cb0ef41Sopenharmony_ci ' 5', 5761cb0ef41Sopenharmony_ci ' ]'].join('\n'); 5771cb0ef41Sopenharmony_ci assert.throws( 5781cb0ef41Sopenharmony_ci () => assert.deepEqual([[[1, 2, 3]], 4, 5], [[[1, 2, '3']], 4, 5]), 5791cb0ef41Sopenharmony_ci { message }); 5801cb0ef41Sopenharmony_ci 5811cb0ef41Sopenharmony_ci message = [ 5821cb0ef41Sopenharmony_ci start, 5831cb0ef41Sopenharmony_ci `${actExp} ... Lines skipped`, 5841cb0ef41Sopenharmony_ci '', 5851cb0ef41Sopenharmony_ci ' [', 5861cb0ef41Sopenharmony_ci ' 1,', 5871cb0ef41Sopenharmony_ci '...', 5881cb0ef41Sopenharmony_ci ' 1,', 5891cb0ef41Sopenharmony_ci ' 0,', 5901cb0ef41Sopenharmony_ci '- 1,', 5911cb0ef41Sopenharmony_ci ' 1,', 5921cb0ef41Sopenharmony_ci '...', 5931cb0ef41Sopenharmony_ci ' 1,', 5941cb0ef41Sopenharmony_ci ' 1', 5951cb0ef41Sopenharmony_ci ' ]', 5961cb0ef41Sopenharmony_ci ].join('\n'); 5971cb0ef41Sopenharmony_ci assert.throws( 5981cb0ef41Sopenharmony_ci () => assert.deepEqual( 5991cb0ef41Sopenharmony_ci [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], 6001cb0ef41Sopenharmony_ci [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]), 6011cb0ef41Sopenharmony_ci { message }); 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci message = [ 6041cb0ef41Sopenharmony_ci start, 6051cb0ef41Sopenharmony_ci `${actExp} ... Lines skipped`, 6061cb0ef41Sopenharmony_ci '', 6071cb0ef41Sopenharmony_ci ' [', 6081cb0ef41Sopenharmony_ci ' 1,', 6091cb0ef41Sopenharmony_ci '...', 6101cb0ef41Sopenharmony_ci ' 1,', 6111cb0ef41Sopenharmony_ci ' 0,', 6121cb0ef41Sopenharmony_ci '+ 1,', 6131cb0ef41Sopenharmony_ci ' 1,', 6141cb0ef41Sopenharmony_ci ' 1,', 6151cb0ef41Sopenharmony_ci ' 1', 6161cb0ef41Sopenharmony_ci ' ]', 6171cb0ef41Sopenharmony_ci ].join('\n'); 6181cb0ef41Sopenharmony_ci assert.throws( 6191cb0ef41Sopenharmony_ci () => assert.deepEqual( 6201cb0ef41Sopenharmony_ci [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1], 6211cb0ef41Sopenharmony_ci [1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1]), 6221cb0ef41Sopenharmony_ci { message }); 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci message = [ 6251cb0ef41Sopenharmony_ci start, 6261cb0ef41Sopenharmony_ci actExp, 6271cb0ef41Sopenharmony_ci '', 6281cb0ef41Sopenharmony_ci ' [', 6291cb0ef41Sopenharmony_ci ' 1,', 6301cb0ef41Sopenharmony_ci '+ 2,', 6311cb0ef41Sopenharmony_ci '- 1,', 6321cb0ef41Sopenharmony_ci ' 1,', 6331cb0ef41Sopenharmony_ci ' 1,', 6341cb0ef41Sopenharmony_ci ' 0,', 6351cb0ef41Sopenharmony_ci '+ 1,', 6361cb0ef41Sopenharmony_ci ' 1', 6371cb0ef41Sopenharmony_ci ' ]', 6381cb0ef41Sopenharmony_ci ].join('\n'); 6391cb0ef41Sopenharmony_ci assert.throws( 6401cb0ef41Sopenharmony_ci () => assert.deepEqual( 6411cb0ef41Sopenharmony_ci [1, 2, 1, 1, 0, 1, 1], 6421cb0ef41Sopenharmony_ci [1, 1, 1, 1, 0, 1]), 6431cb0ef41Sopenharmony_ci { message }); 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci message = [ 6461cb0ef41Sopenharmony_ci start, 6471cb0ef41Sopenharmony_ci actExp, 6481cb0ef41Sopenharmony_ci '', 6491cb0ef41Sopenharmony_ci '+ [', 6501cb0ef41Sopenharmony_ci '+ 1,', 6511cb0ef41Sopenharmony_ci '+ 2,', 6521cb0ef41Sopenharmony_ci '+ 1', 6531cb0ef41Sopenharmony_ci '+ ]', 6541cb0ef41Sopenharmony_ci '- undefined', 6551cb0ef41Sopenharmony_ci ].join('\n'); 6561cb0ef41Sopenharmony_ci assert.throws( 6571cb0ef41Sopenharmony_ci () => assert.deepEqual([1, 2, 1], undefined), 6581cb0ef41Sopenharmony_ci { message }); 6591cb0ef41Sopenharmony_ci 6601cb0ef41Sopenharmony_ci message = [ 6611cb0ef41Sopenharmony_ci start, 6621cb0ef41Sopenharmony_ci actExp, 6631cb0ef41Sopenharmony_ci '', 6641cb0ef41Sopenharmony_ci ' [', 6651cb0ef41Sopenharmony_ci '+ 1,', 6661cb0ef41Sopenharmony_ci ' 2,', 6671cb0ef41Sopenharmony_ci ' 1', 6681cb0ef41Sopenharmony_ci ' ]', 6691cb0ef41Sopenharmony_ci ].join('\n'); 6701cb0ef41Sopenharmony_ci assert.throws( 6711cb0ef41Sopenharmony_ci () => assert.deepEqual([1, 2, 1], [2, 1]), 6721cb0ef41Sopenharmony_ci { message }); 6731cb0ef41Sopenharmony_ci 6741cb0ef41Sopenharmony_ci message = `${start}\n` + 6751cb0ef41Sopenharmony_ci `${actExp} ... Lines skipped\n` + 6761cb0ef41Sopenharmony_ci '\n' + 6771cb0ef41Sopenharmony_ci ' [\n' + 6781cb0ef41Sopenharmony_ci '+ 1,\n'.repeat(25) + 6791cb0ef41Sopenharmony_ci '...\n' + 6801cb0ef41Sopenharmony_ci '- 2,\n'.repeat(25) + 6811cb0ef41Sopenharmony_ci '...'; 6821cb0ef41Sopenharmony_ci assert.throws( 6831cb0ef41Sopenharmony_ci () => assert.deepEqual(Array(28).fill(1), Array(28).fill(2)), 6841cb0ef41Sopenharmony_ci { message }); 6851cb0ef41Sopenharmony_ci 6861cb0ef41Sopenharmony_ci const obj1 = {}; 6871cb0ef41Sopenharmony_ci const obj2 = { loop: 'forever' }; 6881cb0ef41Sopenharmony_ci obj2[inspect.custom] = () => '{}'; 6891cb0ef41Sopenharmony_ci // No infinite loop and no custom inspect. 6901cb0ef41Sopenharmony_ci assert.throws(() => assert.deepEqual(obj1, obj2), { 6911cb0ef41Sopenharmony_ci message: `${start}\n` + 6921cb0ef41Sopenharmony_ci `${actExp}\n` + 6931cb0ef41Sopenharmony_ci '\n' + 6941cb0ef41Sopenharmony_ci '+ {}\n' + 6951cb0ef41Sopenharmony_ci '- {\n' + 6961cb0ef41Sopenharmony_ci '- [Symbol(nodejs.util.inspect.custom)]: [Function (anonymous)],\n' + 6971cb0ef41Sopenharmony_ci "- loop: 'forever'\n" + 6981cb0ef41Sopenharmony_ci '- }' 6991cb0ef41Sopenharmony_ci }); 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ci // notDeepEqual tests 7021cb0ef41Sopenharmony_ci assert.throws( 7031cb0ef41Sopenharmony_ci () => assert.notDeepEqual([1], [1]), 7041cb0ef41Sopenharmony_ci { 7051cb0ef41Sopenharmony_ci message: 'Expected "actual" not to be strictly deep-equal to:\n\n' + 7061cb0ef41Sopenharmony_ci '[\n 1\n]\n' 7071cb0ef41Sopenharmony_ci } 7081cb0ef41Sopenharmony_ci ); 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ci message = 'Expected "actual" not to be strictly deep-equal to:' + 7111cb0ef41Sopenharmony_ci `\n\n[${'\n 1,'.repeat(45)}\n...\n`; 7121cb0ef41Sopenharmony_ci const data = Array(51).fill(1); 7131cb0ef41Sopenharmony_ci assert.throws( 7141cb0ef41Sopenharmony_ci () => assert.notDeepEqual(data, data), 7151cb0ef41Sopenharmony_ci { message }); 7161cb0ef41Sopenharmony_ci /* eslint-enable no-restricted-properties */ 7171cb0ef41Sopenharmony_ci} 7181cb0ef41Sopenharmony_ci 7191cb0ef41Sopenharmony_ciassert.throws( 7201cb0ef41Sopenharmony_ci () => assert.ok(null), 7211cb0ef41Sopenharmony_ci { 7221cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7231cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 7241cb0ef41Sopenharmony_ci generatedMessage: true, 7251cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 7261cb0ef41Sopenharmony_ci 'assert.ok(null)\n' 7271cb0ef41Sopenharmony_ci } 7281cb0ef41Sopenharmony_ci); 7291cb0ef41Sopenharmony_ciassert.throws( 7301cb0ef41Sopenharmony_ci () => { 7311cb0ef41Sopenharmony_ci // This test case checks if `try` left brace without a line break 7321cb0ef41Sopenharmony_ci // before the assertion causes any wrong assertion message. 7331cb0ef41Sopenharmony_ci // Therefore, don't reformat the following code. 7341cb0ef41Sopenharmony_ci // Refs: https://github.com/nodejs/node/issues/30872 7351cb0ef41Sopenharmony_ci try { assert.ok(0); // eslint-disable-line no-useless-catch, brace-style 7361cb0ef41Sopenharmony_ci } catch (err) { 7371cb0ef41Sopenharmony_ci throw err; 7381cb0ef41Sopenharmony_ci } 7391cb0ef41Sopenharmony_ci }, 7401cb0ef41Sopenharmony_ci { 7411cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7421cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 7431cb0ef41Sopenharmony_ci generatedMessage: true, 7441cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 7451cb0ef41Sopenharmony_ci 'assert.ok(0)\n' 7461cb0ef41Sopenharmony_ci } 7471cb0ef41Sopenharmony_ci); 7481cb0ef41Sopenharmony_ciassert.throws( 7491cb0ef41Sopenharmony_ci () => { 7501cb0ef41Sopenharmony_ci try { 7511cb0ef41Sopenharmony_ci throw new Error(); 7521cb0ef41Sopenharmony_ci // This test case checks if `catch` left brace without a line break 7531cb0ef41Sopenharmony_ci // before the assertion causes any wrong assertion message. 7541cb0ef41Sopenharmony_ci // Therefore, don't reformat the following code. 7551cb0ef41Sopenharmony_ci // Refs: https://github.com/nodejs/node/issues/30872 7561cb0ef41Sopenharmony_ci } catch (err) { assert.ok(0); } // eslint-disable-line no-unused-vars 7571cb0ef41Sopenharmony_ci }, 7581cb0ef41Sopenharmony_ci { 7591cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7601cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 7611cb0ef41Sopenharmony_ci generatedMessage: true, 7621cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 7631cb0ef41Sopenharmony_ci 'assert.ok(0)\n' 7641cb0ef41Sopenharmony_ci } 7651cb0ef41Sopenharmony_ci); 7661cb0ef41Sopenharmony_ciassert.throws( 7671cb0ef41Sopenharmony_ci () => { 7681cb0ef41Sopenharmony_ci // This test case checks if `function` left brace without a line break 7691cb0ef41Sopenharmony_ci // before the assertion causes any wrong assertion message. 7701cb0ef41Sopenharmony_ci // Therefore, don't reformat the following code. 7711cb0ef41Sopenharmony_ci // Refs: https://github.com/nodejs/node/issues/30872 7721cb0ef41Sopenharmony_ci function test() { assert.ok(0); // eslint-disable-line brace-style 7731cb0ef41Sopenharmony_ci } 7741cb0ef41Sopenharmony_ci test(); 7751cb0ef41Sopenharmony_ci }, 7761cb0ef41Sopenharmony_ci { 7771cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7781cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 7791cb0ef41Sopenharmony_ci generatedMessage: true, 7801cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 7811cb0ef41Sopenharmony_ci 'assert.ok(0)\n' 7821cb0ef41Sopenharmony_ci } 7831cb0ef41Sopenharmony_ci); 7841cb0ef41Sopenharmony_ciassert.throws( 7851cb0ef41Sopenharmony_ci () => assert(typeof 123n === 'string'), 7861cb0ef41Sopenharmony_ci { 7871cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7881cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 7891cb0ef41Sopenharmony_ci generatedMessage: true, 7901cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 7911cb0ef41Sopenharmony_ci "assert(typeof 123n === 'string')\n" 7921cb0ef41Sopenharmony_ci } 7931cb0ef41Sopenharmony_ci); 7941cb0ef41Sopenharmony_ci 7951cb0ef41Sopenharmony_ciassert.throws( 7961cb0ef41Sopenharmony_ci () => assert(false, Symbol('foo')), 7971cb0ef41Sopenharmony_ci { 7981cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 7991cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8001cb0ef41Sopenharmony_ci generatedMessage: false, 8011cb0ef41Sopenharmony_ci message: 'Symbol(foo)' 8021cb0ef41Sopenharmony_ci } 8031cb0ef41Sopenharmony_ci); 8041cb0ef41Sopenharmony_ci 8051cb0ef41Sopenharmony_ci{ 8061cb0ef41Sopenharmony_ci // Test caching. 8071cb0ef41Sopenharmony_ci const fs = internalBinding('fs'); 8081cb0ef41Sopenharmony_ci const tmp = fs.close; 8091cb0ef41Sopenharmony_ci fs.close = common.mustCall(tmp, 1); 8101cb0ef41Sopenharmony_ci function throwErr() { 8111cb0ef41Sopenharmony_ci assert( 8121cb0ef41Sopenharmony_ci (Buffer.from('test') instanceof Error) 8131cb0ef41Sopenharmony_ci ); 8141cb0ef41Sopenharmony_ci } 8151cb0ef41Sopenharmony_ci assert.throws( 8161cb0ef41Sopenharmony_ci () => throwErr(), 8171cb0ef41Sopenharmony_ci { 8181cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8191cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8201cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 8211cb0ef41Sopenharmony_ci "assert(\n (Buffer.from('test') instanceof Error)\n )\n" 8221cb0ef41Sopenharmony_ci } 8231cb0ef41Sopenharmony_ci ); 8241cb0ef41Sopenharmony_ci assert.throws( 8251cb0ef41Sopenharmony_ci () => throwErr(), 8261cb0ef41Sopenharmony_ci { 8271cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8281cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8291cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 8301cb0ef41Sopenharmony_ci "assert(\n (Buffer.from('test') instanceof Error)\n )\n" 8311cb0ef41Sopenharmony_ci } 8321cb0ef41Sopenharmony_ci ); 8331cb0ef41Sopenharmony_ci fs.close = tmp; 8341cb0ef41Sopenharmony_ci} 8351cb0ef41Sopenharmony_ci 8361cb0ef41Sopenharmony_ciassert.throws( 8371cb0ef41Sopenharmony_ci () => { 8381cb0ef41Sopenharmony_ci a( 8391cb0ef41Sopenharmony_ci (() => 'string')() 8401cb0ef41Sopenharmony_ci // eslint-disable-next-line operator-linebreak 8411cb0ef41Sopenharmony_ci === 8421cb0ef41Sopenharmony_ci 123 instanceof 8431cb0ef41Sopenharmony_ci Buffer 8441cb0ef41Sopenharmony_ci ); 8451cb0ef41Sopenharmony_ci }, 8461cb0ef41Sopenharmony_ci { 8471cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8481cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8491cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n' + 8501cb0ef41Sopenharmony_ci ' a(\n' + 8511cb0ef41Sopenharmony_ci ' (() => \'string\')()\n' + 8521cb0ef41Sopenharmony_ci ' // eslint-disable-next-line operator-linebreak\n' + 8531cb0ef41Sopenharmony_ci ' ===\n' + 8541cb0ef41Sopenharmony_ci ' 123 instanceof\n' + 8551cb0ef41Sopenharmony_ci ' Buffer\n' + 8561cb0ef41Sopenharmony_ci ' )\n' 8571cb0ef41Sopenharmony_ci } 8581cb0ef41Sopenharmony_ci); 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ciassert.throws( 8611cb0ef41Sopenharmony_ci () => { 8621cb0ef41Sopenharmony_ci a( 8631cb0ef41Sopenharmony_ci (() => 'string')() 8641cb0ef41Sopenharmony_ci // eslint-disable-next-line operator-linebreak 8651cb0ef41Sopenharmony_ci === 8661cb0ef41Sopenharmony_ci 123 instanceof 8671cb0ef41Sopenharmony_ci Buffer 8681cb0ef41Sopenharmony_ci ); 8691cb0ef41Sopenharmony_ci }, 8701cb0ef41Sopenharmony_ci { 8711cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8721cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8731cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n' + 8741cb0ef41Sopenharmony_ci ' a(\n' + 8751cb0ef41Sopenharmony_ci ' (() => \'string\')()\n' + 8761cb0ef41Sopenharmony_ci ' // eslint-disable-next-line operator-linebreak\n' + 8771cb0ef41Sopenharmony_ci ' ===\n' + 8781cb0ef41Sopenharmony_ci ' 123 instanceof\n' + 8791cb0ef41Sopenharmony_ci ' Buffer\n' + 8801cb0ef41Sopenharmony_ci ' )\n' 8811cb0ef41Sopenharmony_ci } 8821cb0ef41Sopenharmony_ci); 8831cb0ef41Sopenharmony_ci 8841cb0ef41Sopenharmony_ci/* eslint-disable indent */ 8851cb0ef41Sopenharmony_ciassert.throws(() => { 8861cb0ef41Sopenharmony_cia(( 8871cb0ef41Sopenharmony_ci () => 'string')() === 8881cb0ef41Sopenharmony_ci123 instanceof 8891cb0ef41Sopenharmony_ciBuffer 8901cb0ef41Sopenharmony_ci); 8911cb0ef41Sopenharmony_ci}, { 8921cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 8931cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 8941cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n' + 8951cb0ef41Sopenharmony_ci ' a((\n' + 8961cb0ef41Sopenharmony_ci ' () => \'string\')() ===\n' + 8971cb0ef41Sopenharmony_ci ' 123 instanceof\n' + 8981cb0ef41Sopenharmony_ci ' Buffer\n' + 8991cb0ef41Sopenharmony_ci ' )\n' 9001cb0ef41Sopenharmony_ci } 9011cb0ef41Sopenharmony_ci); 9021cb0ef41Sopenharmony_ci/* eslint-enable indent */ 9031cb0ef41Sopenharmony_ci 9041cb0ef41Sopenharmony_ciassert.throws( 9051cb0ef41Sopenharmony_ci () => { 9061cb0ef41Sopenharmony_ci assert(true); assert(null, undefined); 9071cb0ef41Sopenharmony_ci }, 9081cb0ef41Sopenharmony_ci { 9091cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9101cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9111cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 9121cb0ef41Sopenharmony_ci 'assert(null, undefined)\n' 9131cb0ef41Sopenharmony_ci } 9141cb0ef41Sopenharmony_ci); 9151cb0ef41Sopenharmony_ci 9161cb0ef41Sopenharmony_ciassert.throws( 9171cb0ef41Sopenharmony_ci () => { 9181cb0ef41Sopenharmony_ci assert 9191cb0ef41Sopenharmony_ci .ok(null, undefined); 9201cb0ef41Sopenharmony_ci }, 9211cb0ef41Sopenharmony_ci { 9221cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9231cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9241cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 9251cb0ef41Sopenharmony_ci 'ok(null, undefined)\n' 9261cb0ef41Sopenharmony_ci } 9271cb0ef41Sopenharmony_ci); 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ciassert.throws( 9301cb0ef41Sopenharmony_ci // eslint-disable-next-line dot-notation, quotes 9311cb0ef41Sopenharmony_ci () => assert['ok']["apply"](null, [0]), 9321cb0ef41Sopenharmony_ci { 9331cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9341cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9351cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 9361cb0ef41Sopenharmony_ci 'assert[\'ok\']["apply"](null, [0])\n' 9371cb0ef41Sopenharmony_ci } 9381cb0ef41Sopenharmony_ci); 9391cb0ef41Sopenharmony_ci 9401cb0ef41Sopenharmony_ciassert.throws( 9411cb0ef41Sopenharmony_ci () => { 9421cb0ef41Sopenharmony_ci const wrapper = (fn, value) => fn(value); 9431cb0ef41Sopenharmony_ci wrapper(assert, false); 9441cb0ef41Sopenharmony_ci }, 9451cb0ef41Sopenharmony_ci { 9461cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9471cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9481cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n fn(value)\n' 9491cb0ef41Sopenharmony_ci } 9501cb0ef41Sopenharmony_ci); 9511cb0ef41Sopenharmony_ci 9521cb0ef41Sopenharmony_ciassert.throws( 9531cb0ef41Sopenharmony_ci () => assert.ok.call(null, 0), 9541cb0ef41Sopenharmony_ci { 9551cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9561cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9571cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n ' + 9581cb0ef41Sopenharmony_ci 'assert.ok.call(null, 0)\n', 9591cb0ef41Sopenharmony_ci generatedMessage: true 9601cb0ef41Sopenharmony_ci } 9611cb0ef41Sopenharmony_ci); 9621cb0ef41Sopenharmony_ci 9631cb0ef41Sopenharmony_ciassert.throws( 9641cb0ef41Sopenharmony_ci () => assert.ok.call(null, 0, 'test'), 9651cb0ef41Sopenharmony_ci { 9661cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9671cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9681cb0ef41Sopenharmony_ci message: 'test', 9691cb0ef41Sopenharmony_ci generatedMessage: false 9701cb0ef41Sopenharmony_ci } 9711cb0ef41Sopenharmony_ci); 9721cb0ef41Sopenharmony_ci 9731cb0ef41Sopenharmony_ci// Works in eval. 9741cb0ef41Sopenharmony_ciassert.throws( 9751cb0ef41Sopenharmony_ci () => new Function('assert', 'assert(1 === 2);')(assert), 9761cb0ef41Sopenharmony_ci { 9771cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9781cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 9791cb0ef41Sopenharmony_ci message: 'false == true' 9801cb0ef41Sopenharmony_ci } 9811cb0ef41Sopenharmony_ci); 9821cb0ef41Sopenharmony_ciassert.throws( 9831cb0ef41Sopenharmony_ci () => eval('console.log("FOO");\nassert.ok(1 === 2);'), 9841cb0ef41Sopenharmony_ci { 9851cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 9861cb0ef41Sopenharmony_ci message: 'false == true' 9871cb0ef41Sopenharmony_ci } 9881cb0ef41Sopenharmony_ci); 9891cb0ef41Sopenharmony_ci 9901cb0ef41Sopenharmony_ciassert.throws( 9911cb0ef41Sopenharmony_ci () => assert.throws(() => {}, 'Error message', 'message'), 9921cb0ef41Sopenharmony_ci { 9931cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 9941cb0ef41Sopenharmony_ci name: 'TypeError', 9951cb0ef41Sopenharmony_ci message: 'The "error" argument must be of type function or ' + 9961cb0ef41Sopenharmony_ci 'an instance of Error, RegExp, or Object. Received type string ' + 9971cb0ef41Sopenharmony_ci "('Error message')" 9981cb0ef41Sopenharmony_ci } 9991cb0ef41Sopenharmony_ci); 10001cb0ef41Sopenharmony_ci 10011cb0ef41Sopenharmony_ci[ 10021cb0ef41Sopenharmony_ci 1, 10031cb0ef41Sopenharmony_ci false, 10041cb0ef41Sopenharmony_ci Symbol(), 10051cb0ef41Sopenharmony_ci].forEach((input) => { 10061cb0ef41Sopenharmony_ci assert.throws( 10071cb0ef41Sopenharmony_ci () => assert.throws(() => {}, input), 10081cb0ef41Sopenharmony_ci { 10091cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 10101cb0ef41Sopenharmony_ci message: 'The "error" argument must be of type function or ' + 10111cb0ef41Sopenharmony_ci 'an instance of Error, RegExp, or Object.' + 10121cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(input) 10131cb0ef41Sopenharmony_ci } 10141cb0ef41Sopenharmony_ci ); 10151cb0ef41Sopenharmony_ci}); 10161cb0ef41Sopenharmony_ci 10171cb0ef41Sopenharmony_ci{ 10181cb0ef41Sopenharmony_ci 10191cb0ef41Sopenharmony_ci assert.throws(() => { 10201cb0ef41Sopenharmony_ci assert.ok((() => Boolean('' === false))()); 10211cb0ef41Sopenharmony_ci }, { 10221cb0ef41Sopenharmony_ci message: 'The expression evaluated to a falsy value:\n\n' + 10231cb0ef41Sopenharmony_ci " assert.ok((() => Boolean('\\u0001' === false))())\n" 10241cb0ef41Sopenharmony_ci }); 10251cb0ef41Sopenharmony_ci 10261cb0ef41Sopenharmony_ci const errFn = () => { 10271cb0ef41Sopenharmony_ci const err = new TypeError('Wrong value'); 10281cb0ef41Sopenharmony_ci err.code = 404; 10291cb0ef41Sopenharmony_ci throw err; 10301cb0ef41Sopenharmony_ci }; 10311cb0ef41Sopenharmony_ci const errObj = { 10321cb0ef41Sopenharmony_ci name: 'TypeError', 10331cb0ef41Sopenharmony_ci message: 'Wrong value' 10341cb0ef41Sopenharmony_ci }; 10351cb0ef41Sopenharmony_ci assert.throws(errFn, errObj); 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci errObj.code = 404; 10381cb0ef41Sopenharmony_ci assert.throws(errFn, errObj); 10391cb0ef41Sopenharmony_ci 10401cb0ef41Sopenharmony_ci // Fail in case a expected property is undefined and not existent on the 10411cb0ef41Sopenharmony_ci // error. 10421cb0ef41Sopenharmony_ci errObj.foo = undefined; 10431cb0ef41Sopenharmony_ci assert.throws( 10441cb0ef41Sopenharmony_ci () => assert.throws(errFn, errObj), 10451cb0ef41Sopenharmony_ci { 10461cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 10471cb0ef41Sopenharmony_ci name: 'AssertionError', 10481cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 10491cb0ef41Sopenharmony_ci ' Comparison {\n' + 10501cb0ef41Sopenharmony_ci ' code: 404,\n' + 10511cb0ef41Sopenharmony_ci '- foo: undefined,\n' + 10521cb0ef41Sopenharmony_ci " message: 'Wrong value',\n" + 10531cb0ef41Sopenharmony_ci " name: 'TypeError'\n" + 10541cb0ef41Sopenharmony_ci ' }' 10551cb0ef41Sopenharmony_ci } 10561cb0ef41Sopenharmony_ci ); 10571cb0ef41Sopenharmony_ci 10581cb0ef41Sopenharmony_ci // Show multiple wrong properties at the same time. 10591cb0ef41Sopenharmony_ci errObj.code = '404'; 10601cb0ef41Sopenharmony_ci assert.throws( 10611cb0ef41Sopenharmony_ci () => assert.throws(errFn, errObj), 10621cb0ef41Sopenharmony_ci { 10631cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 10641cb0ef41Sopenharmony_ci name: 'AssertionError', 10651cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 10661cb0ef41Sopenharmony_ci ' Comparison {\n' + 10671cb0ef41Sopenharmony_ci '+ code: 404,\n' + 10681cb0ef41Sopenharmony_ci "- code: '404',\n" + 10691cb0ef41Sopenharmony_ci '- foo: undefined,\n' + 10701cb0ef41Sopenharmony_ci " message: 'Wrong value',\n" + 10711cb0ef41Sopenharmony_ci " name: 'TypeError'\n" + 10721cb0ef41Sopenharmony_ci ' }' 10731cb0ef41Sopenharmony_ci } 10741cb0ef41Sopenharmony_ci ); 10751cb0ef41Sopenharmony_ci 10761cb0ef41Sopenharmony_ci assert.throws( 10771cb0ef41Sopenharmony_ci () => assert.throws(() => { throw new Error(); }, { foo: 'bar' }, 'foobar'), 10781cb0ef41Sopenharmony_ci { 10791cb0ef41Sopenharmony_ci constructor: assert.AssertionError, 10801cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 10811cb0ef41Sopenharmony_ci message: 'foobar' 10821cb0ef41Sopenharmony_ci } 10831cb0ef41Sopenharmony_ci ); 10841cb0ef41Sopenharmony_ci 10851cb0ef41Sopenharmony_ci assert.throws( 10861cb0ef41Sopenharmony_ci () => a.doesNotThrow(() => { throw new Error(); }, { foo: 'bar' }), 10871cb0ef41Sopenharmony_ci { 10881cb0ef41Sopenharmony_ci name: 'TypeError', 10891cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 10901cb0ef41Sopenharmony_ci message: 'The "expected" argument must be of type function or an ' + 10911cb0ef41Sopenharmony_ci 'instance of RegExp. Received an instance of Object' 10921cb0ef41Sopenharmony_ci } 10931cb0ef41Sopenharmony_ci ); 10941cb0ef41Sopenharmony_ci 10951cb0ef41Sopenharmony_ci assert.throws(() => { throw new Error('e'); }, new Error('e')); 10961cb0ef41Sopenharmony_ci assert.throws( 10971cb0ef41Sopenharmony_ci () => assert.throws(() => { throw new TypeError('e'); }, new Error('e')), 10981cb0ef41Sopenharmony_ci { 10991cb0ef41Sopenharmony_ci name: 'AssertionError', 11001cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 11011cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 11021cb0ef41Sopenharmony_ci ' Comparison {\n' + 11031cb0ef41Sopenharmony_ci " message: 'e',\n" + 11041cb0ef41Sopenharmony_ci "+ name: 'TypeError'\n" + 11051cb0ef41Sopenharmony_ci "- name: 'Error'\n" + 11061cb0ef41Sopenharmony_ci ' }' 11071cb0ef41Sopenharmony_ci } 11081cb0ef41Sopenharmony_ci ); 11091cb0ef41Sopenharmony_ci assert.throws( 11101cb0ef41Sopenharmony_ci () => assert.throws(() => { throw new Error('foo'); }, new Error('')), 11111cb0ef41Sopenharmony_ci { 11121cb0ef41Sopenharmony_ci name: 'AssertionError', 11131cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 11141cb0ef41Sopenharmony_ci generatedMessage: true, 11151cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 11161cb0ef41Sopenharmony_ci ' Comparison {\n' + 11171cb0ef41Sopenharmony_ci "+ message: 'foo',\n" + 11181cb0ef41Sopenharmony_ci "- message: '',\n" + 11191cb0ef41Sopenharmony_ci " name: 'Error'\n" + 11201cb0ef41Sopenharmony_ci ' }' 11211cb0ef41Sopenharmony_ci } 11221cb0ef41Sopenharmony_ci ); 11231cb0ef41Sopenharmony_ci 11241cb0ef41Sopenharmony_ci // eslint-disable-next-line no-throw-literal 11251cb0ef41Sopenharmony_ci assert.throws(() => { throw undefined; }, /undefined/); 11261cb0ef41Sopenharmony_ci assert.throws( 11271cb0ef41Sopenharmony_ci // eslint-disable-next-line no-throw-literal 11281cb0ef41Sopenharmony_ci () => a.doesNotThrow(() => { throw undefined; }), 11291cb0ef41Sopenharmony_ci { 11301cb0ef41Sopenharmony_ci name: 'AssertionError', 11311cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 11321cb0ef41Sopenharmony_ci message: 'Got unwanted exception.\nActual message: "undefined"' 11331cb0ef41Sopenharmony_ci } 11341cb0ef41Sopenharmony_ci ); 11351cb0ef41Sopenharmony_ci} 11361cb0ef41Sopenharmony_ci 11371cb0ef41Sopenharmony_ciassert.throws( 11381cb0ef41Sopenharmony_ci () => assert.throws(() => { throw new Error(); }, {}), 11391cb0ef41Sopenharmony_ci { 11401cb0ef41Sopenharmony_ci message: "The argument 'error' may not be an empty object. Received {}", 11411cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE' 11421cb0ef41Sopenharmony_ci } 11431cb0ef41Sopenharmony_ci); 11441cb0ef41Sopenharmony_ci 11451cb0ef41Sopenharmony_ciassert.throws( 11461cb0ef41Sopenharmony_ci () => a.throws( 11471cb0ef41Sopenharmony_ci // eslint-disable-next-line no-throw-literal 11481cb0ef41Sopenharmony_ci () => { throw 'foo'; }, 11491cb0ef41Sopenharmony_ci 'foo' 11501cb0ef41Sopenharmony_ci ), 11511cb0ef41Sopenharmony_ci { 11521cb0ef41Sopenharmony_ci code: 'ERR_AMBIGUOUS_ARGUMENT', 11531cb0ef41Sopenharmony_ci message: 'The "error/message" argument is ambiguous. ' + 11541cb0ef41Sopenharmony_ci 'The error "foo" is identical to the message.' 11551cb0ef41Sopenharmony_ci } 11561cb0ef41Sopenharmony_ci); 11571cb0ef41Sopenharmony_ci 11581cb0ef41Sopenharmony_ciassert.throws( 11591cb0ef41Sopenharmony_ci () => a.throws( 11601cb0ef41Sopenharmony_ci () => { throw new TypeError('foo'); }, 11611cb0ef41Sopenharmony_ci 'foo' 11621cb0ef41Sopenharmony_ci ), 11631cb0ef41Sopenharmony_ci { 11641cb0ef41Sopenharmony_ci code: 'ERR_AMBIGUOUS_ARGUMENT', 11651cb0ef41Sopenharmony_ci message: 'The "error/message" argument is ambiguous. ' + 11661cb0ef41Sopenharmony_ci 'The error message "foo" is identical to the message.' 11671cb0ef41Sopenharmony_ci } 11681cb0ef41Sopenharmony_ci); 11691cb0ef41Sopenharmony_ci/* eslint-enable no-restricted-syntax */ 11701cb0ef41Sopenharmony_ci 11711cb0ef41Sopenharmony_ci// Should not throw. 11721cb0ef41Sopenharmony_ci// eslint-disable-next-line no-restricted-syntax, no-throw-literal 11731cb0ef41Sopenharmony_ciassert.throws(() => { throw null; }, 'foo'); 11741cb0ef41Sopenharmony_ci 11751cb0ef41Sopenharmony_ciassert.throws( 11761cb0ef41Sopenharmony_ci () => assert.strictEqual([], []), 11771cb0ef41Sopenharmony_ci { 11781cb0ef41Sopenharmony_ci message: 'Values have same structure but are not reference-equal:\n\n[]\n' 11791cb0ef41Sopenharmony_ci } 11801cb0ef41Sopenharmony_ci); 11811cb0ef41Sopenharmony_ci 11821cb0ef41Sopenharmony_ci{ 11831cb0ef41Sopenharmony_ci const args = (function() { return arguments; })('a'); 11841cb0ef41Sopenharmony_ci assert.throws( 11851cb0ef41Sopenharmony_ci () => assert.strictEqual(args, { 0: 'a' }), 11861cb0ef41Sopenharmony_ci { 11871cb0ef41Sopenharmony_ci message: 'Expected "actual" to be reference-equal to "expected":\n' + 11881cb0ef41Sopenharmony_ci '+ actual - expected\n\n' + 11891cb0ef41Sopenharmony_ci "+ [Arguments] {\n- {\n '0': 'a'\n }" 11901cb0ef41Sopenharmony_ci } 11911cb0ef41Sopenharmony_ci ); 11921cb0ef41Sopenharmony_ci} 11931cb0ef41Sopenharmony_ci 11941cb0ef41Sopenharmony_ciassert.throws( 11951cb0ef41Sopenharmony_ci () => { throw new TypeError('foobar'); }, 11961cb0ef41Sopenharmony_ci { 11971cb0ef41Sopenharmony_ci message: /foo/, 11981cb0ef41Sopenharmony_ci name: /^TypeError$/ 11991cb0ef41Sopenharmony_ci } 12001cb0ef41Sopenharmony_ci); 12011cb0ef41Sopenharmony_ci 12021cb0ef41Sopenharmony_ciassert.throws( 12031cb0ef41Sopenharmony_ci () => assert.throws( 12041cb0ef41Sopenharmony_ci () => { throw new TypeError('foobar'); }, 12051cb0ef41Sopenharmony_ci { 12061cb0ef41Sopenharmony_ci message: /fooa/, 12071cb0ef41Sopenharmony_ci name: /^TypeError$/ 12081cb0ef41Sopenharmony_ci } 12091cb0ef41Sopenharmony_ci ), 12101cb0ef41Sopenharmony_ci { 12111cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 12121cb0ef41Sopenharmony_ci ' Comparison {\n' + 12131cb0ef41Sopenharmony_ci "+ message: 'foobar',\n" + 12141cb0ef41Sopenharmony_ci '- message: /fooa/,\n' + 12151cb0ef41Sopenharmony_ci " name: 'TypeError'\n" + 12161cb0ef41Sopenharmony_ci ' }' 12171cb0ef41Sopenharmony_ci } 12181cb0ef41Sopenharmony_ci); 12191cb0ef41Sopenharmony_ci 12201cb0ef41Sopenharmony_ci{ 12211cb0ef41Sopenharmony_ci let actual = null; 12221cb0ef41Sopenharmony_ci const expected = { message: 'foo' }; 12231cb0ef41Sopenharmony_ci assert.throws( 12241cb0ef41Sopenharmony_ci () => assert.throws( 12251cb0ef41Sopenharmony_ci () => { throw actual; }, 12261cb0ef41Sopenharmony_ci expected 12271cb0ef41Sopenharmony_ci ), 12281cb0ef41Sopenharmony_ci { 12291cb0ef41Sopenharmony_ci operator: 'throws', 12301cb0ef41Sopenharmony_ci actual, 12311cb0ef41Sopenharmony_ci expected, 12321cb0ef41Sopenharmony_ci generatedMessage: true, 12331cb0ef41Sopenharmony_ci message: `${start}\n${actExp}\n\n` + 12341cb0ef41Sopenharmony_ci '+ null\n' + 12351cb0ef41Sopenharmony_ci '- {\n' + 12361cb0ef41Sopenharmony_ci "- message: 'foo'\n" + 12371cb0ef41Sopenharmony_ci '- }' 12381cb0ef41Sopenharmony_ci } 12391cb0ef41Sopenharmony_ci ); 12401cb0ef41Sopenharmony_ci 12411cb0ef41Sopenharmony_ci actual = 'foobar'; 12421cb0ef41Sopenharmony_ci const message = 'message'; 12431cb0ef41Sopenharmony_ci assert.throws( 12441cb0ef41Sopenharmony_ci () => assert.throws( 12451cb0ef41Sopenharmony_ci () => { throw actual; }, 12461cb0ef41Sopenharmony_ci { message: 'foobar' }, 12471cb0ef41Sopenharmony_ci message 12481cb0ef41Sopenharmony_ci ), 12491cb0ef41Sopenharmony_ci { 12501cb0ef41Sopenharmony_ci actual, 12511cb0ef41Sopenharmony_ci message, 12521cb0ef41Sopenharmony_ci operator: 'throws', 12531cb0ef41Sopenharmony_ci generatedMessage: false 12541cb0ef41Sopenharmony_ci } 12551cb0ef41Sopenharmony_ci ); 12561cb0ef41Sopenharmony_ci} 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_ci// Indicate where the strings diverge. 12591cb0ef41Sopenharmony_ciassert.throws( 12601cb0ef41Sopenharmony_ci () => assert.strictEqual('test test', 'test foobar'), 12611cb0ef41Sopenharmony_ci { 12621cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 12631cb0ef41Sopenharmony_ci name: 'AssertionError', 12641cb0ef41Sopenharmony_ci message: strictEqualMessageStart + 12651cb0ef41Sopenharmony_ci '+ actual - expected\n\n' + 12661cb0ef41Sopenharmony_ci "+ 'test test'\n" + 12671cb0ef41Sopenharmony_ci "- 'test foobar'\n" + 12681cb0ef41Sopenharmony_ci ' ^' 12691cb0ef41Sopenharmony_ci } 12701cb0ef41Sopenharmony_ci); 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_ci// Check for reference-equal objects in `notStrictEqual()` 12731cb0ef41Sopenharmony_ciassert.throws( 12741cb0ef41Sopenharmony_ci () => { 12751cb0ef41Sopenharmony_ci const obj = {}; 12761cb0ef41Sopenharmony_ci assert.notStrictEqual(obj, obj); 12771cb0ef41Sopenharmony_ci }, 12781cb0ef41Sopenharmony_ci { 12791cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 12801cb0ef41Sopenharmony_ci name: 'AssertionError', 12811cb0ef41Sopenharmony_ci message: 'Expected "actual" not to be reference-equal to "expected": {}' 12821cb0ef41Sopenharmony_ci } 12831cb0ef41Sopenharmony_ci); 12841cb0ef41Sopenharmony_ci 12851cb0ef41Sopenharmony_ciassert.throws( 12861cb0ef41Sopenharmony_ci () => { 12871cb0ef41Sopenharmony_ci const obj = { a: true }; 12881cb0ef41Sopenharmony_ci assert.notStrictEqual(obj, obj); 12891cb0ef41Sopenharmony_ci }, 12901cb0ef41Sopenharmony_ci { 12911cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 12921cb0ef41Sopenharmony_ci name: 'AssertionError', 12931cb0ef41Sopenharmony_ci message: 'Expected "actual" not to be reference-equal to "expected":\n\n' + 12941cb0ef41Sopenharmony_ci '{\n a: true\n}\n' 12951cb0ef41Sopenharmony_ci } 12961cb0ef41Sopenharmony_ci); 12971cb0ef41Sopenharmony_ci 12981cb0ef41Sopenharmony_ci{ 12991cb0ef41Sopenharmony_ci let threw = false; 13001cb0ef41Sopenharmony_ci try { 13011cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 13021cb0ef41Sopenharmony_ci assert.deepStrictEqual(Array(100).fill(1), 'foobar'); 13031cb0ef41Sopenharmony_ci } catch (err) { 13041cb0ef41Sopenharmony_ci threw = true; 13051cb0ef41Sopenharmony_ci assert.match(inspect(err), /actual: \[Array],\n {2}expected: 'foobar',/); 13061cb0ef41Sopenharmony_ci } 13071cb0ef41Sopenharmony_ci assert(threw); 13081cb0ef41Sopenharmony_ci} 13091cb0ef41Sopenharmony_ci 13101cb0ef41Sopenharmony_ciassert.throws( 13111cb0ef41Sopenharmony_ci () => a.equal(1), 13121cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13131cb0ef41Sopenharmony_ci); 13141cb0ef41Sopenharmony_ci 13151cb0ef41Sopenharmony_ciassert.throws( 13161cb0ef41Sopenharmony_ci () => a.deepEqual(/a/), 13171cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13181cb0ef41Sopenharmony_ci); 13191cb0ef41Sopenharmony_ci 13201cb0ef41Sopenharmony_ciassert.throws( 13211cb0ef41Sopenharmony_ci () => a.notEqual(null), 13221cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13231cb0ef41Sopenharmony_ci); 13241cb0ef41Sopenharmony_ci 13251cb0ef41Sopenharmony_ciassert.throws( 13261cb0ef41Sopenharmony_ci () => a.notDeepEqual('test'), 13271cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13281cb0ef41Sopenharmony_ci); 13291cb0ef41Sopenharmony_ci 13301cb0ef41Sopenharmony_ciassert.throws( 13311cb0ef41Sopenharmony_ci () => a.strictEqual({}), 13321cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13331cb0ef41Sopenharmony_ci); 13341cb0ef41Sopenharmony_ci 13351cb0ef41Sopenharmony_ciassert.throws( 13361cb0ef41Sopenharmony_ci () => a.deepStrictEqual(Symbol()), 13371cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13381cb0ef41Sopenharmony_ci); 13391cb0ef41Sopenharmony_ci 13401cb0ef41Sopenharmony_ciassert.throws( 13411cb0ef41Sopenharmony_ci () => a.notStrictEqual(5n), // eslint-disable-line no-restricted-syntax 13421cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13431cb0ef41Sopenharmony_ci); 13441cb0ef41Sopenharmony_ci 13451cb0ef41Sopenharmony_ciassert.throws( 13461cb0ef41Sopenharmony_ci () => a.notDeepStrictEqual(undefined), 13471cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13481cb0ef41Sopenharmony_ci); 13491cb0ef41Sopenharmony_ci 13501cb0ef41Sopenharmony_ciassert.throws( 13511cb0ef41Sopenharmony_ci () => a.strictEqual(), 13521cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13531cb0ef41Sopenharmony_ci); 13541cb0ef41Sopenharmony_ci 13551cb0ef41Sopenharmony_ciassert.throws( 13561cb0ef41Sopenharmony_ci () => a.deepStrictEqual(), 13571cb0ef41Sopenharmony_ci { code: 'ERR_MISSING_ARGS' } 13581cb0ef41Sopenharmony_ci); 13591cb0ef41Sopenharmony_ci 13601cb0ef41Sopenharmony_ci// Verify that `stackStartFunction` works as alternative to `stackStartFn`. 13611cb0ef41Sopenharmony_ci{ 13621cb0ef41Sopenharmony_ci (function hidden() { 13631cb0ef41Sopenharmony_ci const err = new assert.AssertionError({ 13641cb0ef41Sopenharmony_ci actual: 'foo', 13651cb0ef41Sopenharmony_ci operator: 'strictEqual', 13661cb0ef41Sopenharmony_ci stackStartFunction: hidden 13671cb0ef41Sopenharmony_ci }); 13681cb0ef41Sopenharmony_ci const err2 = new assert.AssertionError({ 13691cb0ef41Sopenharmony_ci actual: 'foo', 13701cb0ef41Sopenharmony_ci operator: 'strictEqual', 13711cb0ef41Sopenharmony_ci stackStartFn: hidden 13721cb0ef41Sopenharmony_ci }); 13731cb0ef41Sopenharmony_ci assert(!err.stack.includes('hidden')); 13741cb0ef41Sopenharmony_ci assert(!err2.stack.includes('hidden')); 13751cb0ef41Sopenharmony_ci })(); 13761cb0ef41Sopenharmony_ci} 13771cb0ef41Sopenharmony_ci 13781cb0ef41Sopenharmony_ciassert.throws( 13791cb0ef41Sopenharmony_ci () => assert.throws(() => { throw Symbol('foo'); }, RangeError), 13801cb0ef41Sopenharmony_ci { 13811cb0ef41Sopenharmony_ci message: 'The error is expected to be an instance of "RangeError". ' + 13821cb0ef41Sopenharmony_ci 'Received "Symbol(foo)"' 13831cb0ef41Sopenharmony_ci } 13841cb0ef41Sopenharmony_ci); 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ciassert.throws( 13871cb0ef41Sopenharmony_ci // eslint-disable-next-line no-throw-literal 13881cb0ef41Sopenharmony_ci () => assert.throws(() => { throw [1, 2]; }, RangeError), 13891cb0ef41Sopenharmony_ci { 13901cb0ef41Sopenharmony_ci message: 'The error is expected to be an instance of "RangeError". ' + 13911cb0ef41Sopenharmony_ci 'Received "[Array]"' 13921cb0ef41Sopenharmony_ci } 13931cb0ef41Sopenharmony_ci); 13941cb0ef41Sopenharmony_ci 13951cb0ef41Sopenharmony_ci{ 13961cb0ef41Sopenharmony_ci const err = new TypeError('foo'); 13971cb0ef41Sopenharmony_ci const validate = (() => () => ({ a: true, b: [ 1, 2, 3 ] }))(); 13981cb0ef41Sopenharmony_ci assert.throws( 13991cb0ef41Sopenharmony_ci () => assert.throws(() => { throw err; }, validate), 14001cb0ef41Sopenharmony_ci { 14011cb0ef41Sopenharmony_ci message: 'The validation function is expected to ' + 14021cb0ef41Sopenharmony_ci `return "true". Received ${inspect(validate())}\n\nCaught ` + 14031cb0ef41Sopenharmony_ci `error:\n\n${err}`, 14041cb0ef41Sopenharmony_ci code: 'ERR_ASSERTION', 14051cb0ef41Sopenharmony_ci actual: err, 14061cb0ef41Sopenharmony_ci expected: validate, 14071cb0ef41Sopenharmony_ci name: 'AssertionError', 14081cb0ef41Sopenharmony_ci operator: 'throws', 14091cb0ef41Sopenharmony_ci } 14101cb0ef41Sopenharmony_ci ); 14111cb0ef41Sopenharmony_ci} 14121cb0ef41Sopenharmony_ci 14131cb0ef41Sopenharmony_ciassert.throws( 14141cb0ef41Sopenharmony_ci () => { 14151cb0ef41Sopenharmony_ci const script = new vm.Script('new RangeError("foobar");'); 14161cb0ef41Sopenharmony_ci const context = vm.createContext(); 14171cb0ef41Sopenharmony_ci const err = script.runInContext(context); 14181cb0ef41Sopenharmony_ci assert.throws(() => { throw err; }, RangeError); 14191cb0ef41Sopenharmony_ci }, 14201cb0ef41Sopenharmony_ci { 14211cb0ef41Sopenharmony_ci message: 'The error is expected to be an instance of "RangeError". ' + 14221cb0ef41Sopenharmony_ci 'Received an error with identical name but a different ' + 14231cb0ef41Sopenharmony_ci 'prototype.\n\nError message:\n\nfoobar' 14241cb0ef41Sopenharmony_ci } 14251cb0ef41Sopenharmony_ci); 14261cb0ef41Sopenharmony_ci 14271cb0ef41Sopenharmony_ci// Multiple assert.match() tests. 14281cb0ef41Sopenharmony_ci{ 14291cb0ef41Sopenharmony_ci assert.throws( 14301cb0ef41Sopenharmony_ci () => assert.match(/abc/, 'string'), 14311cb0ef41Sopenharmony_ci { 14321cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 14331cb0ef41Sopenharmony_ci message: 'The "regexp" argument must be an instance of RegExp. ' + 14341cb0ef41Sopenharmony_ci "Received type string ('string')" 14351cb0ef41Sopenharmony_ci } 14361cb0ef41Sopenharmony_ci ); 14371cb0ef41Sopenharmony_ci assert.throws( 14381cb0ef41Sopenharmony_ci () => assert.match('string', /abc/), 14391cb0ef41Sopenharmony_ci { 14401cb0ef41Sopenharmony_ci actual: 'string', 14411cb0ef41Sopenharmony_ci expected: /abc/, 14421cb0ef41Sopenharmony_ci operator: 'match', 14431cb0ef41Sopenharmony_ci message: 'The input did not match the regular expression /abc/. ' + 14441cb0ef41Sopenharmony_ci "Input:\n\n'string'\n", 14451cb0ef41Sopenharmony_ci generatedMessage: true 14461cb0ef41Sopenharmony_ci } 14471cb0ef41Sopenharmony_ci ); 14481cb0ef41Sopenharmony_ci assert.throws( 14491cb0ef41Sopenharmony_ci () => assert.match('string', /abc/, 'foobar'), 14501cb0ef41Sopenharmony_ci { 14511cb0ef41Sopenharmony_ci actual: 'string', 14521cb0ef41Sopenharmony_ci expected: /abc/, 14531cb0ef41Sopenharmony_ci operator: 'match', 14541cb0ef41Sopenharmony_ci message: 'foobar', 14551cb0ef41Sopenharmony_ci generatedMessage: false 14561cb0ef41Sopenharmony_ci } 14571cb0ef41Sopenharmony_ci ); 14581cb0ef41Sopenharmony_ci const errorMessage = new RangeError('foobar'); 14591cb0ef41Sopenharmony_ci assert.throws( 14601cb0ef41Sopenharmony_ci () => assert.match('string', /abc/, errorMessage), 14611cb0ef41Sopenharmony_ci errorMessage 14621cb0ef41Sopenharmony_ci ); 14631cb0ef41Sopenharmony_ci assert.throws( 14641cb0ef41Sopenharmony_ci () => assert.match({ abc: 123 }, /abc/), 14651cb0ef41Sopenharmony_ci { 14661cb0ef41Sopenharmony_ci actual: { abc: 123 }, 14671cb0ef41Sopenharmony_ci expected: /abc/, 14681cb0ef41Sopenharmony_ci operator: 'match', 14691cb0ef41Sopenharmony_ci message: 'The "string" argument must be of type string. ' + 14701cb0ef41Sopenharmony_ci 'Received type object ({ abc: 123 })', 14711cb0ef41Sopenharmony_ci generatedMessage: true 14721cb0ef41Sopenharmony_ci } 14731cb0ef41Sopenharmony_ci ); 14741cb0ef41Sopenharmony_ci assert.match('I will pass', /pass$/); 14751cb0ef41Sopenharmony_ci} 14761cb0ef41Sopenharmony_ci 14771cb0ef41Sopenharmony_ci// Multiple assert.doesNotMatch() tests. 14781cb0ef41Sopenharmony_ci{ 14791cb0ef41Sopenharmony_ci assert.throws( 14801cb0ef41Sopenharmony_ci () => assert.doesNotMatch(/abc/, 'string'), 14811cb0ef41Sopenharmony_ci { 14821cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 14831cb0ef41Sopenharmony_ci message: 'The "regexp" argument must be an instance of RegExp. ' + 14841cb0ef41Sopenharmony_ci "Received type string ('string')" 14851cb0ef41Sopenharmony_ci } 14861cb0ef41Sopenharmony_ci ); 14871cb0ef41Sopenharmony_ci assert.throws( 14881cb0ef41Sopenharmony_ci () => assert.doesNotMatch('string', /string/), 14891cb0ef41Sopenharmony_ci { 14901cb0ef41Sopenharmony_ci actual: 'string', 14911cb0ef41Sopenharmony_ci expected: /string/, 14921cb0ef41Sopenharmony_ci operator: 'doesNotMatch', 14931cb0ef41Sopenharmony_ci message: 'The input was expected to not match the regular expression ' + 14941cb0ef41Sopenharmony_ci "/string/. Input:\n\n'string'\n", 14951cb0ef41Sopenharmony_ci generatedMessage: true 14961cb0ef41Sopenharmony_ci } 14971cb0ef41Sopenharmony_ci ); 14981cb0ef41Sopenharmony_ci assert.throws( 14991cb0ef41Sopenharmony_ci () => assert.doesNotMatch('string', /string/, 'foobar'), 15001cb0ef41Sopenharmony_ci { 15011cb0ef41Sopenharmony_ci actual: 'string', 15021cb0ef41Sopenharmony_ci expected: /string/, 15031cb0ef41Sopenharmony_ci operator: 'doesNotMatch', 15041cb0ef41Sopenharmony_ci message: 'foobar', 15051cb0ef41Sopenharmony_ci generatedMessage: false 15061cb0ef41Sopenharmony_ci } 15071cb0ef41Sopenharmony_ci ); 15081cb0ef41Sopenharmony_ci const errorMessage = new RangeError('foobar'); 15091cb0ef41Sopenharmony_ci assert.throws( 15101cb0ef41Sopenharmony_ci () => assert.doesNotMatch('string', /string/, errorMessage), 15111cb0ef41Sopenharmony_ci errorMessage 15121cb0ef41Sopenharmony_ci ); 15131cb0ef41Sopenharmony_ci assert.throws( 15141cb0ef41Sopenharmony_ci () => assert.doesNotMatch({ abc: 123 }, /abc/), 15151cb0ef41Sopenharmony_ci { 15161cb0ef41Sopenharmony_ci actual: { abc: 123 }, 15171cb0ef41Sopenharmony_ci expected: /abc/, 15181cb0ef41Sopenharmony_ci operator: 'doesNotMatch', 15191cb0ef41Sopenharmony_ci message: 'The "string" argument must be of type string. ' + 15201cb0ef41Sopenharmony_ci 'Received type object ({ abc: 123 })', 15211cb0ef41Sopenharmony_ci generatedMessage: true 15221cb0ef41Sopenharmony_ci } 15231cb0ef41Sopenharmony_ci ); 15241cb0ef41Sopenharmony_ci assert.doesNotMatch('I will pass', /different$/); 15251cb0ef41Sopenharmony_ci} 15261cb0ef41Sopenharmony_ci 15271cb0ef41Sopenharmony_ci{ 15281cb0ef41Sopenharmony_ci const tempColor = inspect.defaultOptions.colors; 15291cb0ef41Sopenharmony_ci assert.throws(() => { 15301cb0ef41Sopenharmony_ci inspect.defaultOptions.colors = true; 15311cb0ef41Sopenharmony_ci // Guarantee the position indicator is placed correctly. 15321cb0ef41Sopenharmony_ci assert.strictEqual(111554n, 11111115); 15331cb0ef41Sopenharmony_ci }, (err) => { 15341cb0ef41Sopenharmony_ci assert.strictEqual(inspect(err).split('\n')[5], ' ^'); 15351cb0ef41Sopenharmony_ci inspect.defaultOptions.colors = tempColor; 15361cb0ef41Sopenharmony_ci return true; 15371cb0ef41Sopenharmony_ci }); 15381cb0ef41Sopenharmony_ci} 1539