Skip to content

Commit

Permalink
feat: add test.sequential() api (#4512)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyddall committed Nov 16, 2023
1 parent 2d1b478 commit c3619c7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/api/index.md
Expand Up @@ -224,6 +224,30 @@ test.concurrent('test 2', async ({ expect }) => {
You cannot use this syntax, when using Vitest as [type checker](/guide/testing-types).
:::

### test.sequential

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

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

```ts
// with config option { sequence: { concurrent: true } }
test('concurrent test 1', async () => { /* ... */ })
test('concurrent test 2', async () => { /* ... */ })

test.sequential('sequential test 1', async () => { /* ... */ })
test.sequential('sequential test 2', async () => { /* ... */ })

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

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

### test.todo

- **Type:** `(name: string | Function) => void`
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/suite.ts
Expand Up @@ -280,7 +280,7 @@ export function createTaskCollector(
}

const _test = createChainable(
['concurrent', 'skip', 'only', 'todo', 'fails'],
['concurrent', 'sequential', 'skip', 'only', 'todo', 'fails'],
taskFn,
) as CustomAPI

Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/types/tasks.ts
Expand Up @@ -150,7 +150,7 @@ interface TestEachFunction {
}

type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
'concurrent' | 'sequential' | 'only' | 'skip' | 'todo' | 'fails',
[name: string | Function, fn?: TestFunction<ExtraContext>, options?: number | TestOptions],
void,
{
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/sequential-sequence-concurrent.test.ts
Expand Up @@ -22,3 +22,14 @@ describe.sequential('running sequential suite when sequence.concurrent is true',
expect(++count).toBe(2)
})
})

test.sequential('third test completes third', async ({ task }) => {
await delay(50)
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(3)
})

test.sequential('fourth test completes fourth', ({ task }) => {
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(4)
})
11 changes: 11 additions & 0 deletions test/core/test/sequential.test.ts
Expand Up @@ -41,6 +41,17 @@ function assertConcurrent() {
expect(task.concurrent).toBe(true)
expect(++count).toBe(1)
})

test.sequential('third test completes third', async ({ task }) => {
await delay(50)
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(3)
})

test.sequential('fourth test completes fourth', ({ task }) => {
expect(task.concurrent).toBeFalsy()
expect(++count).toBe(4)
})
}

assertSequential()
Expand Down

0 comments on commit c3619c7

Please sign in to comment.