Skip to content

Commit

Permalink
Don't return null for vitest environment (fixes #575)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Mar 27, 2024
1 parent aa19491 commit 141b701
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['**/*.vitest.ts'],
environment: 'happy-dom',
environment: 'node',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"devDependencies": {
"eslint-plugin-no-secrets": "*",
"eslint-plugin-prettier": "*",
"happy-dom": "*",
"vitest": "^0.34.6"
}
}
7 changes: 2 additions & 5 deletions packages/knip/src/plugins/vitest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { ViteConfig } from './types.js';
*/

type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime';
type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>);

const environments = {
node: null,
Expand All @@ -22,10 +21,8 @@ const envPackageNames: Record<Exclude<keyof typeof environments, 'node'>, string
'edge-runtime': '@edge-runtime/vm',
};

export const getEnvPackageName = (env: VitestEnvironment) => {
if (env === 'node') return null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (env in envPackageNames) return (envPackageNames as any)[env];
export const getEnvPackageName = (env: string) => {
if (env in envPackageNames) return envPackageNames[env as Exclude<BuiltinEnvironment, 'node'>];
return `vitest-environment-${env}`;
};

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 @@ -42,7 +42,8 @@ const findConfigDependencies = (localConfig: ViteConfig, options: PluginOptions)

if (!testConfig) return [];

const environments = testConfig.environment ? [getEnvPackageName(testConfig.environment)] : [];
const environments =
testConfig.environment && testConfig.environment !== 'node' ? [getEnvPackageName(testConfig.environment)] : [];
const reporters = getExternalReporters(testConfig.reporters);

const hasCoverageEnabled =
Expand Down

0 comments on commit 141b701

Please sign in to comment.