Skip to content

Commit

Permalink
test_runner: support typescript files in default glob
Browse files Browse the repository at this point in the history
PR-URL: #55081
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
RedYetiDev authored and targos committed Oct 4, 2024
1 parent 7982d3d commit 1240197
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
23 changes: 17 additions & 6 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,22 @@ node --test

By default, Node.js will run all files matching these patterns:

* `**/*.test.?(c|m)js`
* `**/*-test.?(c|m)js`
* `**/*_test.?(c|m)js`
* `**/test-*.?(c|m)js`
* `**/test.?(c|m)js`
* `**/test/**/*.?(c|m)js`
* `**/*.test.{cjs,mjs,js}`
* `**/*-test.{cjs,mjs,js}`
* `**/*_test.{cjs,mjs,js}`
* `**/test-*.{cjs,mjs,js}`
* `**/test.{cjs,mjs,js}`
* `**/test/**/*.{cjs,mjs,js}`

When [`--experimental-strip-types`][] is supplied, the following
additional patterns are matched:

* `**/*.test.{cts,mts,ts}`
* `**/*-test.{cts,mts,ts}`
* `**/*_test.{cts,mts,ts}`
* `**/test-*.{cts,mts,ts}`
* `**/test.{cts,mts,ts}`
* `**/test/**/*.{cts,mts,ts}`

Alternatively, one or more glob patterns can be provided as the
final argument(s) to the Node.js command, as shown below.
Expand Down Expand Up @@ -3558,6 +3568,7 @@ Can be used to abort test subtasks when the test has been aborted.

[TAP]: https://testanything.org/
[TTY]: tty.md
[`--experimental-strip-types`]: cli.md#--experimental-strip-types
[`--experimental-test-coverage`]: cli.md#--experimental-test-coverage
[`--experimental-test-module-mocks`]: cli.md#--experimental-test-module-mocks
[`--experimental-test-snapshots`]: cli.md#--experimental-test-snapshots
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ const kMultipleCallbackInvocations = 'multipleCallbackInvocations';
const kRegExpPattern = /^\/(.*)\/([a-z]*)$/;

const kPatterns = ['test', 'test/**/*', 'test-*', '*[._-]test'];
const kDefaultPattern = `**/{${ArrayPrototypeJoin(kPatterns, ',')}}.?(c|m)js`;

const kFileExtensions = ['js', 'mjs', 'cjs'];
if (getOptionValue('--experimental-strip-types')) {
ArrayPrototypePush(kFileExtensions, 'ts', 'mts', 'cts');
}
const kDefaultPattern = `**/{${ArrayPrototypeJoin(kPatterns, ',')}}.{${ArrayPrototypeJoin(kFileExtensions, ',')}}`;

function createDeferredCallback() {
let calledCount = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('this should pass' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { test } from 'node:test';

// 'as string' ensures that type stripping actually occurs
test('this should pass' as string);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const test = require('node:test');

// 'as string' ensures that type stripping actually occurs
test('this should pass' as string);
21 changes: 21 additions & 0 deletions test/parallel/test-runner-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ for (const isolation of ['none', 'process']) {
assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /ok 2 - this should pass/);
assert.match(stdout, /ok 3 - this should pass/);
// Doesn't match the TypeScript files
assert.doesNotMatch(stdout, /ok 4 - this should pass/);
}

for (const type of ['strip', 'transform']) {
// Should match files with "-test.(c|m)(t|j)s" suffix when typescript support is enabled
const args = ['--test', '--test-reporter=tap', '--no-warnings',
`--experimental-${type}-types`, `--experimental-test-isolation=${isolation}`];
const child = spawnSync(process.execPath, args, { cwd: join(testFixtures, 'matching-patterns') });

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
assert.strictEqual(child.stderr.toString(), '');
const stdout = child.stdout.toString();

assert.match(stdout, /ok 1 - this should pass/);
assert.match(stdout, /ok 2 - this should pass/);
assert.match(stdout, /ok 3 - this should pass/);
assert.match(stdout, /ok 4 - this should pass/);
assert.match(stdout, /ok 5 - this should pass/);
assert.match(stdout, /ok 6 - this should pass/);
}

{
Expand Down

0 comments on commit 1240197

Please sign in to comment.