Skip to content

Commit

Permalink
Fix test for absence of test config in vitest plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Dec 4, 2023
1 parent c2ff2b3 commit 3b1135b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/knip/src/WorkspaceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export class WorkspaceWorker {
manifest: this.manifest,
config: pluginConfig,
isProduction: this.isProduction,
enabledPlugins: this.enabledPlugins,
});

dependencies.forEach(specifier => {
Expand Down
3 changes: 2 additions & 1 deletion packages/knip/src/plugins/vitest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export const findVitestDependencies = async (
resolveEntry(configFilePath, specifier)
);

if (!localConfig.test) return dependencies;
// When coming from the vite plugin we should not assume vitest is enabled
if (!options.enabledPlugins.includes('vitest')) return dependencies;

return [...dependencies, ...findConfigDependencies(configFilePath, localConfig, options)];
};
Expand Down
1 change: 1 addition & 0 deletions packages/knip/src/types/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type GenericPluginCallbackOptions = {
manifest: PackageJsonWithPlugins;
config: EnsuredPluginConfiguration;
isProduction: boolean;
enabledPlugins: string[];
};

export type GenericPluginCallback = (
Expand Down
3 changes: 2 additions & 1 deletion packages/knip/test/plugins/vitest-npm-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { getManifest, pluginConfig as config } from '../helpers/index.js';

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

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 });
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config, enabledPlugins });
assert.deepEqual(dependencies, ['entry:**/*.{test,spec}.?(c|m)[jt]s?(x)', '@vitest/coverage-v8']);
});
7 changes: 4 additions & 3 deletions packages/knip/test/plugins/vitest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { getManifest, pluginConfig as config } from '../helpers/index.js';

const cwd = resolve('fixtures/plugins/vitest');
const manifest = getManifest(cwd);
const enabledPlugins = ['vitest'];

test('Find dependencies in vitest configuration (vitest)', async () => {
const configFilePath = join(cwd, 'vitest.config.ts');
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config });
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config, enabledPlugins });
assert.deepEqual(dependencies, [
'entry:**/*.{test,spec}.?(c|m)[jt]s?(x)',
'happy-dom',
Expand All @@ -24,13 +25,13 @@ test('Find dependencies in vitest configuration (vitest)', async () => {

test('Find dependencies in vitest configuration without coverage providers (vitest)', async () => {
const configFilePath = join(cwd, 'vitest-default-coverage.config');
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config });
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config, enabledPlugins });
assert.deepEqual(dependencies, ['entry:**/*.{test,spec}.?(c|m)[jt]s?(x)', 'jsdom', '@vitest/coverage-v8']);
});

test('Find dependencies in vitest configuration (vite)', async () => {
const configFilePath = join(cwd, 'vite.config.ts');
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config });
const dependencies = await vitest.findDependencies(configFilePath, { cwd, manifest, config, enabledPlugins });
assert.deepEqual(dependencies, [
'entry:**/*.{test,spec}.?(c|m)[jt]s?(x)',
'@vitest/coverage-c8',
Expand Down

0 comments on commit 3b1135b

Please sign in to comment.