MCPcopy
hub / github.com/vitest-dev/vitest / generateContextFile

Function generateContextFile

packages/browser/src/node/plugins/pluginContext.ts:58–97  ·  view source on GitHub ↗
(
  this: Rollup.PluginContext,
  globalServer: ParentBrowserProject,
)

Source from the content-addressed store, hash-verified

56}
57
58async function generateContextFile(
59 this: Rollup.PluginContext,
60 globalServer: ParentBrowserProject,
61) {
62 const commands = Object.keys(globalServer.commands)
63 const provider = [...globalServer.children][0].provider
64 const providerName = provider?.name || 'preview'
65
66 const commandsCode = commands
67 .filter(command => !command.startsWith('__vitest'))
68 .map((command) => {
69 return ` ["${command}"]: (...args) => __vitest_browser_runner__.commands.triggerCommand("${command}", args),`
70 })
71 .join('\n')
72
73 const userEventNonProviderImport = await getUserEventImport(
74 provider,
75 this.resolve.bind(this),
76 )
77 const distContextPath = slash(`/@fs/${resolve(__dirname, 'context.js')}`)
78
79 return `
80import { page, createUserEvent, cdp, locators, utils } from '${distContextPath}'
81${userEventNonProviderImport}
82
83export const server = {
84 platform: ${JSON.stringify(process.platform)},
85 version: ${JSON.stringify(process.version)},
86 provider: ${JSON.stringify(providerName)},
87 browser: __vitest_browser_runner__.config.browser.name,
88 commands: {
89 ${commandsCode}
90 },
91 config: __vitest_browser_runner__.config,
92}
93export const commands = server.commands
94export const userEvent = createUserEvent(_userEventSetup)
95export { page, cdp, locators, utils }
96`
97}
98
99async function getUserEventImport(provider: BrowserProvider | undefined, resolve: (id: string, importer: string) => Promise<null | { id: string }>) {
100 if (!provider || provider.name !== 'preview') {

Callers

nothing calls this directly

Calls 6

slashFunction · 0.90
getUserEventImportFunction · 0.85
keysMethod · 0.80
bindMethod · 0.80
filterMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected