vitest-dev/vitest · error · Error
invalid globalSetup file ${file}. Must export setup, teardow
Error message
invalid globalSetup file ${file}. Must export setup, teardown or have a default export What it means
loadGlobalSetupFile (globalSetup.ts:47) requires the module to export at least one of: default, setup, or teardown. If none is present, vitest can't know what to run for global setup and throws.
Source
Thrown at packages/vitest/src/node/globalSetup.ts:47
`invalid export in globalSetup file ${file}: ${exp} must be a function`,
)
}
}
if (m.default) {
return {
file,
setup: m.default,
}
}
else if (m.setup || m.teardown) {
return {
file,
setup: m.setup,
teardown: m.teardown,
}
}
else {
throw new Error(
`invalid globalSetup file ${file}. Must export setup, teardown or have a default export`,
)
}
}
View on GitHub (pinned to d568f8ce37)
Solutions
- Add export default function setup() { ... } (or named setup/teardown) to the file.
- Verify the path configured in test.globalSetup points at the intended file.
- If you only need teardown, export an empty setup and a real teardown.
Example fix
// before (globalSetup file)
export const utils = { ... } // no setup/teardown/default
// after
export default function setup() { /* init */ } Defensive patterns
Strategy: validation
Validate before calling
const mod = await runner.import(file)
if (typeof mod.default !== 'function' && typeof mod.setup !== 'function' && typeof mod.teardown !== 'function') {
throw new Error(`globalSetup file ${file} must export default/setup/teardown`)
} Type guard
function hasGlobalSetupExport(mod: any): boolean {
return typeof mod?.default === 'function'
|| typeof mod?.setup === 'function'
|| typeof mod?.teardown === 'function'
} Prevention
- Always export a default setup function from globalSetup files.
- Double-check the test.globalSetup path resolves to the intended file.
- Add a smoke test that imports each globalSetup file and asserts an export.
When it happens
Trigger: A globalSetup file that has no default export and no setup/teardown named exports — e.g. an empty file, a file with only side effects, or one with differently-named exports.
Common situations: Forgetting to add a setup function; pointing globalSetup at the wrong file; file that only re-exports helpers.
Related errors
- invalid export in globalSetup file ${file}: ${exp} must be a
- Environment "${name}" is not a valid environment. Path "${pa
- Tag "${tag.name}": priority must be a non-negative number.
- Unknown value for "test.listTags": ${listTags}
- Expected config.test.coverage.thresholds to be an object
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/82ea749a308d8ace.json.
Report an issue: GitHub.