Skip to content

Commit

Permalink
feat: print a deprecation warning if suite or test uses object as the…
Browse files Browse the repository at this point in the history
… third argument (#7031)
  • Loading branch information
sheremet-va authored Dec 5, 2024
1 parent 7957f91 commit 407f10e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
37 changes: 37 additions & 0 deletions docs/guide/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,43 @@ outline: deep

# Migration Guide

## Migrating to Vitest 3.0

### Test Options as a Third Argument

Vitest 3.0 prints a warning if you pass down an object as a third argument to `test` or `describe` functions:

```ts
test('validation works', () => {
// ...
}, { retry: 3 }) // [!code --]

test('validation works', { retry: 3 }, () => { // [!code ++]
// ...
})
```

Vitest 4.0 will throw an error if the third argument is an object. Note that the timeout number is not deprecated:

```ts
test('validation works', () => {
// ...
}, 1000) // Ok ✅
```

### `Custom` Type is Deprecated <Badge type="warning">experimental API</Badge> {#custom-type-is-deprecated}

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`:

```ts
import {
RunnerCustomCase, // [!code --]
RunnerTestCase, // [!code ++]
} from 'vitest'
```

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.

## Migrating to Vitest 2.0

### Default Pool is `forks`
Expand Down
11 changes: 6 additions & 5 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ function parseArguments<T extends (...args: any[]) => any>(
'Cannot use two objects as arguments. Please provide options and a function callback in that order.',
)
}
// TODO: more info, add a name
// console.warn('The third argument is deprecated. Please use the second argument for options.')
console.warn(
'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',
)
options = optionsOrTest
}
// it('', () => {}, 1000)
Expand Down Expand Up @@ -497,7 +498,7 @@ function createSuite() {
this: Record<string, boolean | undefined>,
name: string | Function,
factoryOrOptions?: SuiteFactory | TestOptions,
optionsOrFactory: number | TestOptions | SuiteFactory = {},
optionsOrFactory?: number | TestOptions | SuiteFactory,
) {
const mode: RunMode = this.only
? 'only'
Expand Down Expand Up @@ -563,7 +564,7 @@ function createSuite() {

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

const fnFirst = typeof optionsOrFn === 'function'
const fnFirst = typeof optionsOrFn === 'function' && typeof fnOrOptions === 'object'

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

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

const fnFirst = typeof optionsOrFn === 'function'
const fnFirst = typeof optionsOrFn === 'function' && typeof fnOrOptions === 'object'

cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
Expand Down
8 changes: 2 additions & 6 deletions test/cli/fixtures/expect-soft/expects/soft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ test('passed', () => {
})

let num = 0
test('retry will passed', () => {
test('retry will passed', { retry: 1 }, () => {
expect.soft(num += 1).toBe(3)
expect.soft(num += 1).toBe(4)
}, {
retry: 1,
})

num = 0
test('retry will failed', () => {
test('retry will failed', { retry: 1 }, () => {
expect.soft(num += 1).toBe(4)
expect.soft(num += 1).toBe(5)
}, {
retry: 1,
})

0 comments on commit 407f10e

Please sign in to comment.