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(browser): failing to load vitest/utils #3190

Merged
merged 4 commits into from May 3, 2023
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/browser/src/client/main.ts
Expand Up @@ -22,7 +22,7 @@ export const ENTRY_URL = `${

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

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

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

for (const file of files)
await startTests([file], runner)
Expand Down
19 changes: 10 additions & 9 deletions packages/browser/src/client/runner.ts
Expand Up @@ -4,7 +4,7 @@ import type { ResolvedConfig } from '#types'

interface BrowserRunnerOptions {
config: ResolvedConfig
browserHashMap: Map<string, string>
browserHashMap: Map<string, [test: boolean, timstamp: string]>
}

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

constructor(options: BrowserRunnerOptions) {
super(options.config)
Expand Down Expand Up @@ -54,15 +54,16 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl
}

async importFile(filepath: string) {
const match = filepath.match(/^(\w:\/)/)
let hash = this.hashMap.get(filepath)
if (!hash) {
let [test, hash] = this.hashMap.get(filepath) ?? [false, '']
if (hash === '') {
hash = Date.now().toString()
this.hashMap.set(filepath, hash)
this.hashMap.set(filepath, [false, hash])
}
const importpath = match
? `/@fs/${filepath.slice(match[1].length)}?v=${hash}`
: `${filepath}?v=${hash}`

// on Windows we need the unit to resolve the test file
const importpath = /^\w:/.test(filepath)
? `/@fs/${filepath}?${test ? 'browserv' : 'v'}=${hash}`
: `${filepath}?${test ? 'browserv' : 'v'}=${hash}`
await import(importpath)
}
}
Expand Down
31 changes: 28 additions & 3 deletions packages/browser/src/node/index.ts
Expand Up @@ -41,13 +41,38 @@ export default (project: any, base = '/'): Plugin[] => {
config() {
return {
optimizeDeps: {
exclude: [...polyfills, ...builtinModules],
exclude: [
...polyfills,
...builtinModules,
'vitest',
'vitest/utils',
'vitest/browser',
'vitest/runners',
'@vitest/utils',
],
include: [
'@vitest/utils > concordance',
'@vitest/utils > loupe',
'@vitest/utils > pretty-format',
'vitest > chai',
],
},
}
},
async resolveId(id) {
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:'))
return
if (!builtinModules.includes(id) && !polyfills.includes(id) && !id.startsWith('node:')) {
if (!/\?browserv=\w+$/.test(id))
return

let useId = id.slice(0, id.lastIndexOf('?'))
if (useId.startsWith('/@fs/'))
useId = useId.slice(5)

if (/^\w:/.test(useId))
useId = useId.replace(/\\/g, '/')

return useId
}

id = normalizeId(id)
return { id: await polyfillPath(id), moduleSideEffects: false }
Expand Down