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: replace root when resolving dependency #1310

Merged
merged 1 commit into from May 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have this function in utils/base.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing function is under vitest package. Should we reference this function across packages?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't notice. I guess no, we shouldn't. Maybe sometime we can create a utils package, but not in this issue, I think

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')
})