summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChemi Atlow <chemi@testim.io>2023-05-15 08:23:22 +0300
committerMichaƫl Zasso <targos@protonmail.com>2023-05-15 11:37:37 +0200
commit17befe008ce9803b12eb441b21036cbf532b0018 (patch)
treedea09e5af8213d8bb8a1df748ccc493755b621e9
parent060c1d502b6bf259360b010152f38f92b536df32 (diff)
downloadnode-new-17befe008ce9803b12eb441b21036cbf532b0018.tar.gz
test_runner: add shorthands to `test`
PR-URL: https://github.com/nodejs/node/pull/47909 Fixes: https://github.com/nodejs/node/issues/47897 Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
-rw-r--r--doc/api/test.md18
-rw-r--r--lib/internal/test_runner/harness.js8
-rw-r--r--test/fixtures/test-runner/output/only_tests.js18
-rw-r--r--test/fixtures/test-runner/output/only_tests.snapshot38
4 files changed, 72 insertions, 10 deletions
diff --git a/doc/api/test.md b/doc/api/test.md
index eaa4557924..94a819e1bf 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 a501efacdf..dd1f386e10 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 97df061f82..9ed3154a5d 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 d0cab370ed..4ee0907031 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 *