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: export default config to expand it #702

Merged
merged 7 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/config/index.md
Expand Up @@ -21,6 +21,18 @@ export default defineConfig({
})
```

You can retrieve Vitest's default options to expand them if needed:

```ts
import { defaults } from 'vitest'
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

export default defineConfig({
test: {
exclude: [...defaults.exclude, 'packages/template/*'],
},
})
```

## Options

### include
Expand Down
26 changes: 26 additions & 0 deletions packages/vitest/src/constants.ts
@@ -1,11 +1,37 @@
import { fileURLToPath } from 'url'
import { resolve } from 'pathe'
import type { UserConfig } from './types'
import { defaults as coverageDefaults } from './integrations/coverage'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move the default from coverage to this file, the module is running in different context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved it there, but I still import it from here.


export const distDir = resolve(fileURLToPath(import.meta.url), '../../dist')

export const defaultInclude = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
export const defaultExclude = ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**']

export const defaults: UserConfig = {
globals: false,
environment: 'node',
threads: true,
clearMocks: false,
restoreMocks: false,
mockReset: false,
include: defaultInclude,
exclude: defaultExclude,
testTimeout: 5_000,
hookTimeout: 10_000,
isolate: true,
watchIgnore: [/\/node_modules\//, /\/dist\//],
update: false,
watch: true,
reporters: 'default',
silent: false,
api: false,
ui: false,
uiBase: '/__vitest__/',
open: true,
coverage: coverageDefaults,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to froze this object to avoid mutation on user land

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Froze both objects


// if changed, update also jsdocs and docs
export const defaultPort = 51204

Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/index.ts
Expand Up @@ -16,6 +16,8 @@ export * from './integrations/vi'
export * from './types'
export * from './api/types'

export { defaults } from './constants'

declare module 'vite' {
interface UserConfig {
/**
Expand Down
26 changes: 15 additions & 11 deletions packages/vitest/src/integrations/coverage.ts
Expand Up @@ -21,19 +21,23 @@ const defaultExcludes = [
'**/.{eslint,mocha}rc.{js,cjs}',
]

export const defaults = {
enabled: false,
clean: true,
cleanOnRerun: false,
reportsDirectory: './coverage',
excludeNodeModules: true,
exclude: defaultExcludes,
reporter: ['text', 'html'],
allowExternal: false,
// default extensions used by c8, plus '.vue' and '.svelte'
// see https://github.com/istanbuljs/schema/blob/master/default-extension.js
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', 'svelte'],
} as ResolvedC8Options

export function resolveC8Options(options: C8Options, root: string): ResolvedC8Options {
const resolved: ResolvedC8Options = {
enabled: false,
clean: true,
cleanOnRerun: false,
reportsDirectory: './coverage',
excludeNodeModules: true,
exclude: defaultExcludes,
reporter: ['text', 'html'],
allowExternal: false,
// default extensions used by c8, plus '.vue' and '.svelte'
// see https://github.com/istanbuljs/schema/blob/master/default-extension.js
extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', 'svelte'],
...defaults,
...options as any,
}

Expand Down
22 changes: 3 additions & 19 deletions packages/vitest/src/node/config.ts
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'pathe'
import type { ResolvedConfig as ResolvedViteConfig } from 'vite'

import type { ApiConfig, ResolvedConfig, UserConfig } from '../types'
import { defaultExclude, defaultInclude, defaultPort } from '../constants'
import { defaultPort, defaults } from '../constants'
import { resolveC8Options } from '../integrations/coverage'
import { toArray } from '../utils'

Expand Down Expand Up @@ -50,6 +50,7 @@ export function resolveConfig(
const globals = options?.global ?? options.globals

const resolved = {
...defaults,
...options,
root: viteConfig.root,
globals,
Expand All @@ -59,33 +60,16 @@ export function resolveConfig(
if (viteConfig.base !== '/')
resolved.base = viteConfig.base

resolved.coverage = resolveC8Options(resolved.coverage, resolved.root)
resolved.coverage = resolveC8Options(options.coverage || {}, resolved.root)

resolved.deps = resolved.deps || {}

resolved.environment = resolved.environment || 'node'
resolved.threads = resolved.threads ?? true

resolved.clearMocks = resolved.clearMocks ?? false
resolved.restoreMocks = resolved.restoreMocks ?? false
resolved.mockReset = resolved.mockReset ?? false

resolved.include = resolved.include ?? defaultInclude
resolved.exclude = resolved.exclude ?? defaultExclude

resolved.testTimeout = resolved.testTimeout ?? 5_000
resolved.hookTimeout = resolved.hookTimeout ?? 10_000

resolved.isolate = resolved.isolate ?? true

resolved.testNamePattern = resolved.testNamePattern
? resolved.testNamePattern instanceof RegExp
? resolved.testNamePattern
: new RegExp(resolved.testNamePattern)
: undefined

resolved.watchIgnore = resolved.watchIgnore ?? [/\/node_modules\//, /\/dist\//]

const CI = !!process.env.CI
const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT
resolved.snapshotOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/config.ts
Expand Up @@ -159,7 +159,7 @@ export interface InlineConfig {
/**
* Default timeout of a hook in milliseconds
*
* @default 5000
* @default 10000
*/
hookTimeout?: number

Expand Down