How do I skip tests when using node native test runner with TypeScript/esrun?

24 Views Asked by At

I am using the node test runner. have some tests in a small demo file:

import { describe, test } from "node:test";
import assert from "node:assert";

describe("thing", () => {
  test("it works", () => {
    assert.ok(true);
  });
});

describe("this should not be tested", () => {
  test("it works", () => {
    assert.ok(true);
  });
});

When I run node --test-name-pattern="thing" runme.test.mjs I can see it only tests thing and skips this should not be tested:

▶ thing
  ✔ it works (0.153ms)
▶ thing (1.306708ms)

▶ this should not be tested
  ﹣ it works (0.13525ms) # test name does not match pattern
▶ this should not be tested (0.23425ms)

ℹ tests 2
ℹ suites 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 0.079417

Great! I can see this should not be tested is skipped!

However I want to use TypeScript for my tests. Using esrun and following the esrun docs I am running:

esrun runme.test.mjs --test-name-pattern="thing"

(yes, this is the same JS file as before, esrun can also run JS files)

However the tests are not skipped:

▶ thing
  ✔ it works (0.11725ms)
▶ thing (0.945292ms)

▶ this should not be tested
  ✔ it works (0.040375ms)
▶ this should not be tested (0.100708ms)

ℹ tests 2
ℹ suites 2
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 0.105375

How can I skip tests when using the node native test runner with TypeScript/esrun?

1

There are 1 best solutions below

0
mikemaccana On

Answering my own question to help others

The correct syntax is mentioned in the esrun docs, but I had missed it:

You can pass custom options to the node cli by prefixing the option name with "--node", like this: esrun --node-max-old-space-size=4096 foo.ts

So to skip tests with the native test runner, use --node-test-name-pattern:

esrun --node-test-name-pattern="thing" runme.test.mjs

▶ thing
  ✔ it works (0.144791ms)
▶ thing (1.027917ms)

▶ this should not be tested
  ﹣ it works (0.048459ms) # test name does not match pattern
▶ this should not be tested (0.124208ms)

ℹ tests 2
ℹ suites 2
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 0.124166

This also works with TS files (including both .ts and .mts).