From bd02ab8195d4b3ea7a51f723017302c4ffa97d84 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 6 Oct 2023 23:24:47 +0800 Subject: [PATCH 1/3] fix(runner): nested tests should throw errors --- packages/runner/src/suite.ts | 4 ++++ test/core/test/nested-test.test.ts | 15 +++++++++++++++ 2 files changed, 19 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..ed213ab43aef --- /dev/null +++ b/test/core/test/nested-test.test.ts @@ -0,0 +1,15 @@ +import { expect, test } from 'vitest' + +test('nested test should throw error', () => { + expect(() => { + test('test inside test', () => {}) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) + + expect(() => { + test.each([1, 2, 3])('test.each inside test %d', () => {}) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) + + expect(() => { + test.skipIf(false)('test.skipIf inside test', () => {}) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) +}) From 17e7349cc5b5795cfb1291aa08c4b90399fec638 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Fri, 6 Oct 2023 10:34:34 -0500 Subject: [PATCH 2/3] Update nested-test.test.ts --- test/core/test/nested-test.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/core/test/nested-test.test.ts b/test/core/test/nested-test.test.ts index ed213ab43aef..7b89d6345543 100644 --- a/test/core/test/nested-test.test.ts +++ b/test/core/test/nested-test.test.ts @@ -3,13 +3,13 @@ import { expect, test } from 'vitest' test('nested test should throw error', () => { expect(() => { test('test inside test', () => {}) - }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`) expect(() => { test.each([1, 2, 3])('test.each inside test %d', () => {}) - }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`) expect(() => { test.skipIf(false)('test.skipIf inside test', () => {}) - }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not supported"`) + }).toThrowErrorMatchingInlineSnapshot(`"Nested tests are not allowed"`) }) From e9a7bba30084afe430d240d77da510a0310f5383 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 7 Oct 2023 08:55:01 +0800 Subject: [PATCH 3/3] test: update parallel test case --- test/core/test/nested-test.test.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/core/test/nested-test.test.ts b/test/core/test/nested-test.test.ts index 7b89d6345543..54efdb671258 100644 --- a/test/core/test/nested-test.test.ts +++ b/test/core/test/nested-test.test.ts @@ -1,4 +1,4 @@ -import { expect, test } from 'vitest' +import { describe, expect, test } from 'vitest' test('nested test should throw error', () => { expect(() => { @@ -13,3 +13,18 @@ test('nested test should throw error', () => { 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"`) + }) +})