From e64570dfe325c832b0d1de57a1e4961f2395a7d8 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sat, 21 Jan 2023 14:51:12 +0100 Subject: [PATCH] feat: improve "isCI" check (#2705) --- packages/vitest/package.json | 1 + packages/vitest/src/defaults.ts | 5 +++-- packages/vitest/src/node/config.ts | 5 ++--- packages/vitest/src/node/reporters/base.ts | 4 ++-- packages/vitest/src/utils/env.ts | 1 + packages/vitest/src/utils/index.ts | 4 ++-- pnpm-lock.yaml | 6 ++++++ 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/vitest/package.json b/packages/vitest/package.json index 18d4988fed4d..b5a693f10f78 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -117,6 +117,7 @@ "local-pkg": "^0.4.2", "picocolors": "^1.0.0", "source-map": "^0.6.1", + "std-env": "^3.3.1", "strip-literal": "^1.0.0", "tinybench": "^2.3.1", "tinypool": "^0.3.0", diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 4c494df06c4a..d5210544c89a 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -1,4 +1,5 @@ import type { BenchmarkUserOptions, ResolvedCoverageOptions, UserConfig } from './types' +import { isCI } from './utils/env' 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}/**', '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*'] @@ -53,8 +54,8 @@ export const fakeTimersDefaults = { } as NonNullable const config = { - allowOnly: !process.env.CI, - watch: !process.env.CI, + allowOnly: !isCI, + watch: !isCI, globals: false, environment: 'node' as const, threads: true, diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index b780a80818f7..7ff6540be997 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -6,7 +6,7 @@ import type { ResolvedConfig as ResolvedViteConfig } from 'vite' import type { ApiConfig, ResolvedConfig, UserConfig, VitestRunMode } from '../types' import { defaultPort } from '../constants' import { benchmarkConfigDefaults, configDefaults } from '../defaults' -import { toArray } from '../utils' +import { isCI, toArray } from '../utils' import { VitestCache } from './cache' import { BaseSequencer } from './sequencers/BaseSequencer' import { RandomSequencer } from './sequencers/RandomSequencer' @@ -139,11 +139,10 @@ export function resolveConfig( : new RegExp(resolved.testNamePattern) : undefined - const CI = !!process.env.CI const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT resolved.snapshotOptions = { snapshotFormat: resolved.snapshotFormat || {}, - updateSnapshot: CI && !UPDATE_SNAPSHOT + updateSnapshot: isCI && !UPDATE_SNAPSHOT ? 'none' : UPDATE_SNAPSHOT ? 'all' diff --git a/packages/vitest/src/node/reporters/base.ts b/packages/vitest/src/node/reporters/base.ts index 320b634e37b7..80ac85e139d4 100644 --- a/packages/vitest/src/node/reporters/base.ts +++ b/packages/vitest/src/node/reporters/base.ts @@ -1,7 +1,7 @@ import { performance } from 'perf_hooks' import c from 'picocolors' import type { ErrorWithDiff, File, Reporter, Task, TaskResultPack, UserConsoleLog } from '../../types' -import { clearInterval, getFullName, getSuites, getTests, hasFailed, hasFailedSnapshot, isNode, relativePath, setInterval } from '../../utils' +import { clearInterval, getFullName, getSuites, getTests, hasFailed, hasFailedSnapshot, isCI, isNode, relativePath, setInterval } from '../../utils' import type { Vitest } from '../../node' import { F_RIGHT } from '../../utils/figures' import { countTestErrors, divider, formatProjectName, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils' @@ -20,7 +20,7 @@ export abstract class BaseReporter implements Reporter { start = 0 end = 0 watchFilters?: string[] - isTTY = isNode && process.stdout?.isTTY && !process.env.CI + isTTY = isNode && process.stdout?.isTTY && !isCI ctx: Vitest = undefined! private _filesInWatchMode = new Map() diff --git a/packages/vitest/src/utils/env.ts b/packages/vitest/src/utils/env.ts index 648a7337ceea..cafe9b01256e 100644 --- a/packages/vitest/src/utils/env.ts +++ b/packages/vitest/src/utils/env.ts @@ -1,2 +1,3 @@ export const isNode: boolean = typeof process < 'u' && typeof process.stdout < 'u' && !process.versions?.deno && !globalThis.window export const isBrowser: boolean = typeof window !== 'undefined' +export { isCI } from 'std-env' diff --git a/packages/vitest/src/utils/index.ts b/packages/vitest/src/utils/index.ts index 21f3d40b7889..73365d759e5a 100644 --- a/packages/vitest/src/utils/index.ts +++ b/packages/vitest/src/utils/index.ts @@ -7,7 +7,7 @@ import type { Suite, Task } from '../types' import { EXIT_CODE_RESTART } from '../constants' import { getWorkerState } from '../utils' import { getNames } from './tasks' -import { isBrowser, isNode } from './env' +import { isBrowser, isCI, isNode } from './env' export * from './graph' export * from './tasks' @@ -83,7 +83,7 @@ export async function ensurePackageInstalled( if (isPackageExists(dependency, { paths: [root] })) return true - const promptInstall = !process.env.CI && process.stdout.isTTY + const promptInstall = !isCI && process.stdout.isTTY process.stderr.write(c.red(`${c.inverse(c.red(' MISSING DEP '))} Can not find dependency '${dependency}'\n\n`)) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd884b1089aa..c160b72af4b4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -871,6 +871,7 @@ importers: prompts: ^2.4.2 rollup: ^2.79.1 source-map: ^0.6.1 + std-env: ^3.3.1 strip-ansi: ^7.0.1 strip-literal: ^1.0.0 tinybench: ^2.3.1 @@ -893,6 +894,7 @@ importers: local-pkg: 0.4.2 picocolors: 1.0.0 source-map: 0.6.1 + std-env: 3.3.1 strip-literal: 1.0.0 tinybench: 2.3.1 tinypool: 0.3.0 @@ -19118,6 +19120,10 @@ packages: engines: {node: '>= 0.8'} dev: true + /std-env/3.3.1: + resolution: {integrity: sha512-3H20QlwQsSm2OvAxWIYhs+j01MzzqwMwGiiO1NQaJYZgJZFPuAbf95/DiKRBSTYIJ2FeGUc+B/6mPGcWP9dO3Q==} + dev: false + /store2/2.14.2: resolution: {integrity: sha512-siT1RiqlfQnGqgT/YzXVUNsom9S0H1OX+dpdGN1xkyYATo4I6sep5NmsRD/40s3IIOvlCq6akxkqG82urIZW1w==} dev: true