-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vitest): don't initialize globalSetup if workspace doesn't run te…
…sts (#4213)
- Loading branch information
1 parent
98fe3d5
commit 0646197
Showing
8 changed files
with
103 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { toArray } from '@vitest/utils' | ||
import type { ViteNodeRunner } from 'vite-node/client' | ||
import type { WorkspaceProject } from './workspace' | ||
|
||
export interface GlobalSetupFile { | ||
file: string | ||
setup?: () => Promise<Function | void> | void | ||
teardown?: Function | ||
} | ||
|
||
export async function loadGlobalSetupFiles(project: WorkspaceProject): Promise<GlobalSetupFile[]> { | ||
const globalSetupFiles = toArray(project.server.config.test?.globalSetup) | ||
return Promise.all(globalSetupFiles.map(file => loadGlobalSetupFile(file, project.runner))) | ||
} | ||
|
||
async function loadGlobalSetupFile(file: string, runner: ViteNodeRunner): Promise<GlobalSetupFile> { | ||
const m = await runner.executeFile(file) | ||
for (const exp of ['default', 'setup', 'teardown']) { | ||
if (m[exp] != null && typeof m[exp] !== 'function') | ||
throw new Error(`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`) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { expect, test } from 'vitest' | ||
|
||
test('example test', () => { | ||
expect(1 + 1).toBe(2) | ||
}) |