Skip to content

Commit 407f10e

Browse files
authoredDec 5, 2024··
feat: print a deprecation warning if suite or test uses object as the third argument (#7031)
1 parent 7957f91 commit 407f10e

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed
 

‎docs/guide/migration.md

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,43 @@ outline: deep
55

66
# Migration Guide
77

8+
## Migrating to Vitest 3.0
9+
10+
### Test Options as a Third Argument
11+
12+
Vitest 3.0 prints a warning if you pass down an object as a third argument to `test` or `describe` functions:
13+
14+
```ts
15+
test('validation works', () => {
16+
// ...
17+
}, { retry: 3 }) // [!code --]
18+
19+
test('validation works', { retry: 3 }, () => { // [!code ++]
20+
// ...
21+
})
22+
```
23+
24+
Vitest 4.0 will throw an error if the third argument is an object. Note that the timeout number is not deprecated:
25+
26+
```ts
27+
test('validation works', () => {
28+
// ...
29+
}, 1000) // Ok ✅
30+
```
31+
32+
### `Custom` Type is Deprecated <Badge type="warning">experimental API</Badge> {#custom-type-is-deprecated}
33+
34+
The `Custom` type is now equal to the `Test` type. Note that Vitest updated the public types in 2.1 and changed exported names to `RunnerCustomCase` and `RunnerTestCase`:
35+
36+
```ts
37+
import {
38+
RunnerCustomCase, // [!code --]
39+
RunnerTestCase, // [!code ++]
40+
} from 'vitest'
41+
```
42+
43+
If you are using `getCurrentSuite().custom()`, the `type` of the returned task is now is equal to `'test'`. The `Custom` type will be removed in Vitest 4.
44+
845
## Migrating to Vitest 2.0
946

1047
### Default Pool is `forks`

‎packages/runner/src/suite.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,9 @@ function parseArguments<T extends (...args: any[]) => any>(
253253
'Cannot use two objects as arguments. Please provide options and a function callback in that order.',
254254
)
255255
}
256-
// TODO: more info, add a name
257-
// console.warn('The third argument is deprecated. Please use the second argument for options.')
256+
console.warn(
257+
'Using an object as a third argument is deprecated. Vitest 4 will throw an error if the third argument is not a timeout number. Please use the second argument for options. See more at https://vitest.dev/guide/migration',
258+
)
258259
options = optionsOrTest
259260
}
260261
// it('', () => {}, 1000)
@@ -497,7 +498,7 @@ function createSuite() {
497498
this: Record<string, boolean | undefined>,
498499
name: string | Function,
499500
factoryOrOptions?: SuiteFactory | TestOptions,
500-
optionsOrFactory: number | TestOptions | SuiteFactory = {},
501+
optionsOrFactory?: number | TestOptions | SuiteFactory,
501502
) {
502503
const mode: RunMode = this.only
503504
? 'only'
@@ -563,7 +564,7 @@ function createSuite() {
563564

564565
const { options, handler } = parseArguments(optionsOrFn, fnOrOptions)
565566

566-
const fnFirst = typeof optionsOrFn === 'function'
567+
const fnFirst = typeof optionsOrFn === 'function' && typeof fnOrOptions === 'object'
567568

568569
cases.forEach((i, idx) => {
569570
const items = Array.isArray(i) ? i : [i]
@@ -635,7 +636,7 @@ export function createTaskCollector(
635636

636637
const { options, handler } = parseArguments(optionsOrFn, fnOrOptions)
637638

638-
const fnFirst = typeof optionsOrFn === 'function'
639+
const fnFirst = typeof optionsOrFn === 'function' && typeof fnOrOptions === 'object'
639640

640641
cases.forEach((i, idx) => {
641642
const items = Array.isArray(i) ? i : [i]

‎test/cli/fixtures/expect-soft/expects/soft.test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,13 @@ test('passed', () => {
6969
})
7070

7171
let num = 0
72-
test('retry will passed', () => {
72+
test('retry will passed', { retry: 1 }, () => {
7373
expect.soft(num += 1).toBe(3)
7474
expect.soft(num += 1).toBe(4)
75-
}, {
76-
retry: 1,
7775
})
7876

7977
num = 0
80-
test('retry will failed', () => {
78+
test('retry will failed', { retry: 1 }, () => {
8179
expect.soft(num += 1).toBe(4)
8280
expect.soft(num += 1).toBe(5)
83-
}, {
84-
retry: 1,
8581
})

0 commit comments

Comments
 (0)
Please sign in to comment.