node/test/parallel/test-process-emitwarning.js
Michaël Zasso 508890d795
test: use assert.match instead of regexp.test
PR-URL: https://github.com/nodejs/node/pull/39928
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
2021-08-31 18:50:16 +02:00

82 lines
2.1 KiB
JavaScript

// Flags: --no-warnings
// The flag suppresses stderr output but the warning event will still emit
'use strict';
const common = require('../common');
const assert = require('assert');
const testMsg = 'A Warning';
const testCode = 'CODE001';
const testDetail = 'Some detail';
const testType = 'CustomWarning';
process.on('warning', common.mustCall((warning) => {
assert(warning);
assert.match(warning.name, /^(?:Warning|CustomWarning)/);
assert.strictEqual(warning.message, testMsg);
if (warning.code) assert.strictEqual(warning.code, testCode);
if (warning.detail) assert.strictEqual(warning.detail, testDetail);
}, 15));
class CustomWarning extends Error {
constructor() {
super();
this.name = testType;
this.message = testMsg;
this.code = testCode;
Error.captureStackTrace(this, CustomWarning);
}
}
[
[testMsg],
[testMsg, testType],
[testMsg, CustomWarning],
[testMsg, testType, CustomWarning],
[testMsg, testType, testCode],
[testMsg, { type: testType }],
[testMsg, { type: testType, code: testCode }],
[testMsg, { type: testType, code: testCode, detail: testDetail }],
[new CustomWarning()],
// Detail will be ignored for the following. No errors thrown
[testMsg, { type: testType, code: testCode, detail: true }],
[testMsg, { type: testType, code: testCode, detail: [] }],
[testMsg, { type: testType, code: testCode, detail: null }],
[testMsg, { type: testType, code: testCode, detail: 1 }],
].forEach((args) => {
process.emitWarning(...args);
});
const warningNoToString = new CustomWarning();
warningNoToString.toString = null;
process.emitWarning(warningNoToString);
const warningThrowToString = new CustomWarning();
warningThrowToString.toString = function() {
throw new Error('invalid toString');
};
process.emitWarning(warningThrowToString);
// TypeError is thrown on invalid input
[
[1],
[{}],
[true],
[[]],
['', '', {}],
['', 1],
['', '', 1],
['', true],
['', '', true],
['', []],
['', '', []],
[],
[undefined, 'foo', 'bar'],
[undefined],
].forEach((args) => {
assert.throws(
() => process.emitWarning(...args),
{ code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' }
);
});