diff options
author | Richie McColl <richie.mccoll@hotmail.co.uk> | 2023-02-21 22:28:03 +0000 |
---|---|---|
committer | Juan José Arboleda <soyjuanarbol@gmail.com> | 2023-03-04 23:03:34 -0500 |
commit | 0a690efb76f4498ad891f2507e4dfc24bd52a3fd (patch) | |
tree | 25cb860c9ab80221bacb38af724020c56b6638a4 | |
parent | 28a1317efe974cb657ba89e81d3df7b6bd9bec3a (diff) | |
download | node-new-0a690efb76f4498ad891f2507e4dfc24bd52a3fd.tar.gz |
test_runner: add `describe.only` and `it.only` shorthands
PR-URL: https://github.com/nodejs/node/pull/46604
Backport-PR-URL: https://github.com/nodejs/node/pull/46839
Fixes: https://github.com/nodejs/node/issues/46562
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
-rw-r--r-- | doc/api/test.md | 18 | ||||
-rw-r--r-- | lib/internal/test_runner/harness.js | 2 | ||||
-rw-r--r-- | test/message/test_runner_only_tests.js | 36 | ||||
-rw-r--r-- | test/message/test_runner_only_tests.out | 79 |
4 files changed, 130 insertions, 5 deletions
diff --git a/doc/api/test.md b/doc/api/test.md index a67907c359..4c353c8869 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -803,6 +803,15 @@ Shorthand for skipping a suite, same as [`describe([name], { skip: true }[, fn]) Shorthand for marking a suite as `TODO`, same as [`describe([name], { todo: true }[, fn])`][describe options]. +## `describe.only([name][, options][, fn])` + +<!-- YAML +added: REPLACEME +--> + +Shorthand for marking a suite as `only`, same as +[`describe([name], { only: true }[, fn])`][describe options]. + ## `it([name][, options][, fn])` * `name` {string} The name of the test, which is displayed when reporting test @@ -827,6 +836,15 @@ same as [`it([name], { skip: true }[, fn])`][it options]. Shorthand for marking a test as `TODO`, same as [`it([name], { todo: true }[, fn])`][it options]. +## `it.only([name][, options][, fn])` + +<!-- YAML +added: REPLACEME +--> + +Shorthand for marking a test as `only`, +same as [`it([name], { only: true }[, fn])`][it options]. + ## `before([fn][, options])` <!-- YAML diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index 26ea67d998..da68d944f0 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -195,7 +195,7 @@ function runInParentContext(Factory) { run(name, options, fn); }; - ArrayPrototypeForEach(['skip', 'todo'], (keyword) => { + ArrayPrototypeForEach(['skip', 'todo', 'only'], (keyword) => { cb[keyword] = (name, options, fn) => { run(name, options, fn, { [keyword]: true }); }; diff --git a/test/message/test_runner_only_tests.js b/test/message/test_runner_only_tests.js index b6aeb580af..7cce63d2d8 100644 --- a/test/message/test_runner_only_tests.js +++ b/test/message/test_runner_only_tests.js @@ -1,7 +1,7 @@ // Flags: --no-warnings --test-only 'use strict'; require('../common'); -const test = require('node:test'); +const { test, describe, it } = require('node:test'); // These tests should be skipped based on the 'only' option. test('only = undefined'); @@ -46,3 +46,37 @@ test('only = true, with subtests', { only: true }, async (t) => { await t.test('skipped subtest 3', { only: false }); await t.test('skipped subtest 4', { skip: true }); }); + +describe.only('describe only = true, with subtests', () => { + it('`it` subtest 1 should run', () => {}); + + it('`it` subtest 2 should run', async () => {}); +}); + +describe.only('describe only = true, with a mixture of subtests', () => { + it.only('`it` subtest 1', () => {}); + + it.only('`it` async subtest 1', async () => {}); + + it('`it` subtest 2 only=true', { only: true }); + + it('`it` subtest 2 only=false', { only: false }, () => { + throw new Error('This should not run'); + }); + + it.skip('`it` subtest 3 skip', () => { + throw new Error('This should not run'); + }); + + it.todo('`it` subtest 4 todo', { only: false }, () => { + throw new Error('This should not run'); + }); +}); + +describe.only('describe only = true, with subtests', () => { + test('subtest should run', () => {}); + + test('async subtest should run', async () => {}); + + test('subtest should be skipped', { only: false }, () => {}); +}); diff --git a/test/message/test_runner_only_tests.out b/test/message/test_runner_only_tests.out index c471a5284e..7d8240fef0 100644 --- a/test/message/test_runner_only_tests.out +++ b/test/message/test_runner_only_tests.out @@ -116,9 +116,82 @@ ok 11 - only = true, with subtests --- duration_ms: * ... -1..11 -# tests 11 -# pass 1 +# Subtest: describe only = true, with subtests + # Subtest: `it` subtest 1 should run + ok 1 - `it` subtest 1 should run + --- + duration_ms: * + ... + # Subtest: `it` subtest 2 should run + ok 2 - `it` subtest 2 should run + --- + duration_ms: * + ... + 1..2 +ok 12 - describe only = true, with subtests + --- + duration_ms: * + ... +# Subtest: describe only = true, with a mixture of subtests + # Subtest: `it` subtest 1 + ok 1 - `it` subtest 1 + --- + duration_ms: * + ... + # Subtest: `it` async subtest 1 + ok 2 - `it` async subtest 1 + --- + duration_ms: * + ... + # Subtest: `it` subtest 2 only=true + ok 3 - `it` subtest 2 only=true + --- + duration_ms: * + ... + # Subtest: `it` subtest 2 only=false + ok 4 - `it` subtest 2 only=false # SKIP 'only' option not set + --- + duration_ms: * + ... + # Subtest: `it` subtest 3 skip + ok 5 - `it` subtest 3 skip # SKIP + --- + duration_ms: * + ... + # Subtest: `it` subtest 4 todo + ok 6 - `it` subtest 4 todo # SKIP 'only' option not set + --- + duration_ms: * + ... + 1..6 +ok 13 - describe only = true, with a mixture of subtests + --- + duration_ms: * + ... +# Subtest: describe only = true, with subtests + # Subtest: subtest should run + ok 1 - subtest should run + --- + duration_ms: * + ... + # Subtest: async subtest should run + ok 2 - async subtest should run + --- + duration_ms: * + ... + # Subtest: subtest should be skipped + ok 3 - subtest should be skipped # SKIP 'only' option not set + --- + duration_ms: * + ... + 1..3 +ok 14 - describe only = true, with subtests + --- + duration_ms: * + ... +1..14 +# tests 14 +# pass 4 # fail 0 # cancelled 0 # skipped 10 |