Skip to content

Commit 78bad4a

Browse files
authoredMay 3, 2023
fix(browser): failing to load vitest/utils (#3190)
1 parent 036de79 commit 78bad4a

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed
 

‎packages/browser/src/client/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const ENTRY_URL = `${
2222

2323
let config: ResolvedConfig | undefined
2424
let runner: VitestRunner | undefined
25-
const browserHashMap = new Map<string, string>()
25+
const browserHashMap = new Map<string, [test: boolean, timestamp: string]>()
2626

2727
const url = new URL(location.href)
2828
const testId = url.searchParams.get('id') || 'unknown'
@@ -130,7 +130,7 @@ async function runTests(paths: string[], config: ResolvedConfig) {
130130
})
131131

132132
const now = `${new Date().getTime()}`
133-
files.forEach(i => browserHashMap.set(i, now))
133+
files.forEach(i => browserHashMap.set(i, [true, now]))
134134

135135
for (const file of files)
136136
await startTests([file], runner)

‎packages/browser/src/client/runner.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ResolvedConfig } from '#types'
44

55
interface BrowserRunnerOptions {
66
config: ResolvedConfig
7-
browserHashMap: Map<string, string>
7+
browserHashMap: Map<string, [test: boolean, timstamp: string]>
88
}
99

1010
interface CoverageHandler {
@@ -14,7 +14,7 @@ interface CoverageHandler {
1414
export function createBrowserRunner(original: any, coverageModule: CoverageHandler | null) {
1515
return class BrowserTestRunner extends original {
1616
public config: ResolvedConfig
17-
hashMap = new Map<string, string>()
17+
hashMap = new Map<string, [test: boolean, timstamp: string]>()
1818

1919
constructor(options: BrowserRunnerOptions) {
2020
super(options.config)
@@ -54,15 +54,16 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl
5454
}
5555

5656
async importFile(filepath: string) {
57-
const match = filepath.match(/^(\w:\/)/)
58-
let hash = this.hashMap.get(filepath)
59-
if (!hash) {
57+
let [test, hash] = this.hashMap.get(filepath) ?? [false, '']
58+
if (hash === '') {
6059
hash = Date.now().toString()
61-
this.hashMap.set(filepath, hash)
60+
this.hashMap.set(filepath, [false, hash])
6261
}
63-
const importpath = match
64-
? `/@fs/${filepath.slice(match[1].length)}?v=${hash}`
65-
: `${filepath}?v=${hash}`
62+
63+
// on Windows we need the unit to resolve the test file
64+
const importpath = /^\w:/.test(filepath)
65+
? `/@fs/${filepath}?${test ? 'browserv' : 'v'}=${hash}`
66+
: `${filepath}?${test ? 'browserv' : 'v'}=${hash}`
6667
await import(importpath)
6768
}
6869
}

‎packages/browser/src/node/index.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,38 @@ export default (project: any, base = '/'): Plugin[] => {
4141
config() {
4242
return {
4343
optimizeDeps: {
44-
exclude: [...polyfills, ...builtinModules],
44+
exclude: [
45+
...polyfills,
46+
...builtinModules,
47+
'vitest',
48+
'vitest/utils',
49+
'vitest/browser',
50+
'vitest/runners',
51+
'@vitest/utils',
52+
],
53+
include: [
54+
'@vitest/utils > concordance',
55+
'@vitest/utils > loupe',
56+
'@vitest/utils > pretty-format',
57+
'vitest > chai',
58+
],
4559
},
4660
}
4761
},
4862
async resolveId(id) {
49-
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:'))
50-
return
63+
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) {
64+
if (!/\?browserv=\w+$/.test(id))
65+
return
66+
67+
let useId = id.slice(0, id.lastIndexOf('?'))
68+
if (useId.startsWith('/@fs/'))
69+
useId = useId.slice(5)
70+
71+
if (/^\w:/.test(useId))
72+
useId = useId.replace(/\\/g, '/')
73+
74+
return useId
75+
}
5176

5277
id = normalizeId(id)
5378
return { id: await polyfillPath(id), moduleSideEffects: false }

0 commit comments

Comments
 (0)
Please sign in to comment.