From 5dca4ed8fd700690d022e34d8285bf9de76c0938 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 12 Feb 2022 15:10:54 +0800 Subject: [PATCH] fix: improve watch options resolving Thanks to @DerYeger @Aslemammad close #736, close #731 --- packages/vitest/src/constants.ts | 55 ------------------- packages/vitest/src/defaults.ts | 56 ++++++++++++++++++++ packages/vitest/src/integrations/coverage.ts | 10 ++-- packages/vitest/src/node/cli.ts | 29 ++++++---- packages/vitest/src/node/config.ts | 3 +- packages/vitest/src/node/plugins/index.ts | 3 +- packages/vitest/src/types/config.ts | 5 -- 7 files changed, 83 insertions(+), 78 deletions(-) create mode 100644 packages/vitest/src/defaults.ts diff --git a/packages/vitest/src/constants.ts b/packages/vitest/src/constants.ts index 3d02f152272b..19f4e8d57bb2 100644 --- a/packages/vitest/src/constants.ts +++ b/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 diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts new file mode 100644 index 000000000000..59a4a3c821fd --- /dev/null +++ b/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, +}) diff --git a/packages/vitest/src/integrations/coverage.ts b/packages/vitest/src/integrations/coverage.ts index 912294b6977e..1d33f73e67a1 100644 --- a/packages/vitest/src/integrations/coverage.ts +++ b/packages/vitest/src/integrations/coverage.ts @@ -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, } @@ -45,7 +45,7 @@ export async function reportCoverage(ctx: Vitest) { const report = createReport(ctx.config.coverage) // add source maps - const sourceMapMata: Record = {} + const sourceMapMeta: Record = {} await Promise.all(Array .from(ctx.vitenode.fetchCache.entries()) .filter(i => !i[0].includes('/node_modules/')) @@ -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, @@ -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 {} diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 2e71018c8cb8..b46e8d7f5350 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -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) diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 4eb5bed6ef96..f1a60fad06fa 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -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' diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index d8f143f53d6d..7f5e055b445c 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/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' @@ -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 diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index dbf66f917e0f..d245d7d69d45 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -295,11 +295,6 @@ export interface UserConfig extends InlineConfig { */ dom?: boolean - /** - * Do not watch - */ - run?: boolean - /** * Pass with no tests */