From 22d2deda69e67f7c001c72eaecdbf9c9e22fcdc9 Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 18 Jan 2022 06:16:44 +0100 Subject: [PATCH] refactor: rename global to globals (#565) --- bench/test/vue/vitest.config.ts | 2 +- docs/config/index.md | 10 +++++----- examples/basic/vite.config.ts | 2 +- examples/lit/vite.config.ts | 2 +- examples/mocks/tsconfig.json | 2 +- examples/mocks/vite.config.ts | 2 +- examples/react-mui/vite.config.ts | 2 +- examples/react-storybook-testing/vite.config.ts | 2 +- examples/react-testing-lib-msw/vite.config.ts | 2 +- examples/react-testing-lib/vite.config.ts | 2 +- examples/react/vitest.config.ts | 2 +- examples/svelte/vitest.config.ts | 2 +- examples/vitesse/vite.config.ts | 2 +- examples/vue-jsx/vite.config.ts | 2 +- examples/vue/vitest.config.ts | 2 +- examples/vue2/vitest.config.ts | 2 +- packages/vitest/{global.d.ts => globals.d.ts} | 0 packages/vitest/package.json | 5 ++++- .../vitest/src/integrations/{global.ts => globals.ts} | 0 packages/vitest/src/node/cli.ts | 3 ++- packages/vitest/src/node/config.ts | 4 ++++ packages/vitest/src/runtime/setup.ts | 4 ++-- packages/vitest/src/types/config.ts | 11 ++++++++--- test/global-setup/vitest.config.ts | 2 +- test/snapshots/vitest.config.ts | 2 +- tsconfig.json | 1 + 26 files changed, 43 insertions(+), 29 deletions(-) rename packages/vitest/{global.d.ts => globals.d.ts} (100%) rename packages/vitest/src/integrations/{global.ts => globals.ts} (100%) diff --git a/bench/test/vue/vitest.config.ts b/bench/test/vue/vitest.config.ts index 53a1f6f79..77f684f37 100644 --- a/bench/test/vue/vitest.config.ts +++ b/bench/test/vue/vitest.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ Vue(), ], test: { - global: true, + globals: true, isolate: false, environment: 'happy-dom', // jsdom fails when there are more test files }, diff --git a/docs/config/index.md b/docs/config/index.md index e8eddaa81..a8a34a89e 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -57,12 +57,12 @@ Externalize means that Vite will bypass the package to native Node. Externalized Vite will process inlined modules. This could be helpful to handle packages that ship `.js` in ESM format (that Node can't handle). -### global +### globals - **Type:** `boolean` - **Default:** `false` -By default, `vitest` does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the `--global` option to CLI or add `global: true` in the config. +By default, `vitest` does not provide global APIs for explicitness. If you prefer to use the APIs globally like Jest, you can pass the `--globals` option to CLI or add `globals: true` in the config. ```ts // vite.config.ts @@ -70,18 +70,18 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - global: true, + globals: true, }, }) ``` -To get TypeScript working with the global APIs, add `vitest/global` to the `types` filed in your `tsconfig.json` +To get TypeScript working with the global APIs, add `vitest/globals` to the `types` filed in your `tsconfig.json` ```json // tsconfig.json { "compilerOptions": { - "types": ["vitest/global"] + "types": ["vitest/globals"] } } ``` diff --git a/examples/basic/vite.config.ts b/examples/basic/vite.config.ts index 31f4c3ffa..b303aa94f 100644 --- a/examples/basic/vite.config.ts +++ b/examples/basic/vite.config.ts @@ -7,6 +7,6 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { /* for example, use global to avoid globals imports (describe, test, expect): */ - // global: true, + // globals: true, }, }) diff --git a/examples/lit/vite.config.ts b/examples/lit/vite.config.ts index 14cc71b11..b84613935 100644 --- a/examples/lit/vite.config.ts +++ b/examples/lit/vite.config.ts @@ -5,7 +5,7 @@ import { defineConfig } from 'vite' // https://vitejs.dev/config/ export default defineConfig({ test: { - global: true, + globals: true, environment: 'happy-dom', }, }) diff --git a/examples/mocks/tsconfig.json b/examples/mocks/tsconfig.json index 9f9b0a6a9..1f935cba3 100644 --- a/examples/mocks/tsconfig.json +++ b/examples/mocks/tsconfig.json @@ -1,5 +1,5 @@ { "compilerOptions": { - "types": ["vitest/global"], + "types": ["vitest/globals"], } } \ No newline at end of file diff --git a/examples/mocks/vite.config.ts b/examples/mocks/vite.config.ts index 7bca79c61..4d59d8b16 100644 --- a/examples/mocks/vite.config.ts +++ b/examples/mocks/vite.config.ts @@ -5,7 +5,7 @@ import { defineConfig } from 'vite' // https://vitejs.dev/config/ export default defineConfig({ test: { - global: true, + globals: true, environment: 'node', }, }) diff --git a/examples/react-mui/vite.config.ts b/examples/react-mui/vite.config.ts index c8a385c9e..b034ea02c 100644 --- a/examples/react-mui/vite.config.ts +++ b/examples/react-mui/vite.config.ts @@ -7,7 +7,7 @@ export default defineConfig(() => { }, test: { environment: 'jsdom', - global: true, + globals: true, }, } }) diff --git a/examples/react-storybook-testing/vite.config.ts b/examples/react-storybook-testing/vite.config.ts index 270212f50..3d4ae8e13 100644 --- a/examples/react-storybook-testing/vite.config.ts +++ b/examples/react-storybook-testing/vite.config.ts @@ -7,7 +7,7 @@ import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], test: { - global: true, + globals: true, environment: 'jsdom', setupFiles: ['./src/setup.ts'], }, diff --git a/examples/react-testing-lib-msw/vite.config.ts b/examples/react-testing-lib-msw/vite.config.ts index 49af20508..5593c0c5b 100644 --- a/examples/react-testing-lib-msw/vite.config.ts +++ b/examples/react-testing-lib-msw/vite.config.ts @@ -8,7 +8,7 @@ import { defineConfig } from 'vite' export default defineConfig({ plugins: [react()], test: { - global: true, + globals: true, environment: 'jsdom', setupFiles: ['./src/setup.ts'], }, diff --git a/examples/react-testing-lib/vite.config.ts b/examples/react-testing-lib/vite.config.ts index c954c14c4..dbd2c7325 100644 --- a/examples/react-testing-lib/vite.config.ts +++ b/examples/react-testing-lib/vite.config.ts @@ -8,7 +8,7 @@ import { defineConfig } from 'vite' export default defineConfig({ plugins: [react()], test: { - global: true, + globals: true, environment: 'jsdom', setupFiles: './src/test/setup.ts', }, diff --git a/examples/react/vitest.config.ts b/examples/react/vitest.config.ts index 1a64f36fb..85fb2c4ab 100644 --- a/examples/react/vitest.config.ts +++ b/examples/react/vitest.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - global: true, + globals: true, environment: 'happy-dom', }, }) diff --git a/examples/svelte/vitest.config.ts b/examples/svelte/vitest.config.ts index 30af7bb10..e85746e25 100644 --- a/examples/svelte/vitest.config.ts +++ b/examples/svelte/vitest.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ svelte({ hot: !process.env.VITEST }), ], test: { - global: true, + globals: true, environment: 'jsdom', }, }) diff --git a/examples/vitesse/vite.config.ts b/examples/vitesse/vite.config.ts index c63d7307e..f2de3888e 100644 --- a/examples/vitesse/vite.config.ts +++ b/examples/vitesse/vite.config.ts @@ -22,7 +22,7 @@ export default defineConfig({ }), ], test: { - global: true, + globals: true, environment: 'happy-dom', }, }) diff --git a/examples/vue-jsx/vite.config.ts b/examples/vue-jsx/vite.config.ts index a91abf881..e99d23ff7 100644 --- a/examples/vue-jsx/vite.config.ts +++ b/examples/vue-jsx/vite.config.ts @@ -5,7 +5,7 @@ import Jsx from '@vitejs/plugin-vue-jsx' export default defineConfig({ plugins: [Vue(), Jsx()], test: { - global: true, + globals: true, environment: 'happy-dom', transformMode: { web: [/.[tj]sx$/], diff --git a/examples/vue/vitest.config.ts b/examples/vue/vitest.config.ts index e08cb402e..4e3147e9b 100644 --- a/examples/vue/vitest.config.ts +++ b/examples/vue/vitest.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ Vue(), ], test: { - global: true, + globals: true, environment: 'happy-dom', }, }) diff --git a/examples/vue2/vitest.config.ts b/examples/vue2/vitest.config.ts index f5d4da12a..e8757cbb5 100644 --- a/examples/vue2/vitest.config.ts +++ b/examples/vue2/vitest.config.ts @@ -8,7 +8,7 @@ export default defineConfig({ ScriptSetup(), ], test: { - global: true, + globals: true, environment: 'happy-dom', setupFiles: [ 'vitest.setup.ts', diff --git a/packages/vitest/global.d.ts b/packages/vitest/globals.d.ts similarity index 100% rename from packages/vitest/global.d.ts rename to packages/vitest/globals.d.ts diff --git a/packages/vitest/package.json b/packages/vitest/package.json index 97a4d6e33..12962e226 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -27,7 +27,10 @@ }, "./*": "./*", "./global": { - "types": "./global.d.ts" + "types": "./globals.d.ts" + }, + "./globals": { + "types": "./globals.d.ts" }, "./node": { "import": "./dist/node.js", diff --git a/packages/vitest/src/integrations/global.ts b/packages/vitest/src/integrations/globals.ts similarity index 100% rename from packages/vitest/src/integrations/global.ts rename to packages/vitest/src/integrations/globals.ts diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 1a25db799..19f20e5a4 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -24,7 +24,8 @@ cli .option('--outputFile ', 'write test results to a file when the --reporter=json option is also specified') .option('--coverage', 'use c8 for coverage') .option('--run', 'do not watch') - .option('--global', 'inject apis globally') + .option('--globals', 'inject apis globally') + .option('--global', 'deprecated, use --globals') .option('--dom', 'mock browser api with happy-dom') .option('--environment ', 'runner environment', { default: 'node' }) .option('--passWithNoTests', 'pass when no tests found') diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 7260e19e5..67c820bea 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -53,9 +53,13 @@ export function resolveConfig( if (options.dom) options.environment = 'happy-dom' + const globals = options?.global ?? options.globals + const resolved = { ...deepMerge(options, viteConfig.test || {}), root: viteConfig.root, + globals, + global: globals, } as ResolvedConfig if (viteConfig.base !== '/') diff --git a/packages/vitest/src/runtime/setup.ts b/packages/vitest/src/runtime/setup.ts index 072eeaf75..2ca5dccbe 100644 --- a/packages/vitest/src/runtime/setup.ts +++ b/packages/vitest/src/runtime/setup.ts @@ -16,8 +16,8 @@ export async function setupGlobalEnv(config: ResolvedConfig) { setupConsoleLogSpy() await setupChai() - if (config.global) - (await import('../integrations/global')).registerApiGlobally() + if (config.globals) + (await import('../integrations/globals')).registerApiGlobally() } export function setupConsoleLogSpy() { diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 405f600c7..933d02da4 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -72,9 +72,14 @@ export interface InlineConfig { } /** - * Register apis globally - * - * @default false + * Register apis globally + * + * @default false + */ + globals?: boolean + + /** + * @deprecated */ global?: boolean diff --git a/test/global-setup/vitest.config.ts b/test/global-setup/vitest.config.ts index eac4ab5fd..1dbd0c7ed 100644 --- a/test/global-setup/vitest.config.ts +++ b/test/global-setup/vitest.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - global: true, + globals: true, globalSetup: [ './setupFiles/default-export.js', './setupFiles/named-exports.js', diff --git a/test/snapshots/vitest.config.ts b/test/snapshots/vitest.config.ts index cb5f02483..872e76b48 100644 --- a/test/snapshots/vitest.config.ts +++ b/test/snapshots/vitest.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - global: true, + globals: true, snapshotFormat: { printBasicPrototype: true, }, diff --git a/tsconfig.json b/tsconfig.json index abb43bbec..5f9ce0c22 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,7 @@ "~/*": ["./packages/ui/client/*"], "vitest": ["./packages/vitest/src/index.ts"], "vitest/global": ["./packages/vitest/global.d.ts"], + "vitest/globals": ["./packages/vitest/globals.d.ts"], "vitest/node": ["./packages/vitest/src/node/index.ts"], "vite-node": ["./packages/vite-node/src/index.ts"], "vite-node/client": ["./packages/vite-node/src/client.ts"],