Skip to content

Commit 8693449

Browse files
fenghan34dammy001
andauthored
feat: support describe.sequential (#3771)
Co-authored-by: Anjorin Damilare <damilareanjorin1@gmail.com>
1 parent 1762b13 commit 8693449

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

docs/api/index.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,24 @@ When running concurrent tests, Snapshots and Assertions must use `expect` from t
639639
You cannot use this syntax, when using Vitest as [type checker](/guide/testing-types).
640640
:::
641641

642+
### describe.sequential
643+
644+
- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`
645+
646+
`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.
647+
648+
```ts
649+
describe.concurrent('suite', () => {
650+
test('concurrent test 1', async () => { /* ... */ })
651+
test('concurrent test 2', async () => { /* ... */ })
652+
653+
describe.sequential('', () => {
654+
test('sequential test 1', async () => { /* ... */ })
655+
test('sequential test 2', async () => { /* ... */ })
656+
})
657+
})
658+
```
659+
642660
### describe.shuffle
643661

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

packages/runner/src/suite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function createSuiteHooks() {
5353
}
5454

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

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

87-
if (this.concurrent || concurrent || runner.config.sequence.concurrent)
87+
if (this.concurrent || (!sequential && (concurrent || runner.config.sequence.concurrent)))
8888
test.concurrent = true
8989
if (shuffle)
9090
test.shuffle = true
@@ -198,7 +198,7 @@ function createSuite() {
198198
if (currentSuite?.options)
199199
options = { ...currentSuite.options, ...options }
200200

201-
return createSuiteCollector(formatName(name), factory, mode, this.concurrent, this.shuffle, this.each, options)
201+
return createSuiteCollector(formatName(name), factory, mode, this.concurrent, this.sequence, this.shuffle, this.each, options)
202202
}
203203

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

228228
return createChainable(
229-
['concurrent', 'shuffle', 'skip', 'only', 'todo'],
229+
['concurrent', 'sequential', 'shuffle', 'skip', 'only', 'todo'],
230230
suiteFn,
231231
) as unknown as SuiteAPI
232232
}
233233

234234
function createTest(fn: (
235235
(
236-
this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined> & { fixtures?: FixtureItem[] },
236+
this: Record<'concurrent' | 'sequential' | 'skip' | 'only' | 'todo' | 'fails' | 'each', boolean | undefined> & { fixtures?: FixtureItem[] },
237237
title: string,
238238
fn?: TestFunction,
239239
options?: number | TestOptions

packages/runner/src/types/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export type Fixtures<T extends Record<string, any>, ExtraContext = {}> = {
199199
}
200200

201201
type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
202-
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
202+
'concurrent' | 'sequential' | 'only' | 'skip' | 'todo' | 'shuffle',
203203
[name: string | Function, factory?: SuiteFactory<ExtraContext>, options?: number | TestOptions],
204204
SuiteCollector<ExtraContext>,
205205
{

test/core/test/sequential.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
const delay = (timeout: number) => new Promise(resolve => setTimeout(resolve, timeout))
4+
5+
let count = 0
6+
7+
describe.concurrent('', () => {
8+
describe.sequential('', () => {
9+
test('should pass', async ({ task }) => {
10+
await delay(50)
11+
expect(task.concurrent).toBeFalsy()
12+
expect(++count).toBe(1)
13+
})
14+
15+
test('should pass', ({ task }) => {
16+
expect(task.concurrent).toBeFalsy()
17+
expect(++count).toBe(2)
18+
})
19+
})
20+
})

0 commit comments

Comments
 (0)