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

fix: can mock non-existent paths and virtual modules via __mocks__ #1298

Merged
merged 3 commits into from
May 13, 2022
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
1 change: 1 addition & 0 deletions examples/mocks/__mocks__/virtual-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'folder'
1 change: 1 addition & 0 deletions examples/mocks/__mocks__/vscode-mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const folder = true
24 changes: 24 additions & 0 deletions examples/mocks/test/virtual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @ts-expect-error vscode is not installed
import * as vscodeMocks from 'vscode-mocks'
// @ts-expect-error vscode is not installed
import * as vscodeFactory from 'vscode-factory'
// @ts-expect-error virtual module
import * as virtual from 'virtual-module'

vi.mock('vscode-mocks')
vi.mock('vscode-factory', () => {
return { factory: true }
})
vi.mock('virtual-module')

it('mocks not installed in mocks folder', () => {
expect(vscodeMocks.folder).toBe(true)
})

it('mocks not installed in mocks factory', () => {
expect(vscodeFactory.factory).toBe(true)
})

it('mocks virtual modules in mocks folder', () => {
expect(virtual.value).toBe('folder')
})
16 changes: 16 additions & 0 deletions examples/mocks/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
{
name: 'example',
resolveId(source) {
if (source === 'virtual-module')
return source
},
load(id) {
if (id === 'virtual-module') {
return `
export const value = 'original';
`
}
},
},
],
test: {
globals: true,
environment: 'node',
Expand Down
10 changes: 7 additions & 3 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ export class VitestMocker {

private async resolvePath(id: string, importer: string) {
const path = await this.options.resolveId!(id, importer)
// external is node_module or unresolved module
// for example, some people mock "vscode" and don't have it installed
const external = path == null || path.id.includes('/node_modules/') ? id : null

return {
path: normalizeRequestId(path?.id || id),
external: path?.id.includes('/node_modules/') ? id : null,
external,
}
}

Expand Down Expand Up @@ -115,15 +119,15 @@ export class VitestMocker {

// it's a node_module alias
// all mocks should be inside <root>/__mocks__
if (external || isNodeBuiltin(mockPath)) {
if (external || isNodeBuiltin(mockPath) || !existsSync(mockPath)) {
const mockDirname = dirname(path) // for nested mocks: @vueuse/integration/useJwt
const baseFilename = basename(path)
const mockFolder = resolve(this.root, '__mocks__', mockDirname)

if (!existsSync(mockFolder))
return null

const files = readdirSync(mockFolder)
const baseFilename = basename(path)

for (const file of files) {
const [basename] = file.split('.')
Expand Down