• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --no-warnings --test-only
2'use strict';
3require('../../../common');
4const { test, describe, it } = require('node:test');
5
6// These tests should be skipped based on the 'only' option.
7test('only = undefined');
8test('only = undefined, skip = string', { skip: 'skip message' });
9test('only = undefined, skip = true', { skip: true });
10test('only = undefined, skip = false', { skip: false });
11test('only = false', { only: false });
12test('only = false, skip = string', { only: false, skip: 'skip message' });
13test('only = false, skip = true', { only: false, skip: true });
14test('only = false, skip = false', { only: false, skip: false });
15
16// These tests should be skipped based on the 'skip' option.
17test('only = true, skip = string', { only: true, skip: 'skip message' });
18test('only = true, skip = true', { only: true, skip: true });
19
20// An 'only' test with subtests.
21test('only = true, with subtests', { only: true }, async (t) => {
22  // These subtests should run.
23  await t.test('running subtest 1');
24  await t.test('running subtest 2');
25
26  // Switch the context to only execute 'only' tests.
27  t.runOnly(true);
28  await t.test('skipped subtest 1');
29  await t.test('skipped subtest 2');
30  await t.test('running subtest 3', { only: true });
31
32  // Switch the context back to execute all tests.
33  t.runOnly(false);
34  await t.test('running subtest 4', async (t) => {
35    // These subtests should run.
36    await t.test('running sub-subtest 1');
37    await t.test('running sub-subtest 2');
38
39    // Switch the context to only execute 'only' tests.
40    t.runOnly(true);
41    await t.test('skipped sub-subtest 1');
42    await t.test('skipped sub-subtest 2');
43  });
44
45  // Explicitly do not run these tests.
46  await t.test('skipped subtest 3', { only: false });
47  await t.test('skipped subtest 4', { skip: true });
48});
49
50describe.only('describe only = true, with subtests', () => {
51  it.only('`it` subtest 1 should run', () => {});
52
53  it('`it` subtest 2 should not run', async () => {});
54});
55
56describe.only('describe only = true, with a mixture of subtests', () => {
57  it.only('`it` subtest 1', () => {});
58
59  it.only('`it` async subtest 1', async () => {});
60
61  it('`it` subtest 2 only=true', { only: true });
62
63  it('`it` subtest 2 only=false', { only: false }, () => {
64    throw new Error('This should not run');
65  });
66
67  it.skip('`it` subtest 3 skip', () => {
68    throw new Error('This should not run');
69  });
70
71  it.todo('`it` subtest 4 todo', { only: false }, () => {
72    throw new Error('This should not run');
73  });
74
75  test.only('`test` subtest 1', () => {});
76
77  test.only('`test` async subtest 1', async () => {});
78
79  test('`test` subtest 2 only=true', { only: true });
80
81  test('`test` subtest 2 only=false', { only: false }, () => {
82    throw new Error('This should not run');
83  });
84
85  test.skip('`test` subtest 3 skip', () => {
86    throw new Error('This should not run');
87  });
88
89  test.todo('`test` subtest 4 todo', { only: false }, () => {
90    throw new Error('This should not run');
91  });
92});
93
94describe.only('describe only = true, with subtests', () => {
95  test.only('subtest should run', () => {});
96
97  test('async subtest should not run', async () => {});
98
99  test('subtest should be skipped', { only: false }, () => {});
100});
101