Skip to content

Commit 7a53029

Browse files
authoredNov 14, 2024··
Support resolving jest-junit file dependencies (#835)
1 parent 153a836 commit 7a53029

File tree

11 files changed

+86
-26
lines changed

11 files changed

+86
-26
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

‎packages/knip/fixtures/plugins/jest/jest.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ module.exports = {
1414
reporters: [
1515
'default',
1616
'jest-silent-reporter',
17-
['jest-junit', { outputDirectory: 'reports', outputName: 'report.xml' }],
17+
['jest-junit', {
18+
outputDirectory: 'reports', outputName: 'report.xml',
19+
testSuitePropertiesFile: 'customSuiteProperties.cjs',
20+
testSuitePropertiesDirectory: '<rootDir>',
21+
}],
1822
['github-actions', { silent: false }],
1923
'summary',
2024
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

‎packages/knip/fixtures/plugins/jest2/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"scripts": {},
44
"devDependencies": {
55
"jest": "*",
6-
"@testing-library/jest-dom": "*"
6+
"@testing-library/jest-dom": "*",
7+
"jest-junit": "*"
78
}
89
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

‎packages/knip/fixtures/plugins/jest2/project1/jest.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@ module.exports = {
44
rootDir: path.join(__dirname, '../'),
55
displayName: 'project1',
66
setupFilesAfterEnv: ['<rootDir>/project1/setupFiles/setup.js'],
7+
reporters: [
8+
'default',
9+
[
10+
'jest-junit',
11+
{
12+
outputDirectory: '<rootDir>',
13+
outputName: 'junit-vcr.xml',
14+
testCasePropertiesFile: 'customProperties.cjs',
15+
testCasePropertiesDirectory: '<rootDir>/project1',
16+
},
17+
],
18+
],
719
};
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import type { PluginOptions } from '../../types/config.js';
2+
import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
3+
import { load } from '../../util/plugin.js';
4+
import type { JestInitialOptions } from './types.js';
5+
6+
export const resolveExtensibleConfig = async (configFilePath: string) => {
7+
let config = await load(configFilePath);
8+
if (config?.preset) {
9+
const { preset } = config;
10+
if (isInternal(preset)) {
11+
const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
12+
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
13+
config = Object.assign({}, presetConfig, config);
14+
}
15+
}
16+
return config;
17+
};
18+
19+
const getStringPropOrFallback = (prop: unknown, fallback: string): string => {
20+
return typeof prop === 'string' ? prop : fallback;
21+
};
22+
23+
export const getReportersDependencies = (config: JestInitialOptions, options: PluginOptions) => {
24+
// Resolve dependencies for jest-junit reporter config
25+
const jUnitReporterDeps: string[] = [];
26+
for (const reporter of config.reporters ?? []) {
27+
if (typeof reporter !== 'string' && reporter[0] === 'jest-junit') {
28+
const {
29+
testCasePropertiesFile,
30+
testCasePropertiesDirectory,
31+
testSuitePropertiesFile,
32+
testSuitePropertiesDirectory,
33+
} = reporter[1];
34+
35+
const testCaseFileName = getStringPropOrFallback(testCasePropertiesFile, 'junitProperties.js');
36+
const testCaseDirectory = getStringPropOrFallback(testCasePropertiesDirectory, options.rootCwd);
37+
const testCaseFilePath = join(testCaseDirectory, testCaseFileName);
38+
39+
const testSuiteFileName = getStringPropOrFallback(testSuitePropertiesFile, 'junitTestCaseProperties.js');
40+
const testSuiteDirectory = getStringPropOrFallback(testSuitePropertiesDirectory, options.rootCwd);
41+
const testSuiteFilePath = join(testSuiteDirectory, testSuiteFileName);
42+
43+
jUnitReporterDeps.push(testCaseFilePath);
44+
jUnitReporterDeps.push(testSuiteFilePath);
45+
}
46+
}
47+
48+
const reporters = config.reporters
49+
? config.reporters
50+
.map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
51+
.filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
52+
: [];
53+
54+
return [...reporters, ...jUnitReporterDeps];
55+
};

‎packages/knip/src/plugins/jest/index.ts

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '../../types/config.js';
22
import { type Input, toDeferResolve, toEntry } from '../../util/input.js';
3-
import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
4-
import { hasDependency, load } from '../../util/plugin.js';
3+
import { isInternal, join, toAbsolute } from '../../util/path.js';
4+
import { hasDependency } from '../../util/plugin.js';
5+
import { getReportersDependencies, resolveExtensibleConfig } from './helpers.js';
56
import type { JestConfig, JestInitialOptions } from './types.js';
67

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

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

20-
const resolveExtensibleConfig = async (configFilePath: string) => {
21-
let config = await load(configFilePath);
22-
if (config?.preset) {
23-
const { preset } = config;
24-
if (isInternal(preset)) {
25-
const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
26-
const presetConfig = await resolveExtensibleConfig(presetConfigPath);
27-
config = Object.assign({}, presetConfig, config);
28-
}
29-
}
30-
return config;
31-
};
32-
3321
const resolveDependencies = async (config: JestInitialOptions, options: PluginOptions): Promise<Input[]> => {
3422
const { configFileDir } = options;
3523

@@ -65,11 +53,7 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
6553
? [config.testEnvironment]
6654
: [];
6755
const resolvers = config.resolver ? [config.resolver] : [];
68-
const reporters = config.reporters
69-
? config.reporters
70-
.map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
71-
.filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
72-
: [];
56+
const reporters = getReportersDependencies(config, options);
7357
const watchPlugins =
7458
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
7559
const transform = config.transform

‎packages/knip/test/plugins/jest.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('Find dependencies with the Jest plugin', async () => {
3131
devDependencies: 1,
3232
unlisted: 3,
3333
unresolved: 9,
34-
processed: 6,
35-
total: 6,
34+
processed: 8,
35+
total: 8,
3636
});
3737
});

‎packages/knip/test/plugins/jest2.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test('Find dependencies with the Jest plugin', async () => {
2828
devDependencies: 1,
2929
unlisted: 0,
3030
unresolved: 0,
31-
processed: 5,
32-
total: 5,
31+
processed: 7,
32+
total: 7,
3333
});
3434
});

0 commit comments

Comments
 (0)
Please sign in to comment.