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

Function getGeneratedFileContent

test/test-utils/index.ts:415–442  ·  view source on GitHub ↗
(content: TestFsStructure[string])

Source from the content-addressed store, hash-verified

413}
414
415function getGeneratedFileContent(content: TestFsStructure[string]) {
416 if (typeof content === 'string') {
417 return content
418 }
419 if (typeof content === 'function') {
420 const code = `await (${stripIndent(String(content))})()`
421 return code
422 }
423 if (Array.isArray(content) && typeof content[1] === 'object' && ('exports' in content[1] || 'imports' in content[1])) {
424 const imports = Object.entries(content[1].imports || [])
425 const code = `
426${imports.map(([path, is]) => `import { ${is.join(', ')} } from '${path}'`)}
427const results = await (${stripIndent(String(content[0]))})({ ${imports.flatMap(([_, is]) => is).join(', ')} })
428${(content[1].exports || []).map(e => `export const ${e} = results["${e}"]`)}
429 `
430 return code
431 }
432 if ('test' in content && content.test?.browser?.enabled && content.test?.browser?.provider?.name) {
433 const name = content.test.browser.provider.name
434 return `
435import { ${name} } from '@vitest/browser-${name}'
436const config = ${JSON.stringify(content)}
437config.test.browser.provider = ${name}(${JSON.stringify(content.test.browser.provider.options || {})})
438export default config
439 `
440 }
441 return `export default ${JSON.stringify(content)}`
442}
443
444export function useFS<T extends TestFsStructure>(root: string, structure: T, ensureConfig = true, task?: TestContext['task']) {
445 const files = new Set<string>()

Callers 1

useFSFunction · 0.85

Calls 2

entriesMethod · 0.80
stripIndentFunction · 0.70

Tested by

no test coverage detected