Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: print a deprecation warning if suite or test uses object as the third argument #7031

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -255,8 +255,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 @@ -499,7 +500,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 @@ -565,7 +566,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 @@ -637,7 +638,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,
})
Loading