test_runner: add assert.register() API by cjihrig · Pull Request #56434 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/api/test.md
50 changes: 50 additions & 0 deletions lib/internal/test_runner/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'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 };
52 changes: 18 additions & 34 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,15 @@ function lazyFindSourceMap(file) {

function lazyAssertObject(harness) {
if (assertObj === undefined) {
assertObj = new SafeMap();
const assert = require('assert');
const { SnapshotManager } = require('internal/test_runner/snapshot');
const methodsToCopy = [
'deepEqual',
'deepStrictEqual',
'doesNotMatch',
'doesNotReject',
'doesNotThrow',
'equal',
'fail',
'ifError',
'match',
'notDeepEqual',
'notDeepStrictEqual',
'notEqual',
'notStrictEqual',
'partialDeepStrictEqual',
'rejects',
'strictEqual',
'throws',
];
for (let i = 0; i < methodsToCopy.length; i++) {
assertObj.set(methodsToCopy[i], assert[methodsToCopy[i]]);
}
const { getAssertionMap } = require('internal/test_runner/assert');

assertObj = getAssertionMap();
if (!assertObj.has('snapshot')) {
const { SnapshotManager } = require('internal/test_runner/snapshot');

harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
assertObj.set('snapshot', harness.snapshotManager.createAssert());
harness.snapshotManager = new SnapshotManager(harness.config.updateSnapshots);
assertObj.set('snapshot', harness.snapshotManager.createAssert());
}
}
return assertObj;
}
Expand Down Expand Up @@ -264,15 +245,18 @@ class TestContext {
};
});

// This is a hack. It allows the innerOk function to collect the stacktrace from the correct starting point.
function ok(...args) {
if (plan !== null) {
plan.actual++;
if (!map.has('ok')) {
// This is a hack. It allows the innerOk function to collect the
// stacktrace from the correct starting point.
function ok(...args) {
if (plan !== null) {
plan.actual++;
}
Comment thread
cjihrig marked this conversation as resolved.
innerOk(ok, args.length, ...args);
}
innerOk(ok, args.length, ...args);
}

assert.ok = ok;
assert.ok = ok;
}
}
return this.#assert;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ ObjectDefineProperty(module.exports, 'snapshot', {
return lazySnapshot;
},
});

ObjectDefineProperty(module.exports, 'assert', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
const { register } = require('internal/test_runner/assert');
const assert = { __proto__: null, register };
ObjectDefineProperty(module.exports, 'assert', assert);
return assert;
},
});
63 changes: 63 additions & 0 deletions test/parallel/test-runner-custom-assertions.js