From 7a53029cce7bc2740a4814515971b5d308d306a6 Mon Sep 17 00:00:00 2001 From: Tryggvi Gylfason Date: Thu, 14 Nov 2024 07:57:50 +0100 Subject: [PATCH] Support resolving jest-junit file dependencies (#835) --- .../plugins/jest/customSuiteProperties.cjs | 1 + .../knip/fixtures/plugins/jest/jest.config.js | 6 +- .../fixtures/plugins/jest/junitProperties.js | 1 + .../plugins/jest2/junitTestCaseProperties.js | 1 + .../knip/fixtures/plugins/jest2/package.json | 3 +- .../jest2/project1/customProperties.cjs | 1 + .../plugins/jest2/project1/jest.config.js | 12 ++++ packages/knip/src/plugins/jest/helpers.ts | 55 +++++++++++++++++++ packages/knip/src/plugins/jest/index.ts | 24 ++------ packages/knip/test/plugins/jest.test.ts | 4 +- packages/knip/test/plugins/jest2.test.ts | 4 +- 11 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 packages/knip/fixtures/plugins/jest/customSuiteProperties.cjs create mode 100644 packages/knip/fixtures/plugins/jest/junitProperties.js create mode 100644 packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js create mode 100644 packages/knip/fixtures/plugins/jest2/project1/customProperties.cjs create mode 100644 packages/knip/src/plugins/jest/helpers.ts diff --git a/packages/knip/fixtures/plugins/jest/customSuiteProperties.cjs b/packages/knip/fixtures/plugins/jest/customSuiteProperties.cjs new file mode 100644 index 000000000..7c6d6c73d --- /dev/null +++ b/packages/knip/fixtures/plugins/jest/customSuiteProperties.cjs @@ -0,0 +1 @@ +module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest/jest.config.js b/packages/knip/fixtures/plugins/jest/jest.config.js index 763ba8288..646b56dd2 100644 --- a/packages/knip/fixtures/plugins/jest/jest.config.js +++ b/packages/knip/fixtures/plugins/jest/jest.config.js @@ -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: '', + }], ['github-actions', { silent: false }], 'summary', ], diff --git a/packages/knip/fixtures/plugins/jest/junitProperties.js b/packages/knip/fixtures/plugins/jest/junitProperties.js new file mode 100644 index 000000000..7c6d6c73d --- /dev/null +++ b/packages/knip/fixtures/plugins/jest/junitProperties.js @@ -0,0 +1 @@ +module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js b/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js new file mode 100644 index 000000000..7c6d6c73d --- /dev/null +++ b/packages/knip/fixtures/plugins/jest2/junitTestCaseProperties.js @@ -0,0 +1 @@ +module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest2/package.json b/packages/knip/fixtures/plugins/jest2/package.json index 4a3030c00..a81e037bd 100644 --- a/packages/knip/fixtures/plugins/jest2/package.json +++ b/packages/knip/fixtures/plugins/jest2/package.json @@ -3,6 +3,7 @@ "scripts": {}, "devDependencies": { "jest": "*", - "@testing-library/jest-dom": "*" + "@testing-library/jest-dom": "*", + "jest-junit": "*" } } diff --git a/packages/knip/fixtures/plugins/jest2/project1/customProperties.cjs b/packages/knip/fixtures/plugins/jest2/project1/customProperties.cjs new file mode 100644 index 000000000..7c6d6c73d --- /dev/null +++ b/packages/knip/fixtures/plugins/jest2/project1/customProperties.cjs @@ -0,0 +1 @@ +module.exports = {} \ No newline at end of file diff --git a/packages/knip/fixtures/plugins/jest2/project1/jest.config.js b/packages/knip/fixtures/plugins/jest2/project1/jest.config.js index 4f7d89c2d..65850ae7a 100644 --- a/packages/knip/fixtures/plugins/jest2/project1/jest.config.js +++ b/packages/knip/fixtures/plugins/jest2/project1/jest.config.js @@ -4,4 +4,16 @@ module.exports = { rootDir: path.join(__dirname, '../'), displayName: 'project1', setupFilesAfterEnv: ['/project1/setupFiles/setup.js'], + reporters: [ + 'default', + [ + 'jest-junit', + { + outputDirectory: '', + outputName: 'junit-vcr.xml', + testCasePropertiesFile: 'customProperties.cjs', + testCasePropertiesDirectory: '/project1', + }, + ], + ], }; diff --git a/packages/knip/src/plugins/jest/helpers.ts b/packages/knip/src/plugins/jest/helpers.ts new file mode 100644 index 000000000..81984e14e --- /dev/null +++ b/packages/knip/src/plugins/jest/helpers.ts @@ -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]; +}; diff --git a/packages/knip/src/plugins/jest/index.ts b/packages/knip/src/plugins/jest/index.ts index 2bfe09501..f3a8b9f6e 100644 --- a/packages/knip/src/plugins/jest/index.ts +++ b/packages/knip/src/plugins/jest/index.ts @@ -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 @@ -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 => { const { configFileDir } = options; @@ -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 diff --git a/packages/knip/test/plugins/jest.test.ts b/packages/knip/test/plugins/jest.test.ts index 6a38993e1..3a036eba4 100644 --- a/packages/knip/test/plugins/jest.test.ts +++ b/packages/knip/test/plugins/jest.test.ts @@ -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, }); }); diff --git a/packages/knip/test/plugins/jest2.test.ts b/packages/knip/test/plugins/jest2.test.ts index e85de0323..8819d3bf8 100644 --- a/packages/knip/test/plugins/jest2.test.ts +++ b/packages/knip/test/plugins/jest2.test.ts @@ -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, }); });