From 8ac9f8b1fa5094cabb6e3fba2bef5e1311554016 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 16 Oct 2023 02:19:57 -0500 Subject: [PATCH] fix(runner): nested tests should throw errors (#4262) --- packages/runner/src/suite.ts | 4 ++++ test/core/test/nested-test.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/core/test/nested-test.test.ts diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 26b9ef28b140..2ae4f09e7252 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -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) }, ) diff --git a/test/core/test/nested-test.test.ts b/test/core/test/nested-test.test.ts new file mode 100644 index 000000000000..54efdb671258 --- /dev/null +++ b/test/core/test/nested-test.test.ts @@ -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"`) + }) +})