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

Function VitestPlugin

packages/vitest/src/node/plugins/index.ts:26–291  ·  view source on GitHub ↗
(
  options: UserConfig = {},
  vitest: Vitest = new Vitest('test', deepClone(options)),
)

Source from the content-addressed store, hash-verified

24import { VitestCoreResolver } from './vitestResolver'
25
26export async function VitestPlugin(
27 options: UserConfig = {},
28 vitest: Vitest = new Vitest('test', deepClone(options)),
29): Promise<VitePlugin[]> {
30 const userConfig = deepMerge({}, options) as UserConfig
31
32 async function UIPlugin() {
33 await vitest.packageInstaller.ensureInstalled('@vitest/ui', resolve(options.root || process.cwd()), vitest.version)
34 return (await import('@vitest/ui')).default(vitest)
35 }
36
37 return [
38 <VitePlugin>{
39 name: 'vitest',
40 enforce: 'pre',
41 options() {
42 this.meta.watchMode = false
43 },
44 async config(viteConfig) {
45 if (options.watch) {
46 // Earlier runs have overwritten values of the `options`.
47 // Reset it back to initial user config before setting up the server again.
48 options = deepMerge({}, userConfig) as UserConfig
49 }
50
51 // preliminary merge of options to be able to create server options for vite
52 // however to allow vitest plugins to modify vitest config values
53 // this is repeated in configResolved where the config is final
54 const testConfig = deepMerge(
55 {} as UserConfig,
56 configDefaults,
57 viteConfig.test ?? {},
58 options,
59 )
60 testConfig.api = resolveApiServerConfig(testConfig, defaultPort)
61
62 // store defines for globalThis to make them
63 // reassignable when running in worker in src/runtime/setup.ts
64 const originalDefine = { ...viteConfig.define } // stash original defines for browser mode
65 const defines: Record<string, any> = deleteDefineConfig(viteConfig)
66
67 ;(options as unknown as ResolvedConfig).defines = defines
68 ;(options as unknown as ResolvedConfig).viteDefine = originalDefine
69
70 let open: string | boolean | undefined = false
71
72 if (testConfig.ui && testConfig.open) {
73 open = testConfig.uiBase ?? '/__vitest__/'
74 }
75
76 const resolveOptions = getDefaultResolveOptions()
77
78 let config: ViteConfig = {
79 base: '/',
80 root: viteConfig.test?.root || options.root,
81 define: {
82 // disable replacing `process.env.NODE_ENV` with static string by vite:client-inject
83 'process.env.NODE_ENV': 'process.env.NODE_ENV',

Callers 2

createVitestFunction · 0.90
resolveConfigFunction · 0.90

Calls 11

deepCloneFunction · 0.90
deepMergeFunction · 0.90
MetaEnvReplacerPluginFunction · 0.90
CSSEnablerPluginFunction · 0.90
CoverageTransformFunction · 0.90
VitestCoreResolverFunction · 0.90
MocksPluginsFunction · 0.90
VitestOptimizerFunction · 0.90
NormalizeURLPluginFunction · 0.90
ModuleRunnerTransformFunction · 0.90
filterMethod · 0.65

Tested by

no test coverage detected