diff --git a/docs/api/index.md b/docs/api/index.md index 587defd58c80..818085f43d62 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -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 diff --git a/docs/api/vi.md b/docs/api/vi.md index 14461ed79548..17268f3fbaa8 100644 --- a/docs/api/vi.md +++ b/docs/api/vi.md @@ -217,13 +217,31 @@ Type helper for TypeScript. Just returns the object that was passed. When `partial` is `true` it will expect a `Partial` 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 { + 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 }) ``` diff --git a/docs/guide/reporters.md b/docs/guide/reporters.md index 3f97088ecead..03050696eaff 100644 --- a/docs/guide/reporters.md +++ b/docs/guide/reporters.md @@ -249,7 +249,7 @@ AssertionError: expected 5 to be 4 // Object.is equality ``` -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({ diff --git a/test/core/test/vi.spec.ts b/test/core/test/vi.spec.ts index 34aac3275e6e..9e8c9b863f8c 100644 --- a/test/core/test/vi.spec.ts +++ b/test/core/test/vi.spec.ts @@ -83,6 +83,15 @@ describe('testing vi utils', () => { vi.mocked(mockFactoryAsync, { partial: true, deep: true }).mockResolvedValue({ baz: 'baz', }) + + function fetchSomething(): Promise { + 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', () => {