From 382aa351421d378c4b16301b7d94184e061e9a72 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 2 Apr 2022 19:11:19 +0300 Subject: [PATCH] fix: normalize mock dep, remove ?t= from path (#1080) --- packages/vite-node/src/utils.ts | 2 ++ packages/vitest/src/runtime/mocker.ts | 14 +++++++------- packages/vitest/src/utils/index.ts | 1 - packages/vitest/src/utils/path.ts | 12 ------------ 4 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 packages/vitest/src/utils/path.ts diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index 893ffcb1f3d4..aa51574d568c 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -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 diff --git a/packages/vitest/src/runtime/mocker.ts b/packages/vitest/src/runtime/mocker.ts index e37ef1de5813..cca3e66746a5 100644 --- a/packages/vitest/src/runtime/mocker.ts +++ b/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' @@ -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, } } @@ -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 /__mocks__ diff --git a/packages/vitest/src/utils/index.ts b/packages/vitest/src/utils/index.ts index fa834265ec9e..af4a070581b1 100644 --- a/packages/vitest/src/utils/index.ts +++ b/packages/vitest/src/utils/index.ts @@ -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' diff --git a/packages/vitest/src/utils/path.ts b/packages/vitest/src/utils/path.ts deleted file mode 100644 index 89753887c6fa..000000000000 --- a/packages/vitest/src/utils/path.ts +++ /dev/null @@ -1,12 +0,0 @@ -export function normalizeId(id: string, base?: string): string { - if (base && id.startsWith(base)) - id = `/${id.slice(base.length)}` - - return id - .replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0` - .replace(/^\/@id\//, '') - .replace(/^__vite-browser-external:/, '') - .replace(/^node:/, '') - .replace(/[?&]v=\w+/, '?') // remove ?v= query - .replace(/\?$/, '') // remove end query mark -}