From e19b550fdaccfd106042c57d89eab28f6c1e5fec Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Sun, 7 May 2023 21:35:39 +0300 Subject: [PATCH] test_runner: add shorthands to `test` --- doc/api/test.md | 18 +++++++++ lib/internal/test_runner/harness.js | 8 +--- .../fixtures/test-runner/output/only_tests.js | 18 +++++++++ .../test-runner/output/only_tests.snapshot | 38 +++++++++++++++++-- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index 010c8850a32a96..a72a209c7deb8e 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -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 @@ -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 diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index a501efacdfa92d..dd1f386e1017d9 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -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); @@ -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 }); @@ -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'), diff --git a/test/fixtures/test-runner/output/only_tests.js b/test/fixtures/test-runner/output/only_tests.js index 97df061f8268d3..9ed3154a5d6c67 100644 --- a/test/fixtures/test-runner/output/only_tests.js +++ b/test/fixtures/test-runner/output/only_tests.js @@ -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', () => { diff --git a/test/fixtures/test-runner/output/only_tests.snapshot b/test/fixtures/test-runner/output/only_tests.snapshot index d0cab370edcac0..4ee09070315b17 100644 --- a/test/fixtures/test-runner/output/only_tests.snapshot +++ b/test/fixtures/test-runner/output/only_tests.snapshot @@ -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: * @@ -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 *