Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support resolving jest-junit file dependencies #835

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
6 changes: 5 additions & 1 deletion packages/knip/fixtures/plugins/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ module.exports = {
reporters: [
'default',
'jest-silent-reporter',
['jest-junit', { outputDirectory: 'reports', outputName: 'report.xml' }],
['jest-junit', {
outputDirectory: 'reports', outputName: 'report.xml',
testSuitePropertiesFile: 'customSuiteProperties.cjs',
testSuitePropertiesDirectory: '<rootDir>',
}],
['github-actions', { silent: false }],
'summary',
],
Expand Down
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/jest/junitProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
3 changes: 2 additions & 1 deletion packages/knip/fixtures/plugins/jest2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {},
"devDependencies": {
"jest": "*",
"@testing-library/jest-dom": "*"
"@testing-library/jest-dom": "*",
"jest-junit": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
12 changes: 12 additions & 0 deletions packages/knip/fixtures/plugins/jest2/project1/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ module.exports = {
rootDir: path.join(__dirname, '../'),
displayName: 'project1',
setupFilesAfterEnv: ['<rootDir>/project1/setupFiles/setup.js'],
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: '<rootDir>',
outputName: 'junit-vcr.xml',
testCasePropertiesFile: 'customProperties.cjs',
testCasePropertiesDirectory: '<rootDir>/project1',
},
],
],
};
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];
};
24 changes: 4 additions & 20 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 @@ -65,11 +53,7 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
? [config.testEnvironment]
: [];
const resolvers = config.resolver ? [config.resolver] : [];
const reporters = config.reporters
? config.reporters
.map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
.filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
: [];
const reporters = getReportersDependencies(config, options);
const watchPlugins =
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
const transform = config.transform
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/plugins/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('Find dependencies with the Jest plugin', async () => {
devDependencies: 1,
unlisted: 3,
unresolved: 9,
processed: 6,
total: 6,
processed: 8,
total: 8,
});
});
4 changes: 2 additions & 2 deletions packages/knip/test/plugins/jest2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('Find dependencies with the Jest plugin', async () => {
devDependencies: 1,
unlisted: 0,
unresolved: 0,
processed: 5,
total: 5,
processed: 7,
total: 7,
});
});