Skip to content

Commit

Permalink
test: implemented testNamePattens param in run
Browse files Browse the repository at this point in the history
  • Loading branch information
italojs committed Feb 25, 2023
1 parent 65d2a42 commit 2287515
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
8 changes: 8 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,14 @@ message thrown by `block` because that usage suggests that the user believes
`message` is the expected message rather than the message the `AssertionError`
will display if `block` does not throw.

<a id="ERR_ARG_NOT_ALLOWED"></a>

### `ERR_ARG_NOT_ALLOWED`

A function argument that can't be accept depending of some logic restriction.
e.g. The function `run(...)` from module `node:test` can't accept `testNamePatterns`
param from programatic function and from CLI at same time. <a id="ERR_ARG_NOT_ITERABLE"></a>

<a id="ERR_ARG_NOT_ITERABLE"></a>

### `ERR_ARG_NOT_ITERABLE`
Expand Down
1 change: 1 addition & 0 deletions file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some content
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ module.exports = {
// Note: Node.js specific errors must begin with the prefix ERR_

E('ERR_AMBIGUOUS_ARGUMENT', 'The "%s" argument is ambiguous. %s', TypeError);
E('ERR_ARG_NOT_ALLOWED', '%s is already being assigned', TypeError);
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable', TypeError);
E('ERR_ASSERTION', '%s', Error);
E('ERR_ASYNC_CALLBACK', '%s must be a function', TypeError);
Expand Down
15 changes: 8 additions & 7 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ class FileTest extends Test {
const runningProcesses = new SafeMap();
const runningSubtests = new SafeMap();

function runTestFile(path, root, inspectPort, filesWatcher) {
const subtest = root.createSubtest(FileTest, path, async (t) => {
function runTestFile(options) {
const { path, root, inspectPort, filesWatcher, testNamePatterns } = options;
const subtest = root.createSubtest(FileTest, path, { testNamePatterns }, async (t) => {
const args = getRunArgs({ path, inspectPort });
const stdio = ['pipe', 'pipe', 'pipe'];
const env = { ...process.env };
Expand Down Expand Up @@ -328,7 +329,7 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
return subtest.start();
}

function watchFiles(testFiles, root, inspectPort) {
function watchFiles(testFiles, root, inspectPort, testNamePatterns) {
const filesWatcher = new FilesWatcher({ throttle: 500, mode: 'filter' });
filesWatcher.on('changed', ({ owners }) => {
filesWatcher.unfilterFilesOwnedBy(owners);
Expand All @@ -342,7 +343,7 @@ function watchFiles(testFiles, root, inspectPort) {
await once(runningProcess, 'exit');
}
await runningSubtests.get(file);
runningSubtests.set(file, runTestFile(file, root, inspectPort, filesWatcher));
runningSubtests.set(file, runTestFile({ file, root, inspectPort, filesWatcher, testNamePatterns }));
}, undefined, (error) => {
triggerUncaughtException(error, true /* fromPromise */);
}));
Expand All @@ -354,7 +355,7 @@ function run(options) {
if (options === null || typeof options !== 'object') {
options = kEmptyObject;
}
const { concurrency, timeout, signal, files, inspectPort, watch, setup } = options;
const { concurrency, timeout, signal, files, inspectPort, watch, setup, testNamePatterns } = options;

if (files != null) {
validateArray(files, 'options.files');
Expand All @@ -372,11 +373,11 @@ function run(options) {
let postRun = () => root.postRun();
let filesWatcher;
if (watch) {
filesWatcher = watchFiles(testFiles, root, inspectPort);
filesWatcher = watchFiles(testFiles, root, inspectPort, testNamePatterns);
postRun = undefined;
}
const runFiles = () => SafePromiseAllSettledReturnVoid(testFiles, (path) => {
const subtest = runTestFile(path, root, inspectPort, filesWatcher);
const subtest = runTestFile({ path, root, inspectPort, filesWatcher, testNamePatterns });
runningSubtests.set(path, subtest);
return subtest;
});
Expand Down
14 changes: 11 additions & 3 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { AbortController } = require('internal/abort_controller');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_ARG_NOT_ALLOWED,
ERR_TEST_FAILURE,
},
AbortError,
Expand Down Expand Up @@ -163,9 +164,16 @@ class Test extends AsyncResource {
constructor(options) {
super('Test');

let { fn, name, parent, skip } = options;
let { fn, name, parent, skip, testNamePatterns: _testNamePatterns } = options;
const { concurrency, only, timeout, todo, signal } = options;

if (_testNamePatterns && testNamePatterns) {
throw new ERR_ARG_NOT_ALLOWED('testNamePatterns');
}
if (!_testNamePatterns) {
_testNamePatterns = testNamePatterns;
}

if (typeof fn !== 'function') {
fn = noop;
}
Expand Down Expand Up @@ -224,10 +232,10 @@ class Test extends AsyncResource {
this.timeout = timeout;
}

if (testNamePatterns !== null) {
if (_testNamePatterns !== null) {
// eslint-disable-next-line no-use-before-define
const match = this instanceof TestHook || ArrayPrototypeSome(
testNamePatterns,
_testNamePatterns,
(re) => RegExpPrototypeExec(re, name) !== null,
);

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/test-runner/test/random.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
const test = require('node:test');

test('this should pass');

test('this should pass too');
10 changes: 9 additions & 1 deletion test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should succeed with a file', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});

it('should succeed with a file and filter', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs')], testNamePatterns: [/too/] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(1));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
Expand All @@ -35,7 +43,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should run same file twice', async () => {
const stream = run({ files: [join(testFixtures, 'test/random.cjs'), join(testFixtures, 'test/random.cjs')] });
stream.on('test:fail', common.mustNotCall());
stream.on('test:pass', common.mustCall(2));
stream.on('test:pass', common.mustCall(4));
// eslint-disable-next-line no-unused-vars
for await (const _ of stream);
});
Expand Down

0 comments on commit 2287515

Please sign in to comment.