forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginContext.ts
More file actions
88 lines (79 loc) · 2.8 KB
/
pluginContext.ts
File metadata and controls
88 lines (79 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type { Rollup } from 'vite'
import type { Plugin } from 'vitest/config'
import type { ParentBrowserProject } from '../projectParent'
import { fileURLToPath } from 'node:url'
import { slash } from '@vitest/utils'
import { dirname, resolve } from 'pathe'
const VIRTUAL_ID_CONTEXT = '\0@vitest/browser/context'
const ID_CONTEXT = '@vitest/browser/context'
const __dirname = dirname(fileURLToPath(import.meta.url))
export default function BrowserContext(globalServer: ParentBrowserProject): Plugin {
return {
name: 'vitest:browser:virtual-module:context',
enforce: 'pre',
resolveId(id) {
if (id === ID_CONTEXT) {
return VIRTUAL_ID_CONTEXT
}
},
load(id) {
if (id === VIRTUAL_ID_CONTEXT) {
return generateContextFile.call(this, globalServer)
}
},
}
}
async function generateContextFile(
this: Rollup.PluginContext,
globalServer: ParentBrowserProject,
) {
const commands = Object.keys(globalServer.commands)
const filepathCode
= '__vitest_worker__.filepath || __vitest_worker__.current?.file?.filepath || undefined'
const provider = [...globalServer.children][0].provider || { name: 'preview' }
const providerName = provider.name
const commandsCode = commands
.filter(command => !command.startsWith('__vitest'))
.map((command) => {
return ` ["${command}"]: (...args) => rpc().triggerCommand(sessionId, "${command}", filepath(), args),`
})
.join('\n')
const userEventNonProviderImport = await getUserEventImport(
providerName,
this.resolve.bind(this),
)
const distContextPath = slash(`/@fs/${resolve(__dirname, 'context.js')}`)
return `
import { page, createUserEvent, cdp } from '${distContextPath}'
${userEventNonProviderImport}
const filepath = () => ${filepathCode}
const rpc = () => __vitest_worker__.rpc
const sessionId = __vitest_browser_runner__.sessionId
export const server = {
platform: ${JSON.stringify(process.platform)},
version: ${JSON.stringify(process.version)},
provider: ${JSON.stringify(providerName)},
browser: __vitest_browser_runner__.config.browser.name,
commands: {
${commandsCode}
},
config: __vitest_browser_runner__.config,
}
export const commands = server.commands
export const userEvent = createUserEvent(_userEventSetup)
export { page, cdp }
`
}
async function getUserEventImport(provider: string, resolve: (id: string, importer: string) => Promise<null | { id: string }>) {
if (provider !== 'preview') {
return 'const _userEventSetup = undefined'
}
const resolved = await resolve('@testing-library/user-event', __dirname)
if (!resolved) {
throw new Error(`Failed to resolve user-event package from ${__dirname}`)
}
return `\
import { userEvent as __vitest_user_event__ } from '${slash(`/@fs/${resolved.id}`)}'
const _userEventSetup = __vitest_user_event__
`
}