Skip to content

Commit 8ac9f8b

Browse files
authored
fix(runner): nested tests should throw errors (#4262)
1 parent a4501d6 commit 8ac9f8b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/runner/src/suite.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import { collectTask, collectorContext, createTestContext, runWithSuite, withTim
66
import { getHooks, setFixture, setFn, setHooks } from './map'
77
import type { FixtureItem } from './fixture'
88
import { mergeContextFixtures, withFixtures } from './fixture'
9+
import { getCurrentTest } from './test-state'
910

1011
// apis
1112
export const suite = createSuite()
1213
export const test = createTest(
1314
function (name: string | Function, fn?: TestFunction, options?: number | TestOptions) {
15+
if (getCurrentTest())
16+
throw new Error('Nested tests are not allowed')
17+
1418
getCurrentSuite().test.fn.call(this, formatName(name), fn, options)
1519
},
1620
)

test/core/test/nested-test.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
test('nested test should throw error', () => {
4+
expect(() => {
5+
test('test inside test', () => {})
6+
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
7+
8+
expect(() => {
9+
test.each([1, 2, 3])('test.each inside test %d', () => {})
10+
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
11+
12+
expect(() => {
13+
test.skipIf(false)('test.skipIf inside test', () => {})
14+
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
15+
})
16+
17+
describe('parallel tests', () => {
18+
test.concurrent('parallel test 1 with nested test', () => {
19+
expect(() => {
20+
test('test inside test', () => {})
21+
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
22+
})
23+
test.concurrent('parallel test 2 without nested test', () => {})
24+
test.concurrent('parallel test 3 without nested test', () => {})
25+
test.concurrent('parallel test 4 with nested test', () => {
26+
expect(() => {
27+
test('test inside test', () => {})
28+
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
29+
})
30+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy