| 56 | } |
| 57 | |
| 58 | async 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 ` |
| 80 | import { page, createUserEvent, cdp, locators, utils } from '${distContextPath}' |
| 81 | ${userEventNonProviderImport} |
| 82 | |
| 83 | export 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 | } |
| 93 | export const commands = server.commands |
| 94 | export const userEvent = createUserEvent(_userEventSetup) |
| 95 | export { page, cdp, locators, utils } |
| 96 | ` |
| 97 | } |
| 98 | |
| 99 | async function getUserEventImport(provider: BrowserProvider | undefined, resolve: (id: string, importer: string) => Promise<null | { id: string }>) { |
| 100 | if (!provider || provider.name !== 'preview') { |