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 workspace files #712

Merged
merged 6 commits into from Feb 9, 2022
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions packages/vitest/src/node/mocker.ts
@@ -1,9 +1,9 @@
import { existsSync, readdirSync } from 'fs'
import { isNodeBuiltin } from 'mlly'
import { basename, dirname, join, resolve } from 'pathe'
import { basename, dirname, resolve } from 'pathe'
import type { ModuleCache } from 'vite-node'
import { toFilePath } from 'vite-node/utils'
import { mergeSlashes, normalizeId } from '../utils'
import { isWindows, mergeSlashes, normalizeId } from '../utils'
import { distDir } from '../constants'
import type { ExecuteOptions } from './execute'

Expand Down Expand Up @@ -91,15 +91,15 @@ export class VitestMocker {
const path = await this.options.resolveId(id, importer)
return {
path: normalizeId(path?.id || id),
external: path?.id.includes('/node_modules/') ? id : null,
external: path?.external || path?.id.includes('/node_modules/') ? id : null,
Demivan marked this conversation as resolved.
Show resolved Hide resolved
}
}

private async resolveMocks() {
await Promise.all(pendingIds.map(async(mock) => {
const { path, external } = await this.resolvePath(mock.id, mock.importer)
if (mock.type === 'unmock')
this.unmockPath(path, external)
this.unmockPath(path)
if (mock.type === 'mock')
this.mockPath(path, external, mock.factory)
}))
Expand All @@ -121,15 +121,15 @@ export class VitestMocker {
return this.getMocks()[this.resolveDependency(dep)]
}

// npm resolves as /node_modules, but we store as /@fs/.../node_modules
public resolveDependency(dep: string) {
if (dep.startsWith('/node_modules/'))
dep = mergeSlashes(`/@fs/${join(this.root, dep)}`)
return normalizeId(dep).replace(/^\/@fs\//, isWindows ? '' : '/')
}

return normalizeId(dep)
public normalizePath(path: string) {
return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, isWindows ? '' : '/')
}

public getActualPath(path: string, external: string | null) {
public getFsPath(path: string, external: string | null) {
if (external)
return mergeSlashes(`/@fs/${path}`)

Expand Down Expand Up @@ -194,10 +194,10 @@ export class VitestMocker {
return newObj
}

public unmockPath(path: string, external: string | null) {
public unmockPath(path: string) {
const suitefile = this.getSuiteFilepath()

const fsPath = this.getActualPath(path, external)
const fsPath = this.normalizePath(path)

if (this.mockMap[suitefile]?.[fsPath])
delete this.mockMap[suitefile][fsPath]
Expand All @@ -206,15 +206,15 @@ export class VitestMocker {
public mockPath(path: string, external: string | null, factory?: () => any) {
const suitefile = this.getSuiteFilepath()

const fsPath = this.getActualPath(path, external)
const fsPath = this.normalizePath(path)

this.mockMap[suitefile] ??= {}
this.mockMap[suitefile][fsPath] = factory || this.resolveMockPath(path, external)
}

public async importActual<T>(id: string, importer: string): Promise<T> {
const { path, external } = await this.resolvePath(id, importer)
const fsPath = this.getActualPath(path, external)
const fsPath = this.getFsPath(path, external)
const result = await this.request(fsPath)
return result as T
}
Expand All @@ -229,7 +229,7 @@ export class VitestMocker {

if (mock === null) {
await this.ensureSpy()
const fsPath = this.getActualPath(path, external)
const fsPath = this.getFsPath(path, external)
const mod = await this.request(fsPath)
return this.mockObject(mod)
}
Expand Down