Skip to content

Commit

Permalink
feat: support describe.sequential (#3771)
Browse files Browse the repository at this point in the history
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
  • Loading branch information
fenghan34 and dammy001 authored Jul 19, 2023
1 parent 1762b13 commit 8693449
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
18 changes: 18 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,24 @@ When running concurrent tests, Snapshots and Assertions must use `expect` from t
You cannot use this syntax, when using Vitest as [type checker](/guide/testing-types).
:::

### describe.sequential

- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`

`describe.sequential` in a suite marks every test as sequential. This is useful if you want to run tests in sequential within `describe.concurrent` or with the `--sequence.concurrent` command option.

```ts
describe.concurrent('suite', () => {
test('concurrent test 1', async () => { /* ... */ })
test('concurrent test 2', async () => { /* ... */ })
describe.sequential('', () => {
test('sequential test 1', async () => { /* ... */ })
test('sequential test 2', async () => { /* ... */ })
})
})
```

### describe.shuffle

- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`
Expand Down
10 changes: 5 additions & 5 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function createSuiteHooks() {
}

// implementations
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, each?: boolean, suiteOptions?: TestOptions) {
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, sequential?: boolean, shuffle?: boolean, each?: boolean, suiteOptions?: TestOptions) {
const tasks: (Test | TaskCustom | Suite | SuiteCollector)[] = []
const factoryQueue: (Test | Suite | SuiteCollector)[] = []

Expand Down Expand Up @@ -84,7 +84,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
meta: Object.create(null),
} as Omit<Test, 'context'> as Test

if (this.concurrent || concurrent || runner.config.sequence.concurrent)
if (this.concurrent || (!sequential && (concurrent || runner.config.sequence.concurrent)))
test.concurrent = true
if (shuffle)
test.shuffle = true
Expand Down Expand Up @@ -198,7 +198,7 @@ function createSuite() {
if (currentSuite?.options)
options = { ...currentSuite.options, ...options }

return createSuiteCollector(formatName(name), factory, mode, this.concurrent, this.shuffle, this.each, options)
return createSuiteCollector(formatName(name), factory, mode, this.concurrent, this.sequence, this.shuffle, this.each, options)
}

suiteFn.each = function<T>(this: { withContext: () => SuiteAPI; setContext: (key: string, value: boolean | undefined) => SuiteAPI }, cases: ReadonlyArray<T>, ...args: any[]) {
Expand Down Expand Up @@ -226,14 +226,14 @@ function createSuite() {
suiteFn.runIf = (condition: any) => (condition ? suite : suite.skip) as SuiteAPI

return createChainable(
['concurrent', 'shuffle', 'skip', 'only', 'todo'],
['concurrent', 'sequential', 'shuffle', 'skip', 'only', 'todo'],
suiteFn,
) as unknown as SuiteAPI
}

function createTest(fn: (
(
this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined> & { fixtures?: FixtureItem[] },
this: Record<'concurrent' | 'sequential' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined> & { fixtures?: FixtureItem[] },
title: string,
fn?: TestFunction,
options?: number | TestOptions
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export type Fixtures<T extends Record<string, any>, ExtraContext = {}> = {
}

type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
'concurrent' | 'sequential' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string | Function, factory?: SuiteFactory<ExtraContext>, options?: number | TestOptions],
SuiteCollector<ExtraContext>,
{
Expand Down
20 changes: 20 additions & 0 deletions test/core/test/sequential.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from 'vitest'

const delay = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout))

let count = 0

describe.concurrent('', () => {
describe.sequential('', () => {
test('should pass', async ({ task }) => {
await delay(50)
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(1)
})

test('should pass', ({ task }) => {
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(2)
})
})
})

0 comments on commit 8693449

Please sign in to comment.