Skip to content

Commit

Permalink
fix: normalize mock dep, remove ?t= from path (#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 2, 2022
1 parent 1582948 commit 382aa35
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
2 changes: 2 additions & 0 deletions packages/vite-node/src/utils.ts
Expand Up @@ -20,6 +20,8 @@ export function normalizeRequestId(id: string, base?: string): string {
.replace(/^\/+/, '/') // remove duplicate leading slashes
.replace(/\?v=\w+/, '?') // remove ?v= query
.replace(/&v=\w+/, '') // remove &v= query
.replace(/\?t=\w+/, '?') // remove ?t= query
.replace(/&t=\w+/, '') // remove &t= query
.replace(/\?import/, '?') // remove ?import query
.replace(/&import/, '') // remove &import query
.replace(/\?+$/, '') // remove end query mark
Expand Down
14 changes: 7 additions & 7 deletions packages/vitest/src/runtime/mocker.ts
@@ -1,9 +1,9 @@
import { existsSync, readdirSync } from 'fs'
import { isNodeBuiltin } from 'mlly'
import { basename, dirname, resolve } from 'pathe'
import { toFilePath } from 'vite-node/utils'
import { normalizeRequestId, toFilePath } from 'vite-node/utils'
import type { ModuleCacheMap } from 'vite-node/client'
import { getWorkerState, isWindows, mergeSlashes, normalizeId } from '../utils'
import { getWorkerState, isWindows, mergeSlashes } from '../utils'
import { distDir } from '../constants'
import type { PendingSuiteMock } from '../types/mocker'
import type { ExecuteOptions } from './execute'
Expand Down Expand Up @@ -81,7 +81,7 @@ export class VitestMocker {
private async resolvePath(id: string, importer: string) {
const path = await this.options.resolveId!(id, importer)
return {
path: normalizeId(path?.id || id),
path: normalizeRequestId(path?.id || id),
external: path?.id.includes('/node_modules/') ? id : null,
}
}
Expand Down Expand Up @@ -113,22 +113,22 @@ export class VitestMocker {
}

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

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

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

return normalizeId(path.replace(this.root, ''))
return normalizeRequestId(path.replace(this.root, ''))
}

public resolveMockPath(mockPath: string, external: string | null) {
const path = normalizeId(external || mockPath)
const path = normalizeRequestId(external || mockPath)

// it's a node_module alias
// all mocks should be inside <root>/__mocks__
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/utils/index.ts
Expand Up @@ -5,7 +5,6 @@ import type { Suite, Task } from '../types'
import { getNames } from './tasks'

export * from './tasks'
export * from './path'
export * from './base'
export * from './global'
export * from './timers'
Expand Down
12 changes: 0 additions & 12 deletions packages/vitest/src/utils/path.ts

This file was deleted.

0 comments on commit 382aa35

Please sign in to comment.