Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: replace root when resolving dependency (#1310)
  • Loading branch information
nieyuyao committed May 20, 2022
1 parent fa066cb commit b802d5e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/vite-node/src/client.ts
Expand Up @@ -4,7 +4,7 @@ import vm from 'vm'
import { dirname, extname, isAbsolute, resolve } from 'pathe'
import { isNodeBuiltin } from 'mlly'
import createDebug from 'debug'
import { isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import type { ModuleCache, ViteNodeRunnerOptions } from './types'

const debugExecute = createDebug('vite-node:client:execute')
Expand Down Expand Up @@ -135,7 +135,7 @@ export class ViteNodeRunner {
if (importer && importer.startsWith('mock:'))
importer = importer.slice(5)
const { id } = await this.options.resolveId(dep, importer) || {}
dep = id && isAbsolute(id) ? `/@fs/${id}` : id || dep
dep = id && isAbsolute(id) ? mergeSlashes(`/@fs/${id}`) : id || dep
}

return dep
Expand Down
4 changes: 4 additions & 0 deletions packages/vite-node/src/utils.ts
Expand Up @@ -8,6 +8,10 @@ export function slash(str: string) {
return str.replace(/\\/g, '/')
}

export function mergeSlashes(str: string) {
return str.replace(/\/\//g, '/')
}

export function normalizeRequestId(id: string, base?: string): string {
if (base && id.startsWith(base))
id = `/${id.slice(base.length)}`
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/mocker.ts
Expand Up @@ -100,7 +100,7 @@ export class VitestMocker {
}

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

public normalizePath(path: string) {
Expand Down
4 changes: 4 additions & 0 deletions test/core/src/dynamic-import.ts
@@ -0,0 +1,4 @@
export async function dynamicImport(name: string) {
const pkg = await import(name)
return pkg
}
8 changes: 8 additions & 0 deletions test/core/test/mock-internals.test.ts
Expand Up @@ -2,6 +2,7 @@ import childProcess, { exec } from 'child_process'
import timers from 'timers'
import { expect, test, vi } from 'vitest'
import { execDefault, execHelloWorld, execImportAll } from '../src/exec'
import { dynamicImport } from '../src/dynamic-import'

vi.mock('child_process')
vi.mock('timers') // node built in inside __mocks__
Expand All @@ -20,3 +21,10 @@ test('node internal is mocked', () => {
test('builtin is mocked with __mocks__ folder', () => {
expect(timers.clearInterval()).toBe('foo')
})

test('mocked dynamically imported packages', async () => {
const mod = await dynamicImport('timers')
expect(mod).toHaveProperty('default')
expect(mod.default).toHaveProperty('clearInterval')
expect(mod.default.clearInterval()).toBe('foo')
})

0 comments on commit b802d5e

Please sign in to comment.