( globalServer: ParentBrowserProject, url: URL, res: ServerResponse<IncomingMessage>, next: Connect.NextFunction, )
| 7 | import { replacer } from './utils' |
| 8 | |
| 9 | export async function resolveTester( |
| 10 | globalServer: ParentBrowserProject, |
| 11 | url: URL, |
| 12 | res: ServerResponse<IncomingMessage>, |
| 13 | next: Connect.NextFunction, |
| 14 | ): Promise<string | undefined> { |
| 15 | const csp = res.getHeader('Content-Security-Policy') |
| 16 | if (typeof csp === 'string') { |
| 17 | // add frame-ancestors to allow the iframe to be loaded by Vitest, |
| 18 | // but keep the rest of the CSP |
| 19 | res.setHeader( |
| 20 | 'Content-Security-Policy', |
| 21 | csp.replace(/frame-ancestors [^;]+/, 'frame-ancestors *'), |
| 22 | ) |
| 23 | } |
| 24 | |
| 25 | const sessionId = url.searchParams.get('sessionId') || 'none' |
| 26 | const session = globalServer.vitest._browserSessions.getSession(sessionId) |
| 27 | |
| 28 | if (!session) { |
| 29 | res.statusCode = 400 |
| 30 | res.end('Invalid session ID') |
| 31 | return |
| 32 | } |
| 33 | |
| 34 | const project = globalServer.vitest.getProjectByName(session.project.name || '') |
| 35 | const browserProject = (project.browser as ProjectBrowser | undefined) || [...globalServer.children][0] |
| 36 | |
| 37 | if (!browserProject) { |
| 38 | res.statusCode = 400 |
| 39 | res.end('Invalid session ID') |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | const injectorJs: string = typeof globalServer.injectorJs === 'string' |
| 44 | ? globalServer.injectorJs |
| 45 | : await globalServer.injectorJs |
| 46 | |
| 47 | const injector = replacer(injectorJs, { |
| 48 | __VITEST_PROVIDER__: JSON.stringify(project.browser!.provider.name), |
| 49 | __VITEST_CONFIG__: JSON.stringify(browserProject.wrapSerializedConfig()), |
| 50 | __VITEST_VITE_CONFIG__: JSON.stringify({ |
| 51 | root: browserProject.vite.config.root, |
| 52 | }), |
| 53 | __VITEST_TYPE__: '"tester"', |
| 54 | __VITEST_METHOD__: JSON.stringify('none'), |
| 55 | __VITEST_SESSION_ID__: JSON.stringify(sessionId), |
| 56 | __VITEST_OTEL_CARRIER__: JSON.stringify(null), |
| 57 | __VITEST_TESTER_ID__: JSON.stringify(crypto.randomUUID()), |
| 58 | __VITEST_PROVIDED_CONTEXT__: '{}', |
| 59 | __VITEST_API_TOKEN__: JSON.stringify(globalServer.vitest.config.api.token), |
| 60 | }) |
| 61 | |
| 62 | const testerHtml = typeof browserProject.testerHtml === 'string' |
| 63 | ? browserProject.testerHtml |
| 64 | : await browserProject.testerHtml |
| 65 | |
| 66 | try { |
no test coverage detected