From fb8fc7ab1267ca54e1e116983d595f3dc7ebe5d5 Mon Sep 17 00:00:00 2001 From: fenghan34 Date: Wed, 17 May 2023 21:48:29 +0800 Subject: [PATCH] fix: test repeats (#3369) --- packages/runner/src/suite.ts | 18 +++++++++++++++--- packages/runner/src/types/tasks.ts | 7 +++++-- test/core/test/repeats.test.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 15050880ce5..2c8fa8ecfef 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -53,7 +53,7 @@ export function createSuiteHooks() { } // implementations -function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, suiteOptions?: number | TestOptions) { +function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, suiteOptions?: TestOptions) { const tasks: (Test | TaskCustom | Suite | SuiteCollector)[] = [] const factoryQueue: (Test | Suite | SuiteCollector)[] = [] @@ -61,7 +61,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m initSuite() - const test = createTest(function (name: string, fn = noop, options = suiteOptions) { + const test = createTest(function (name: string, fn = noop, options) { const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run' if (typeof options === 'number') @@ -70,7 +70,8 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m // inherit repeats and retry from suite if (typeof suiteOptions === 'object') { options = { - ...suiteOptions, + repeats: suiteOptions.repeats, + retry: suiteOptions.retry, ...options, } } @@ -122,6 +123,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m type: 'collector', name, mode, + options: suiteOptions, test, tasks, collect, @@ -186,6 +188,16 @@ function createSuite() { function suiteFn(this: Record, name: string, factory?: SuiteFactory, options?: number | TestOptions) { checkVersion() const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run' + const currentSuite = getCurrentSuite() + + if (typeof options === 'number') + options = { timeout: options } + + if (currentSuite && typeof currentSuite.options?.repeats === 'number') { + // inherit repeats from current suite + options = { repeats: currentSuite.options.repeats, ...options } + } + return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options) } diff --git a/packages/runner/src/types/tasks.ts b/packages/runner/src/types/tasks.ts index 5c548d87240..194189ba60e 100644 --- a/packages/runner/src/types/tasks.ts +++ b/packages/runner/src/types/tasks.ts @@ -168,9 +168,11 @@ export interface TestOptions { */ retry?: number /** - * How many times the test will repeat. + * How many times the test will run. + * Only inner tests will repeat if set on `describe()`, nested `describe()` will inherit parent's repeat by default. + * + * @default 1 * - * @default 5 */ repeats?: number } @@ -211,6 +213,7 @@ export interface SuiteHooks { export interface SuiteCollector { readonly name: string readonly mode: RunMode + options?: TestOptions type: 'collector' test: TestAPI tasks: (Suite | TaskCustom | Test | SuiteCollector)[] diff --git a/test/core/test/repeats.test.ts b/test/core/test/repeats.test.ts index 7542ceada92..711319440ba 100644 --- a/test/core/test/repeats.test.ts +++ b/test/core/test/repeats.test.ts @@ -48,3 +48,33 @@ describe('testing repeats with retry', () => { expect(retryNumbers).toStrictEqual(result) }) }) + +const nestedDescribeNumbers: number[] = [] + +describe('testing nested describe', () => { + test ('test 1', () => { + nestedDescribeNumbers.push(1) + }) + + describe('nested 1', () => { + test('test 2', () => { + nestedDescribeNumbers.push(2) + }) + + describe('nested 2', () => { + test('test 3', () => { + nestedDescribeNumbers.push(3) + }) + + describe('nested 3', () => { + test('test 4', () => { + nestedDescribeNumbers.push(4) + }) + }, 100) + }, { repeats: 3 }) + }) + + afterAll(() => { + expect(nestedDescribeNumbers).toStrictEqual([1, 1, 2, 2, 3, 3, 3, 4, 4, 4]) + }) +}, { repeats: 2 })