Skip to content

Commit

Permalink
fix(runner): nested tests should throw errors (#4262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Oct 16, 2023
1 parent a4501d6 commit 8ac9f8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { collectTask, collectorContext, createTestContext, runWithSuite, withTim
import { getHooks, setFixture, setFn, setHooks } from './map'
import type { FixtureItem } from './fixture'
import { mergeContextFixtures, withFixtures } from './fixture'
import { getCurrentTest } from './test-state'

// apis
export const suite = createSuite()
export const test = createTest(
function (name: string | Function, fn?: TestFunction, options?: number | TestOptions) {
if (getCurrentTest())
throw new Error('Nested tests are not allowed')

getCurrentSuite().test.fn.call(this, formatName(name), fn, options)
},
)
Expand Down
30 changes: 30 additions & 0 deletions test/core/test/nested-test.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, test } from 'vitest'

test('nested test should throw error', () => {
expect(() => {
test('test inside test', () => {})
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)

expect(() => {
test.each([1, 2, 3])('test.each inside test %d', () => {})
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)

expect(() => {
test.skipIf(false)('test.skipIf inside test', () => {})
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
})

describe('parallel tests', () => {
test.concurrent('parallel test 1 with nested test', () => {
expect(() => {
test('test inside test', () => {})
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
})
test.concurrent('parallel test 2 without nested test', () => {})
test.concurrent('parallel test 3 without nested test', () => {})
test.concurrent('parallel test 4 with nested test', () => {
expect(() => {
test('test inside test', () => {})
}).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`)
})
})

0 comments on commit 8ac9f8b

Please sign in to comment.