Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runner): nested tests should throw errors #4262

Merged
merged 4 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`)
})
})
Loading