refactor: rename global to globals (#565)

This commit is contained in:
patak 2022-01-18 06:16:44 +01:00 committed by GitHub
parent 7259a0627a
commit 22d2deda69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 43 additions and 29 deletions

View File

@ -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
},

View File

@ -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"]
}
}
```

View File

@ -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,
},
})

View File

@ -5,7 +5,7 @@ import { defineConfig } from 'vite'
// https://vitejs.dev/config/
export default defineConfig({
test: {
global: true,
globals: true,
environment: 'happy-dom',
},
})

View File

@ -1,5 +1,5 @@
{
"compilerOptions": {
"types": ["vitest/global"],
"types": ["vitest/globals"],
}
}

View File

@ -5,7 +5,7 @@ import { defineConfig } from 'vite'
// https://vitejs.dev/config/
export default defineConfig({
test: {
global: true,
globals: true,
environment: 'node',
},
})

View File

@ -7,7 +7,7 @@ export default defineConfig(() => {
},
test: {
environment: 'jsdom',
global: true,
globals: true,
},
}
})

View File

@ -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'],
},

View File

@ -8,7 +8,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
plugins: [react()],
test: {
global: true,
globals: true,
environment: 'jsdom',
setupFiles: ['./src/setup.ts'],
},

View File

@ -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',
},

View File

@ -4,7 +4,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
test: {
global: true,
globals: true,
environment: 'happy-dom',
},
})

View File

@ -6,7 +6,7 @@ export default defineConfig({
svelte({ hot: !process.env.VITEST }),
],
test: {
global: true,
globals: true,
environment: 'jsdom',
},
})

View File

@ -22,7 +22,7 @@ export default defineConfig({
}),
],
test: {
global: true,
globals: true,
environment: 'happy-dom',
},
})

View File

@ -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$/],

View File

@ -6,7 +6,7 @@ export default defineConfig({
Vue(),
],
test: {
global: true,
globals: true,
environment: 'happy-dom',
},
})

View File

@ -8,7 +8,7 @@ export default defineConfig({
ScriptSetup(),
],
test: {
global: true,
globals: true,
environment: 'happy-dom',
setupFiles: [
'vitest.setup.ts',

View File

@ -27,7 +27,10 @@
},
"./*": "./*",
"./global": {
"types": "./global.d.ts"
"types": "./globals.d.ts"
},
"./globals": {
"types": "./globals.d.ts"
},
"./node": {
"import": "./dist/node.js",

View File

@ -24,7 +24,8 @@ cli
.option('--outputFile <filename>', '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 <env>', 'runner environment', { default: 'node' })
.option('--passWithNoTests', 'pass when no tests found')

View File

@ -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 !== '/')

View File

@ -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() {

View File

@ -72,9 +72,14 @@ export interface InlineConfig {
}
/**
* Register apis globally
*
* @default false
* Register apis globally
*
* @default false
*/
globals?: boolean
/**
* @deprecated
*/
global?: boolean

View File

@ -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',

View File

@ -2,7 +2,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
test: {
global: true,
globals: true,
snapshotFormat: {
printBasicPrototype: true,
},

View File

@ -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"],