From dda62d0af8e08436d5f53b739d45ec1eb5d47ba9 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 17 Jan 2022 14:58:58 +0300 Subject: [PATCH] feat: vi.mock can be used inside setupFiles to automock libraries --- docs/api/index.md | 12 ++++++------ test/core/src/global-mock.ts | 1 + test/core/test/mocked.test.ts | 5 +++++ test/core/test/setup.ts | 4 +++- 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 test/core/src/global-mock.ts diff --git a/docs/api/index.md b/docs/api/index.md index 20c446f93..82337cd44 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -1086,16 +1086,16 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo ### vi.mock - **Type**: `(path: string, factory?: () => any) => void` + **Type**: `(path: string, factory?: () => unknown) => void` Makes all `imports` to passed module to be mocked. Inside a path you _can_ use configured Vite aliases. - - If there is a `factory`, will return its result. The call to `vi.mock` is hoisted to the top of the file, - so you don't have access to variables declared in the global file scope, if you didn't put them before imports! - - If `__mocks__` folder with file of the same name exist, all imports will return its exports. - - If there is no `__mocks__` folder or a file with the same name inside, will call original module and mock it. + - If `factory` is defined, will return its result. Factory function can be asynchronous. You may call [`vi.importActual`](#vi-importactual) inside to get the original module. The call to `vi.mock` is hoisted to the top of the file, so you don't have access to variables declared in the global file scope! + - If `__mocks__` folder with file of the same name exist, all imports will return its exports. For example, `vi.mock('axios')` with `/__mocks__/axios.ts` folder will return everything exported from `axios.ts`. + - If there is no `__mocks__` folder or a file with the same name inside, will call original module and mock it. (For the rules applied, see [algorithm](/guide/mocking#automocking-algorithm).) + + Additionally, unlike Jest, mocked modules in `/__mocks__` are not loaded unless `vi.mock()` is called. If you need them to be mocked in every test, like in Jest, you can mock them inside [`setupFiles`](/config/#setupfiles). -Additionally, unlike Jest, mocked modules in `__mocks__` are not loaded unless `vi.mock()` is called. ### vi.mockCurrentDate - **Type**: `(date: string | number | Date) => void` diff --git a/test/core/src/global-mock.ts b/test/core/src/global-mock.ts new file mode 100644 index 000000000..f0b5ec62b --- /dev/null +++ b/test/core/src/global-mock.ts @@ -0,0 +1 @@ +export const mocked = false diff --git a/test/core/test/mocked.test.ts b/test/core/test/mocked.test.ts index 59e55cebe..6880b5179 100644 --- a/test/core/test/mocked.test.ts +++ b/test/core/test/mocked.test.ts @@ -4,6 +4,7 @@ import { value as virtualValue } from 'virtual-module' import { two } from '../src/submodule' import * as mocked from '../src/mockedA' import { mockedB } from '../src/mockedB' +import * as globalMock from '../src/global-mock' vitest.mock('../src/submodule') vitest.mock('virtual-module', () => { @@ -14,6 +15,10 @@ test('submodule is mocked to return "two" as 3', () => { assert.equal(3, two) }) +test('globally mocked files are mocked', () => { + expect(globalMock.mocked).toBe(true) +}) + test('can mock esm', () => { const spy = vi.spyOn(mocked, 'mockedA') diff --git a/test/core/test/setup.ts b/test/core/test/setup.ts index 4db67978f..f9a460bb2 100644 --- a/test/core/test/setup.ts +++ b/test/core/test/setup.ts @@ -1,4 +1,6 @@ -import { beforeEach } from 'vitest' +import { beforeEach, vi } from 'vitest' + +vi.mock('../src/global-mock', () => ({ mocked: true })) beforeEach(() => { // console.log(`hi ${s.name}`) -- 2.51.2