Skip to content

Commit

Permalink
Merge branch 'main' into rework-snapshot-state
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Nov 4, 2024
2 parents 671344f + 5d4b382 commit eae4ed7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
9 changes: 0 additions & 9 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ interface TestOptions {
}
```

Vitest 1.3.0 deprecates the use of options as the last parameter. You will see a deprecation message until 2.0.0 when this syntax will be removed. If you need to pass down options, use `test` function's second argument:

```ts
import { test } from 'vitest'

test('flaky test', () => {}, { retry: 3 }) // [!code --]
test('flaky test', { retry: 3 }, () => {}) // [!code ++]
```

When a test function returns a promise, the runner will wait until it is resolved to collect async expectations. If the promise is rejected, the test will fail.

::: tip
Expand Down
26 changes: 22 additions & 4 deletions docs/api/vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,31 @@ Type helper for TypeScript. Just returns the object that was passed.
When `partial` is `true` it will expect a `Partial<T>` as a return value. By default, this will only make TypeScript believe that the first level values are mocked. You can pass down `{ deep: true }` as a second argument to tell TypeScript that the whole object is mocked, if it actually is.

```ts
import example from './example.js'
// example.ts
export function add(x: number, y: number): number {
return x + y
}

export function fetchSomething(): Promise<Response> {
return fetch('https://vitest.dev/')
}
```

vi.mock('./example.js')
```ts
// example.test.ts
import * as example from './example'

vi.mock('./example')

test('1 + 1 equals 10', async () => {
vi.mocked(example.calc).mockReturnValue(10)
expect(example.calc(1, '+', 1)).toBe(10)
vi.mocked(example.add).mockReturnValue(10)
expect(example.add(1, 1)).toBe(10)
})

test('mock return value with only partially correct typing', async () => {
vi.mocked(example.fetchSomething).mockResolvedValue(new Response('hello'))
vi.mocked(example.fetchSomething, { partial: true }).mockResolvedValue({ ok: false })
// vi.mocked(example.someFn).mockResolvedValue({ ok: false }) // this is a type error
})
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ AssertionError: expected 5 to be 4 // Object.is equality
</testsuites>
```

The outputted XML contains nested `testsuites` and `testcase` tags. You can use the environment variables `VITEST_JUNIT_SUITE_NAME` and `VITEST_JUNIT_CLASSNAME` to configure their `name` and `classname` attributes, respectively. These can also be customized via reporter options:
The outputted XML contains nested `testsuites` and `testcase` tags. You can use the reporter options to configure these attributes:

```ts
export default defineConfig({
Expand Down
9 changes: 9 additions & 0 deletions test/core/test/vi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ describe('testing vi utils', () => {
vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({
baz: 'baz',
})

function fetchSomething(): Promise<Response> {
return fetch('https://vitest.dev/')
};
if (0) {
// type check only
vi.mocked(fetchSomething).mockResolvedValue(new Response(null))
vi.mocked(fetchSomething, { partial: true }).mockResolvedValue({ ok: false })
}
})

test('vi.fn and Mock type', () => {
Expand Down

0 comments on commit eae4ed7

Please sign in to comment.