NodeJS by Example: Test Runner cli

The test runner also contains multiple command line options that allow you to customize the behavior of the test runner.

import test from 'node:test';

1. By adding the --test-only flag you can execute tests that have the only option set to true.

test('only test', { only: true }, (t) => {
  assert.strictEqual(1, 1);
});

test('This test will not run', () => {
  throw new Error('This test will not run');
});

2. Using the flag --test-name-pattern you can run tests whose name matches the given pattern. The pattern is interpreted as a regular expression. You can also chain --test-name-pattern flag to run multiple tests.
Using the following flag --test-name-pattern="test [1-2]" will run test 1 and test 2.

test('test 1', (t) => {
  assert.strictEqual(1, 1);
});

test('test 2', (t) => {
  assert.strictEqual(1, 1);
});

test('test 3', (t) => {
  assert.strictEqual(1, 1);
});