Skip to content

Commit

Permalink
create a helpers.ts file for jest plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tryggvigy committed Nov 13, 2024
1 parent a9e6bb2 commit ca2f994
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
55 changes: 55 additions & 0 deletions packages/knip/src/plugins/jest/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { PluginOptions } from '../../types/config.js';
import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
import { load } from '../../util/plugin.js';
import type { JestInitialOptions } from './types.js';

export const resolveExtensibleConfig = async (configFilePath: string) => {
let config = await load(configFilePath);
if (config?.preset) {
const { preset } = config;
if (isInternal(preset)) {
const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
config = Object.assign({}, presetConfig, config);
}
}
return config;
};

const getStringPropOrFallback = (prop: unknown, fallback: string): string => {
return typeof prop === 'string' ? prop : fallback;
};

export const getReportersDependencies = (config: JestInitialOptions, options: PluginOptions) => {
// Resolve dependencies for jest-junit reporter config
const jUnitReporterDeps: string[] = [];
for (const reporter of config.reporters ?? []) {
if (typeof reporter !== 'string' && reporter[0] === 'jest-junit') {
const {
testCasePropertiesFile,
testCasePropertiesDirectory,
testSuitePropertiesFile,
testSuitePropertiesDirectory,
} = reporter[1];

const testCaseFileName = getStringPropOrFallback(testCasePropertiesFile, 'junitProperties.js');
const testCaseDirectory = getStringPropOrFallback(testCasePropertiesDirectory, options.rootCwd);
const testCaseFilePath = join(testCaseDirectory, testCaseFileName);

const testSuiteFileName = getStringPropOrFallback(testSuitePropertiesFile, 'junitTestCaseProperties.js');
const testSuiteDirectory = getStringPropOrFallback(testSuitePropertiesDirectory, options.rootCwd);
const testSuiteFilePath = join(testSuiteDirectory, testSuiteFileName);

jUnitReporterDeps.push(testCaseFilePath);
jUnitReporterDeps.push(testSuiteFilePath);
}
}

const reporters = config.reporters
? config.reporters
.map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
.filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
: [];

return [...reporters, ...jUnitReporterDeps];
};
53 changes: 4 additions & 49 deletions packages/knip/src/plugins/jest/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '../../types/config.js';
import { type Input, toDeferResolve, toEntry } from '../../util/input.js';
import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
import { hasDependency, load } from '../../util/plugin.js';
import { isInternal, join, toAbsolute } from '../../util/path.js';
import { hasDependency } from '../../util/plugin.js';
import { getReportersDependencies, resolveExtensibleConfig } from './helpers.js';
import type { JestConfig, JestInitialOptions } from './types.js';

// https://jestjs.io/docs/configuration
Expand All @@ -17,19 +18,6 @@ const config = ['jest.config.{js,ts,mjs,cjs,json}', 'package.json'];

const entry = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'];

const resolveExtensibleConfig = async (configFilePath: string) => {
let config = await load(configFilePath);
if (config?.preset) {
const { preset } = config;
if (isInternal(preset)) {
const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
config = Object.assign({}, presetConfig, config);
}
}
return config;
};

const resolveDependencies = async (config: JestInitialOptions, options: PluginOptions): Promise<Input[]> => {
const { configFileDir } = options;

Expand Down Expand Up @@ -60,40 +48,7 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
const runtime = config.runtime && config.runtime !== 'jest-circus' ? [config.runtime] : [];
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
const resolvers = config.resolver ? [config.resolver] : [];

// Resolve dependencies for jest-junit reporter config
const jUnitReporterDeps: Array<string> = [];
for (const reporter of config.reporters ?? []) {
if (typeof reporter !== 'string' && reporter[0] === 'jest-junit') {
const {
testCasePropertiesFile,
testCasePropertiesDirectory,
testSuitePropertiesFile,
testSuitePropertiesDirectory,
} = reporter[1];
const testCaseFileName =
typeof testCasePropertiesFile === 'string' ? testCasePropertiesFile : 'junitProperties.js';
const testCaseDirectory =
typeof testCasePropertiesDirectory === 'string' ? testCasePropertiesDirectory : options.rootCwd;
const testSuiteFileName =
typeof testSuitePropertiesFile === 'string' ? testSuitePropertiesFile : 'junitTestCaseProperties.js';
const testSuiteDirectory =
typeof testSuitePropertiesDirectory === 'string' ? testSuitePropertiesDirectory : options.rootCwd;
const testCaseFilePath = join(testCaseDirectory, testCaseFileName);
const testSuiteFilePath = join(testSuiteDirectory, testSuiteFileName);
jUnitReporterDeps.push(testCaseFilePath);
jUnitReporterDeps.push(testSuiteFilePath);
}
}

let reporters = config.reporters
? config.reporters
.map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
.filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
: [];

reporters = [...reporters, ...jUnitReporterDeps];

const reporters = getReportersDependencies(config, options);
const watchPlugins =
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
const transform = config.transform
Expand Down

0 comments on commit ca2f994

Please sign in to comment.