Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge viteConfig.test in configResolved #644

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
name: 'vitest',
enforce: 'pre',
config(viteConfig: any) {
options = deepMerge(options, viteConfig.test || {})
options.api = resolveApiConfig(options)
// preliminary merge of options to be able to create server options for vite
// however to allow vitest plugins to modify vitest config values
// this is repeated in configResolved where the config is final
const preOptions = deepMerge(options, viteConfig.test || {})
preOptions.api = resolveApiConfig(preOptions)
return {
clearScreen: false,
resolve: {
Expand All @@ -29,9 +32,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
mainFields: [],
},
server: {
...options.api,
open: options.ui && options.open
? options.uiBase ?? '/__vitest__/'
...preOptions.api,
open: preOptions.ui && preOptions.open
? preOptions.uiBase ?? '/__vitest__/'
: undefined,
preTransformRequests: false,
},
Expand All @@ -42,6 +45,11 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
cacheDir: undefined,
}
},
async configResolved(viteConfig) {
// viteConfig.test is final now, merge it for real
options = deepMerge(options, viteConfig.test as any || {})
options.api = resolveApiConfig(options)
},
async configureServer(server) {
if (haveStarted)
await ctx.report('onServerRestart')
Expand Down
11 changes: 11 additions & 0 deletions test/global-setup/setupFiles/add-something-to-global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { afterAll, beforeAll } from 'vitest'

beforeAll(() => {
// @ts-expect-error type
global.something = 'something'
})

afterAll(() => {
// @ts-expect-error type
delete global.something
})
6 changes: 6 additions & 0 deletions test/global-setup/test/setup-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

test('something has been added to global by setupFiles entry', async() => {
// @ts-expect-error type
const result = something
expect(result).toBe('something')
})
14 changes: 10 additions & 4 deletions test/global-setup/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [{
name: 'a-vitest-plugin-that-changes-config',
config: () => ({
test: { setupFiles: ['./setupFiles/add-something-to-global.ts'] },
}),
}],
test: {
globals: true,
globalSetup: [
'./setupFiles/default-export.js',
'./setupFiles/named-exports.js',
'./setupFiles/ts-with-imports.ts',
'./setupFiles/another-vite-instance.ts',
'./globalSetup/default-export.js',
'./globalSetup/named-exports.js',
'./globalSetup/ts-with-imports.ts',
'./globalSetup/another-vite-instance.ts',
],
},
})