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

Function configureServer

packages/browser/src/node/plugin.ts:39–162  ·  view source on GitHub ↗
(server)

Source from the content-addressed store, hash-verified

37 enforce: 'pre',
38 name: 'vitest:browser',
39 async configureServer(server) {
40 parentServer.setServer(server)
41
42 // eslint-disable-next-line prefer-arrow-callback
43 server.middlewares.use(function vitestHeaders(_req, res, next) {
44 const headers = server.config.server.headers
45 if (headers) {
46 for (const name in headers) {
47 res.setHeader(name, headers[name]!)
48 }
49 }
50 next()
51 })
52 // strip _vitest_original query added by importActual so that
53 // the plugin pipeline sees the original import id (e.g. virtual modules's load hook).
54 server.middlewares.use((req, _res, next) => {
55 if (
56 req.url?.includes('_vitest_original')
57 && parentServer.project.config.browser.provider?.name === 'playwright'
58 ) {
59 req.url = req.url
60 .replace(/[?&]_vitest_original(?=[&#]|$)/, '')
61 .replace(/[?&]ext\b[^&#]*/, '')
62 .replace(/\?$/, '')
63 }
64 next()
65 })
66 server.middlewares.use(createOrchestratorMiddleware(parentServer))
67 server.middlewares.use(createTesterMiddleware(parentServer))
68
69 server.middlewares.use(
70 `${base}favicon.svg`,
71 (_, res) => {
72 const content = readFileSync(resolve(distRoot, 'client/favicon.svg'))
73 res.write(content, 'utf-8')
74 res.end()
75 },
76 )
77
78 // Serve coverage HTML at ./coverage if configured
79 const coverageHtmlDir = parentServer.vitest.config.coverage?.htmlDir
80 if (coverageHtmlDir) {
81 server.middlewares.use(
82 '/__vitest_test__/coverage',
83 sirv(coverageHtmlDir, {
84 single: true,
85 dev: true,
86 setHeaders: (res) => {
87 const csp = res.getHeader('Content-Security-Policy')
88 if (typeof csp === 'string') {
89 // add frame-ancestors to allow the iframe to be loaded by Vitest,
90 // but keep the rest of the CSP
91 res.setHeader(
92 'Content-Security-Policy',
93 csp.replace(/frame-ancestors [^;]+/, 'frame-ancestors *'),
94 )
95 }
96 res.setHeader(

Callers

nothing calls this directly

Calls 15

createTesterMiddlewareFunction · 0.90
isFileServingAllowedFunction · 0.90
readFileSyncFunction · 0.85
isValidApiRequestFunction · 0.85
setServerMethod · 0.80
endMethod · 0.80
bindMethod · 0.80
nextFunction · 0.50
resolveFunction · 0.50
writeMethod · 0.45
getHeaderMethod · 0.45

Tested by

no test coverage detected