Skip to content

Commit

Permalink
fix: improve watch options resolving
Browse files Browse the repository at this point in the history
Thanks to @DerYeger @Aslemammad

close #736, close #731
  • Loading branch information
antfu committed Feb 12, 2022
1 parent 98846d3 commit 5dca4ed
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 78 deletions.
55 changes: 0 additions & 55 deletions packages/vitest/src/constants.ts
@@ -1,63 +1,8 @@
import { fileURLToPath } from 'url'
import { resolve } from 'pathe'
import type { ResolvedC8Options, UserConfig } from './types'

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}/**']

const defaultCoverageExcludes = [
'coverage/**',
'packages/*/test{,s}/**',
'**/*.d.ts',
'cypress/**',
'test{,s}/**',
'test{,-*}.{js,cjs,mjs,ts,tsx,jsx}',
'**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx}',
'**/__tests__/**',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc}.config.{js,cjs,mjs,ts}',
'**/.{eslint,mocha}rc.{js,cjs}',
]

export const coverageConfigDefaults = Object.freeze({
enabled: false,
clean: true,
cleanOnRerun: false,
reportsDirectory: './coverage',
excludeNodeModules: true,
exclude: defaultCoverageExcludes,
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 const configDefaults: UserConfig = Object.freeze({
allowOnly: !process.env.CI,
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,
reporters: [],
silent: false,
api: false,
ui: false,
uiBase: '/__vitest__/',
open: true,
coverage: coverageConfigDefaults,
})

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

Expand Down
56 changes: 56 additions & 0 deletions packages/vitest/src/defaults.ts
@@ -0,0 +1,56 @@
import type { ResolvedC8Options, UserConfig } from './types'

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}/**']

const defaultCoverageExcludes = [
'coverage/**',
'packages/*/test{,s}/**',
'**/*.d.ts',
'cypress/**',
'test{,s}/**',
'test{,-*}.{js,cjs,mjs,ts,tsx,jsx}',
'**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx}',
'**/__tests__/**',
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc}.config.{js,cjs,mjs,ts}',
'**/.{eslint,mocha}rc.{js,cjs}',
]

const coverageConfigDefaults = {
enabled: false,
clean: true,
cleanOnRerun: false,
reportsDirectory: './coverage',
excludeNodeModules: true,
exclude: defaultCoverageExcludes,
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 const configDefaults: UserConfig = Object.freeze({
allowOnly: !process.env.CI,
watch: !process.env.CI,
globals: false,
environment: 'node',
threads: true,
clearMocks: false,
restoreMocks: false,
mockReset: false,
include: defaultInclude,
exclude: defaultExclude,
testTimeout: 5000,
hookTimeout: 10000,
isolate: true,
watchIgnore: [/\/node_modules\//, /\/dist\//],
update: false,
reporters: [],
silent: false,
api: false,
ui: false,
uiBase: '/__vitest__/',
open: true,
coverage: coverageConfigDefaults,
})
10 changes: 5 additions & 5 deletions packages/vitest/src/integrations/coverage.ts
Expand Up @@ -7,11 +7,11 @@ import type { RawSourceMap } from 'vite-node'
import type { Vitest } from '../node'
import { toArray } from '../utils'
import type { C8Options, ResolvedC8Options } from '../types'
import { coverageConfigDefaults } from '../constants'
import { configDefaults } from '../defaults'

export function resolveC8Options(options: C8Options, root: string): ResolvedC8Options {
const resolved: ResolvedC8Options = {
...coverageConfigDefaults,
...configDefaults.coverage,
...options as any,
}

Expand Down Expand Up @@ -45,7 +45,7 @@ export async function reportCoverage(ctx: Vitest) {
const report = createReport(ctx.config.coverage)

// add source maps
const sourceMapMata: Record<string, { map: RawSourceMap; source: string | undefined }> = {}
const sourceMapMeta: Record<string, { map: RawSourceMap; source: string | undefined }> = {}
await Promise.all(Array
.from(ctx.vitenode.fetchCache.entries())
.filter(i => !i[0].includes('/node_modules/'))
Expand All @@ -66,7 +66,7 @@ export async function reportCoverage(ctx: Vitest) {
// so use an actual file path
const sources = [url]

sourceMapMata[url] = {
sourceMapMeta[url] = {
source: result.code,
map: {
sourcesContent: code ? [code] : undefined,
Expand All @@ -83,7 +83,7 @@ export async function reportCoverage(ctx: Vitest) {

report._getSourceMap = (coverage: Profiler.ScriptCoverage) => {
const path = pathToFileURL(coverage.url).href
const data = sourceMapMata[path]
const data = sourceMapMeta[path]

if (!data)
return {}
Expand Down
29 changes: 19 additions & 10 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -43,34 +43,43 @@ cli

cli
.command('watch [...filters]')
.action(dev)
.action(start)

cli
.command('dev [...filters]')
.action(dev)
.action(start)

cli
.command('[...filters]')
.action(dev)
.action(start)

cli.parse()

async function runRelated(relatedFiles: string[] | string, argv: UserConfig) {
export interface CliOptions extends UserConfig {
/**
* Override the watch mode
*/
run?: boolean
}

async function runRelated(relatedFiles: string[] | string, argv: CliOptions) {
argv.related = relatedFiles
argv.passWithNoTests ??= true
await dev([], argv)
await start([], argv)
}

async function dev(cliFilters: string[], argv: UserConfig) {
if (argv.watch == null)
argv.watch = !process.env.CI && !argv.run
await run(cliFilters, argv)
async function run(cliFilters: string[], options: CliOptions) {
options.run = true
await start(cliFilters, options)
}

async function run(cliFilters: string[], options: UserConfig) {
async function start(cliFilters: string[], options: CliOptions) {
process.env.VITEST = 'true'
process.env.NODE_ENV = 'test'

if (options.run)
options.watch = false

if (!await ensurePackageInstalled('vite'))
process.exit(1)

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

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

Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/node/plugins/index.ts
@@ -1,5 +1,5 @@
import type { Plugin as VitePlugin } from 'vite'
import { configDefaults } from '../../constants'
import { configDefaults } from '../../defaults'
import type { UserConfig } from '../../types'
import { deepMerge, ensurePackageInstalled, notNullish } from '../../utils'
import { resolveApiConfig } from '../config'
Expand Down Expand Up @@ -61,7 +61,6 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
options,
)
options.api = resolveApiConfig(options)
options.watch = options.watch && !options.run

process.env.BASE_URL ??= viteConfig.base
process.env.MODE ??= viteConfig.mode
Expand Down
5 changes: 0 additions & 5 deletions packages/vitest/src/types/config.ts
Expand Up @@ -295,11 +295,6 @@ export interface UserConfig extends InlineConfig {
*/
dom?: boolean

/**
* Do not watch
*/
run?: boolean

/**
* Pass with no tests
*/
Expand Down

0 comments on commit 5dca4ed

Please sign in to comment.