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: support typescript files in default glob #55081

Merged
merged 1 commit into from
Sep 29, 2024
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: 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 @@ -3562,6 +3572,7 @@ added:
Can be used to abort test subtasks when the test has been aborted.

[TAP]: https://testanything.org/
[`--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
Loading