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: test repeats #3369

Merged
merged 2 commits into from May 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/runner/src/suite.ts
Expand Up @@ -53,15 +53,15 @@ 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)[] = []

let suite: Suite

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')
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -122,6 +123,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
type: 'collector',
name,
mode,
options: suiteOptions,
test,
tasks,
collect,
Expand Down Expand Up @@ -186,6 +188,16 @@ function createSuite() {
function suiteFn(this: Record<string, boolean | undefined>, 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)
}

Expand Down
7 changes: 5 additions & 2 deletions packages/runner/src/types/tasks.ts
Expand Up @@ -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
}
Expand Down Expand Up @@ -211,6 +213,7 @@ export interface SuiteHooks<ExtraContext = {}> {
export interface SuiteCollector<ExtraContext = {}> {
readonly name: string
readonly mode: RunMode
options?: TestOptions
type: 'collector'
test: TestAPI<ExtraContext>
tasks: (Suite | TaskCustom | Test | SuiteCollector<ExtraContext>)[]
Expand Down
30 changes: 30 additions & 0 deletions test/core/test/repeats.test.ts
Expand Up @@ -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 })