mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 06:19:07 +00:00

This commit adds a top level assert.register() API to the test runner. This function allows users to define their own custom assertion functions on the TestContext. Fixes: https://github.com/nodejs/node/issues/52033 PR-URL: https://github.com/nodejs/node/pull/56434 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
51 lines
934 B
JavaScript
51 lines
934 B
JavaScript
'use strict';
|
|
const {
|
|
SafeMap,
|
|
} = primordials;
|
|
const {
|
|
validateFunction,
|
|
validateString,
|
|
} = require('internal/validators');
|
|
const assert = require('assert');
|
|
const methodsToCopy = [
|
|
'deepEqual',
|
|
'deepStrictEqual',
|
|
'doesNotMatch',
|
|
'doesNotReject',
|
|
'doesNotThrow',
|
|
'equal',
|
|
'fail',
|
|
'ifError',
|
|
'match',
|
|
'notDeepEqual',
|
|
'notDeepStrictEqual',
|
|
'notEqual',
|
|
'notStrictEqual',
|
|
'partialDeepStrictEqual',
|
|
'rejects',
|
|
'strictEqual',
|
|
'throws',
|
|
];
|
|
let assertMap;
|
|
|
|
function getAssertionMap() {
|
|
if (assertMap === undefined) {
|
|
assertMap = new SafeMap();
|
|
|
|
for (let i = 0; i < methodsToCopy.length; i++) {
|
|
assertMap.set(methodsToCopy[i], assert[methodsToCopy[i]]);
|
|
}
|
|
}
|
|
|
|
return assertMap;
|
|
}
|
|
|
|
function register(name, fn) {
|
|
validateString(name, 'name');
|
|
validateFunction(fn, 'fn');
|
|
const map = getAssertionMap();
|
|
map.set(name, fn);
|
|
}
|
|
|
|
module.exports = { getAssertionMap, register };
|