Skip to content

Commit fb8fc7a

Browse files
authoredMay 17, 2023
fix: test repeats (#3369)
1 parent 6501d2e commit fb8fc7a

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
 

‎packages/runner/src/suite.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ export function createSuiteHooks() {
5353
}
5454

5555
// implementations
56-
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, suiteOptions?: number | TestOptions) {
56+
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, suiteOptions?: TestOptions) {
5757
const tasks: (Test | TaskCustom | Suite | SuiteCollector)[] = []
5858
const factoryQueue: (Test | Suite | SuiteCollector)[] = []
5959

6060
let suite: Suite
6161

6262
initSuite()
6363

64-
const test = createTest(function (name: string, fn = noop, options = suiteOptions) {
64+
const test = createTest(function (name: string, fn = noop, options) {
6565
const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
6666

6767
if (typeof options === 'number')
@@ -70,7 +70,8 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
7070
// inherit repeats and retry from suite
7171
if (typeof suiteOptions === 'object') {
7272
options = {
73-
...suiteOptions,
73+
repeats: suiteOptions.repeats,
74+
retry: suiteOptions.retry,
7475
...options,
7576
}
7677
}
@@ -122,6 +123,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
122123
type: 'collector',
123124
name,
124125
mode,
126+
options: suiteOptions,
125127
test,
126128
tasks,
127129
collect,
@@ -186,6 +188,16 @@ function createSuite() {
186188
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
187189
checkVersion()
188190
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
191+
const currentSuite = getCurrentSuite()
192+
193+
if (typeof options === 'number')
194+
options = { timeout: options }
195+
196+
if (currentSuite && typeof currentSuite.options?.repeats === 'number') {
197+
// inherit repeats from current suite
198+
options = { repeats: currentSuite.options.repeats, ...options }
199+
}
200+
189201
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options)
190202
}
191203

‎packages/runner/src/types/tasks.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ export interface TestOptions {
168168
*/
169169
retry?: number
170170
/**
171-
* How many times the test will repeat.
171+
* How many times the test will run.
172+
* Only inner tests will repeat if set on `describe()`, nested `describe()` will inherit parent's repeat by default.
173+
*
174+
* @default 1
172175
*
173-
* @default 5
174176
*/
175177
repeats?: number
176178
}
@@ -211,6 +213,7 @@ export interface SuiteHooks<ExtraContext = {}> {
211213
export interface SuiteCollector<ExtraContext = {}> {
212214
readonly name: string
213215
readonly mode: RunMode
216+
options?: TestOptions
214217
type: 'collector'
215218
test: TestAPI<ExtraContext>
216219
tasks: (Suite | TaskCustom | Test | SuiteCollector<ExtraContext>)[]

‎test/core/test/repeats.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,33 @@ describe('testing repeats with retry', () => {
4848
expect(retryNumbers).toStrictEqual(result)
4949
})
5050
})
51+
52+
const nestedDescribeNumbers: number[] = []
53+
54+
describe('testing nested describe', () => {
55+
test ('test 1', () => {
56+
nestedDescribeNumbers.push(1)
57+
})
58+
59+
describe('nested 1', () => {
60+
test('test 2', () => {
61+
nestedDescribeNumbers.push(2)
62+
})
63+
64+
describe('nested 2', () => {
65+
test('test 3', () => {
66+
nestedDescribeNumbers.push(3)
67+
})
68+
69+
describe('nested 3', () => {
70+
test('test 4', () => {
71+
nestedDescribeNumbers.push(4)
72+
})
73+
}, 100)
74+
}, { repeats: 3 })
75+
})
76+
77+
afterAll(() => {
78+
expect(nestedDescribeNumbers).toStrictEqual([1, 1, 2, 2, 3, 3, 3, 4, 4, 4])
79+
})
80+
}, { repeats: 2 })

0 commit comments

Comments
 (0)
Please sign in to comment.