Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix t.assert methods #53049

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 27 additions & 10 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
MathMax,
Number,
ObjectDefineProperty,
ObjectEntries,
ObjectSeal,
PromisePrototypeThen,
PromiseResolve,
Expand Down Expand Up @@ -105,13 +104,31 @@ function lazyFindSourceMap(file) {

function lazyAssertObject() {
if (assertObj === undefined) {
assertObj = new SafeMap();
const assert = require('assert');
for (const { 0: key, 1: value } of ObjectEntries(assert)) {
if (typeof value === 'function') {
assertObj.set(value, key);
}
}

assertObj = new SafeMap([
// eslint-disable-next-line no-restricted-properties
['deepEqual', assert.deepEqual],
['deepStrictEqual', assert.deepStrictEqual],
['doesNotMatch', assert.doesNotMatch],
['doesNotReject', assert.doesNotReject],
['doesNotThrow', assert.doesNotThrow],
// eslint-disable-next-line no-restricted-properties
['equal', assert.equal],
['fail', assert.fail],
['ifError', assert.ifError],
['match', assert.match],
// eslint-disable-next-line no-restricted-properties
['notDeepEqual', assert.notDeepEqual],
['notDeepStrictEqual', assert.notDeepStrictEqual],
// eslint-disable-next-line no-restricted-properties
['notEqual', assert.notEqual],
['notStrictEqual', assert.notStrictEqual],
['ok', assert.ok],
['rejects', assert.rejects],
['strictEqual', assert.strictEqual],
['throws', assert.throws],
]);
}
return assertObj;
}
Expand Down Expand Up @@ -227,18 +244,18 @@ class TestContext {
get assert() {
if (this.#assert === undefined) {
const { plan } = this.#test;
const assertions = lazyAssertObject();
const map = lazyAssertObject();
const assert = { __proto__: null };

this.#assert = assert;
for (const { 0: method, 1: name } of assertions.entries()) {
map.forEach((method, name) => {
assert[name] = (...args) => {
if (plan !== null) {
plan.actual++;
}
return ReflectApply(method, assert, args);
};
}
});
}
return this.#assert;
}
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-runner-assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
require('../common');
const { deepStrictEqual } = require('node:assert');
const test = require('node:test');

test('only methods from node:assert are on t.assert', (t) => {
deepStrictEqual(Object.keys(t.assert).sort(), [
'deepEqual',
'deepStrictEqual',
'doesNotMatch',
'doesNotReject',
'doesNotThrow',
'equal',
'fail',
'ifError',
'match',
'notDeepEqual',
'notDeepStrictEqual',
'notEqual',
'notStrictEqual',
'ok',
'rejects',
'strictEqual',
'throws',
]);
});
Loading