From cbcda6ac42b2558be483c2942cd220bb73297cae Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 9 Feb 2022 12:01:15 +0300 Subject: [PATCH 1/6] fix: can mock workspace files --- packages/vitest/src/node/mocker.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index 8001c95e39e4..5b4c03127678 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -1,6 +1,6 @@ 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' @@ -99,7 +99,7 @@ export class VitestMocker { 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) })) @@ -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\//, '/') + } - return normalizeId(dep) + public normalizePath(path: string) { + return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, '/') } - public getActualPath(path: string, external: string | null) { + public getFsPath(path: string, external: string | null) { if (external) return mergeSlashes(`/@fs/${path}`) @@ -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] @@ -206,7 +206,7 @@ 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) @@ -214,7 +214,7 @@ export class VitestMocker { public async importActual(id: string, importer: string): Promise { 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 } @@ -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) } From 27f13f3c7321baca4e6fe6823e8fe1873d85c8e5 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 9 Feb 2022 12:14:19 +0300 Subject: [PATCH 2/6] test: fix windows error --- packages/vitest/src/node/mocker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index 5b4c03127678..1108f95a7fd9 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -122,11 +122,11 @@ export class VitestMocker { } public resolveDependency(dep: string) { - return normalizeId(dep).replace(/^\/@fs\//, '/') + return normalizeId(dep).replace(/^\/@fs\//, '') } public normalizePath(path: string) { - return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, '/') + return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, '') } public getFsPath(path: string, external: string | null) { From e517152bf9cab6cbccdca1eb0ce06f4df3eecf80 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 9 Feb 2022 12:33:07 +0300 Subject: [PATCH 3/6] fix: remove /@fs/ only on windows --- packages/vitest/src/node/mocker.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index 1108f95a7fd9..2ddd3e525ced 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -3,7 +3,7 @@ import { isNodeBuiltin } from 'mlly' 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' @@ -91,7 +91,7 @@ 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, } } @@ -122,11 +122,11 @@ export class VitestMocker { } public resolveDependency(dep: string) { - return normalizeId(dep).replace(/^\/@fs\//, '') + return normalizeId(dep).replace(/^\/@fs\//, isWindows ? '' : '/') } public normalizePath(path: string) { - return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, '') + return normalizeId(path.replace(this.root, '')).replace(/^\/@fs\//, isWindows ? '' : '/') } public getFsPath(path: string, external: string | null) { @@ -208,6 +208,8 @@ export class VitestMocker { const fsPath = this.normalizePath(path) + console.log('mock', path, fsPath) + this.mockMap[suitefile] ??= {} this.mockMap[suitefile][fsPath] = factory || this.resolveMockPath(path, external) } From 2596b05275d78bae20384a73177128ebe66c6a51 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 9 Feb 2022 12:35:20 +0300 Subject: [PATCH 4/6] chore: remove forgotten console.log :S --- packages/vitest/src/node/mocker.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index 2ddd3e525ced..a1492682a846 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -208,8 +208,6 @@ export class VitestMocker { const fsPath = this.normalizePath(path) - console.log('mock', path, fsPath) - this.mockMap[suitefile] ??= {} this.mockMap[suitefile][fsPath] = factory || this.resolveMockPath(path, external) } From 4fdac411366d708ea5b5eac658c471229a6c5be6 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 9 Feb 2022 12:44:22 +0300 Subject: [PATCH 5/6] chore: fix external as string --- packages/vitest/src/node/mocker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index a1492682a846..e571becc82bc 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -91,7 +91,7 @@ export class VitestMocker { const path = await this.options.resolveId(id, importer) return { path: normalizeId(path?.id || id), - external: path?.external ?? path?.id.includes('/node_modules/') ? id : null, + external: path?.external || path?.id.includes('/node_modules/') ? id : null, } } From bab285946a9e5b122acfd3510e4e5e0c9ae7cb6a Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 9 Feb 2022 13:12:01 +0300 Subject: [PATCH 6/6] chore: don't force storing external in root if it's not in node_modules --- packages/vitest/src/node/mocker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/mocker.ts b/packages/vitest/src/node/mocker.ts index e571becc82bc..f83c2f86134f 100644 --- a/packages/vitest/src/node/mocker.ts +++ b/packages/vitest/src/node/mocker.ts @@ -91,7 +91,7 @@ export class VitestMocker { const path = await this.options.resolveId(id, importer) return { path: normalizeId(path?.id || id), - external: path?.external || path?.id.includes('/node_modules/') ? id : null, + external: path?.id.includes('/node_modules/') ? id : null, } }