Skip to content

Commit

Permalink
test_runner: add shorthands to test
Browse files Browse the repository at this point in the history
PR-URL: #47909
Fixes: #47897
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
atlowChemi authored May 15, 2023
1 parent 3ef17b6 commit 8b3777d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
18 changes: 18 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,9 @@ added:
- v18.0.0
- v16.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/47909
description: Added the `skip`, `todo`, and `only` shorthands.
- version:
- v18.8.0
- v16.18.0
Expand Down Expand Up @@ -864,6 +867,21 @@ The `timeout` option can be used to fail the test if it takes longer than
canceling tests because a running test might block the application thread and
thus prevent the scheduled cancellation.

## `test.skip([name][, options][, fn])`

Shorthand for skipping a test,
same as [`test([name], { skip: true }[, fn])`][it options].

## `test.todo([name][, options][, fn])`

Shorthand for marking a test as `TODO`,
same as [`test([name], { todo: true }[, fn])`][it options].

## `test.only([name][, options][, fn])`

Shorthand for marking a test as `only`,
same as [`test([name], { only: true }[, fn])`][it options].

## `describe([name][, options][, fn])`

* `name` {string} The name of the suite, which is displayed when reporting test
Expand Down
8 changes: 2 additions & 6 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ async function startSubtest(subtest) {
await subtest.start();
}

function runInParentContext(Factory, addShorthands = true) {
function runInParentContext(Factory) {
function run(name, options, fn, overrides) {
const parent = testResources.get(executionAsyncId()) || getGlobalRoot();
const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
Expand All @@ -214,10 +214,6 @@ function runInParentContext(Factory, addShorthands = true) {
}

const test = (name, options, fn) => run(name, options, fn);
if (!addShorthands) {
return test;
}

ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => {
test[keyword] = (name, options, fn) => {
run(name, options, fn, { [keyword]: true });
Expand All @@ -235,7 +231,7 @@ function hook(hook) {

module.exports = {
createTestTree,
test: runInParentContext(Test, false),
test: runInParentContext(Test),
describe: runInParentContext(Suite),
it: runInParentContext(Test),
before: hook('before'),
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/test-runner/output/only_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ describe.only('describe only = true, with a mixture of subtests', () => {
it.todo('`it` subtest 4 todo', { only: false }, () => {
throw new Error('This should not run');
});

test.only('`test` subtest 1', () => {});

test.only('`test` async subtest 1', async () => {});

test('`test` subtest 2 only=true', { only: true });

test('`test` subtest 2 only=false', { only: false }, () => {
throw new Error('This should not run');
});

test.skip('`test` subtest 3 skip', () => {
throw new Error('This should not run');
});

test.todo('`test` subtest 4 todo', { only: false }, () => {
throw new Error('This should not run');
});
});

describe.only('describe only = true, with subtests', () => {
Expand Down
38 changes: 34 additions & 4 deletions test/fixtures/test-runner/output/only_tests.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,37 @@ ok 12 - describe only = true, with subtests
---
duration_ms: *
...
1..6
# Subtest: `test` subtest 1
ok 7 - `test` subtest 1
---
duration_ms: *
...
# Subtest: `test` async subtest 1
ok 8 - `test` async subtest 1
---
duration_ms: *
...
# Subtest: `test` subtest 2 only=true
ok 9 - `test` subtest 2 only=true
---
duration_ms: *
...
# Subtest: `test` subtest 2 only=false
ok 10 - `test` subtest 2 only=false # SKIP 'only' option not set
---
duration_ms: *
...
# Subtest: `test` subtest 3 skip
ok 11 - `test` subtest 3 skip # SKIP
---
duration_ms: *
...
# Subtest: `test` subtest 4 todo
ok 12 - `test` subtest 4 todo # SKIP 'only' option not set
---
duration_ms: *
...
1..12
ok 13 - describe only = true, with a mixture of subtests
---
duration_ms: *
Expand Down Expand Up @@ -193,11 +223,11 @@ ok 14 - describe only = true, with subtests
type: 'suite'
...
1..14
# tests 34
# tests 40
# suites 3
# pass 14
# pass 17
# fail 0
# cancelled 0
# skipped 20
# skipped 23
# todo 0
# duration_ms *

0 comments on commit 8b3777d

Please sign in to comment.