Skip to content

Commit

Permalink
vitest: Detect whether coverage is enabled in npm scripts (#378)
Browse files Browse the repository at this point in the history
* vitest: Detect whether coverage is enabled in npm scripts

* use regular expression for detecting coverage cli flag
  • Loading branch information
DMartens authored Dec 4, 2023
1 parent 358250f commit 9fc7d1f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
9 changes: 9 additions & 0 deletions packages/knip/fixtures/plugins/vitest-npm-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@fixtures/vitest-npm-script",
"scripts": {
"test:coverage": "vitest --coverage"
},
"devDependencies": {
"vitest": "^0.34.6"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
test: {
coverage: {
enabled: false,
provider: 'v8',
},
},
};
21 changes: 16 additions & 5 deletions packages/knip/src/plugins/vitest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { hasDependency, load, tryResolve } from '../../util/plugin.js';
import { toEntryPattern } from '../../util/protocols.js';
import { getEnvPackageName, getExternalReporters } from './helpers.js';
import type { ViteConfigOrFn, VitestWorkspaceConfig, ViteConfig, MODE, COMMAND } from './types.js';
import type { PackageJsonWithPlugins } from '../../types/package-json.js';
import type {
IsPluginEnabledCallback,
GenericPluginCallback,
Expand Down Expand Up @@ -36,12 +37,20 @@ const resolveEntry = (containingFilePath: string, specifier: string) => {
return specifier;
};

const enablesCoverageInScript = /vitest(.+)--coverage(?:\.enabled(?:=true)?)?/;

const hasScriptWithCoverage = (scripts: Exclude<PackageJsonWithPlugins['scripts'], undefined>) => {
return Object.values(scripts).some(script => {
return enablesCoverageInScript.test(script);
});
};

const findConfigDependencies = (
configFilePath: string,
localConfig: ViteConfig,
options: GenericPluginCallbackOptions
) => {
const { isProduction, config } = options;
const { isProduction, config, manifest } = options;
const testConfig = localConfig.test;

const entryPatterns = (config?.entry ?? testConfig?.include ?? ENTRY_FILE_PATTERNS).map(toEntryPattern);
Expand All @@ -50,10 +59,12 @@ const findConfigDependencies = (

const environments = testConfig.environment ? [getEnvPackageName(testConfig.environment)] : [];
const reporters = getExternalReporters(testConfig.reporters);
const coverage =
testConfig.coverage && testConfig.coverage.enabled !== false
? [`@vitest/coverage-${testConfig.coverage.provider ?? 'v8'}`]
: [];

const hasCoverageEnabled =
(testConfig.coverage && testConfig.coverage.enabled !== false) ||
(manifest.scripts !== undefined && hasScriptWithCoverage(manifest.scripts));
const coverage = hasCoverageEnabled ? [`@vitest/coverage-${testConfig.coverage?.provider ?? 'v8'}`] : [];

const setupFiles = [testConfig.setupFiles ?? []].flat().map(v => resolveEntry(configFilePath, v));
const globalSetup = [testConfig.globalSetup ?? []].flat().map(v => resolveEntry(configFilePath, v));
return [...entryPatterns, ...environments, ...reporters, ...coverage, ...setupFiles, ...globalSetup];
Expand Down
2 changes: 1 addition & 1 deletion packages/knip/test/plugins/prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ test('Find dependencies in Prettier configuration (.prettierrc)', async () => {

test('Find dependencies in Prettier configuration (package.json)', async () => {
const configFilePath = join(cwd, 'package.json');
const dependencies = await prettier.findDependencies(configFilePath, {manifest});
const dependencies = await prettier.findDependencies(configFilePath, { manifest });
assert.deepEqual(dependencies, ['@company/prettier-config']);
});
14 changes: 14 additions & 0 deletions packages/knip/test/plugins/vitest-npm-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import * as vitest from '../../src/plugins/vitest/index.js';
import { resolve, join } from '../../src/util/path.js';
import { getManifest, pluginConfig as config } from '../helpers/index.js';

const cwd = resolve('fixtures/plugins/vitest-npm-script');
const manifest = getManifest(cwd);

test('detects the coverage dependency when a npm script activates coverage', async () => {
const configFilePath = join(cwd, 'vitest.config.ts');
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config });
assert.deepEqual(dependencies, ['entry:**/*.{test,spec}.?(c|m)[jt]s?(x)', '@vitest/coverage-v8']);
});

0 comments on commit 9fc7d1f

Please sign in to comment.