|
| 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 | +}; |
0 commit comments