|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs"; |
| 3 | +import { JestHTMLReporterConfiguration } from "./types"; |
| 4 | +import { parseBoolean, parseNumber, parseString } from "./utils"; |
| 5 | + |
| 6 | +const defaultValues: JestHTMLReporterConfiguration = { |
| 7 | + append: false, |
| 8 | + boilerplate: undefined, |
| 9 | + collapseSuitesByDefault: false, |
| 10 | + customScriptPath: undefined, |
| 11 | + dateFormat: "yyyy-mm-dd HH:MM:ss", |
| 12 | + executionTimeWarningThreshold: 5, |
| 13 | + includeConsoleLog: false, |
| 14 | + includeFailureMsg: false, |
| 15 | + includeStackTrace: true, |
| 16 | + includeSuiteFailure: false, |
| 17 | + includeObsoleteSnapshots: false, |
| 18 | + logo: undefined, |
| 19 | + outputPath: path.join(process.cwd(), "test-report.html"), |
| 20 | + pageTitle: "Test Report", |
| 21 | + sort: undefined, |
| 22 | + statusIgnoreFilter: undefined, |
| 23 | + styleOverridePath: undefined, |
| 24 | + theme: "defaultTheme", |
| 25 | + useCssFile: false, |
| 26 | +}; |
| 27 | + |
| 28 | +// Convert config key to environment variable format |
| 29 | +export function toEnvVar(key: string): string { |
| 30 | + return `JEST_HTML_REPORTER_${key |
| 31 | + .replace(/([a-z])([A-Z])/g, "$1_$2") |
| 32 | + .toUpperCase()}`; |
| 33 | +} |
| 34 | + |
| 35 | +// Read JSON file safely |
| 36 | +export function readJsonFile(filePath: string) { |
| 37 | + try { |
| 38 | + if (fs.existsSync(filePath)) { |
| 39 | + return JSON.parse(fs.readFileSync(filePath, "utf8")); |
| 40 | + } |
| 41 | + } catch { |
| 42 | + // Ignore errors |
| 43 | + return {}; |
| 44 | + } |
| 45 | + return {}; |
| 46 | +} |
| 47 | + |
| 48 | +// Type conversion functions |
| 49 | +const typeParsers: { |
| 50 | + [key in keyof JestHTMLReporterConfiguration]: ( |
| 51 | + value: unknown |
| 52 | + ) => string | number | boolean | undefined; |
| 53 | +} = { |
| 54 | + append: parseBoolean, |
| 55 | + boilerplate: parseString, |
| 56 | + collapseSuitesByDefault: parseBoolean, |
| 57 | + customScriptPath: parseString, |
| 58 | + dateFormat: parseString, |
| 59 | + executionTimeWarningThreshold: parseNumber, |
| 60 | + includeConsoleLog: parseBoolean, |
| 61 | + includeFailureMsg: parseBoolean, |
| 62 | + includeStackTrace: parseBoolean, |
| 63 | + includeSuiteFailure: parseBoolean, |
| 64 | + includeObsoleteSnapshots: parseBoolean, |
| 65 | + logo: parseString, |
| 66 | + outputPath: parseString, |
| 67 | + pageTitle: parseString, |
| 68 | + sort: parseString, |
| 69 | + statusIgnoreFilter: parseString, |
| 70 | + styleOverridePath: parseString, |
| 71 | + theme: parseString, |
| 72 | + useCssFile: parseBoolean, |
| 73 | +}; |
| 74 | + |
| 75 | +// Function to clean & validate configuration input |
| 76 | +export function sanitizeConfig( |
| 77 | + input: unknown |
| 78 | +): Partial<JestHTMLReporterConfiguration> { |
| 79 | + if (typeof input !== "object" || input === null) { |
| 80 | + return {}; |
| 81 | + } |
| 82 | + |
| 83 | + return ( |
| 84 | + Object.keys(defaultValues) as (keyof JestHTMLReporterConfiguration)[] |
| 85 | + ).reduce((sanitized, key) => { |
| 86 | + const parser = typeParsers[key]; |
| 87 | + if (parser && typeof parser === "function" && key in input) { |
| 88 | + const value = parser((input as JestHTMLReporterConfiguration)[key]); |
| 89 | + if (value !== undefined) { |
| 90 | + return { ...sanitized, [key]: value }; |
| 91 | + } |
| 92 | + } |
| 93 | + return sanitized; |
| 94 | + }, {}); |
| 95 | +} |
| 96 | + |
| 97 | +export default function (cliConfig: unknown): JestHTMLReporterConfiguration { |
| 98 | + // Read from environment variables |
| 99 | + const envConfig: Partial<JestHTMLReporterConfiguration> = ( |
| 100 | + Object.keys(defaultValues) as (keyof JestHTMLReporterConfiguration)[] |
| 101 | + ).reduce((config, key) => { |
| 102 | + const envKey = toEnvVar(key); |
| 103 | + if (process.env[envKey] !== undefined) { |
| 104 | + return { ...config, [key]: process.env[envKey] }; |
| 105 | + } |
| 106 | + return config; |
| 107 | + }, {}); |
| 108 | + |
| 109 | + // Read from JSON files |
| 110 | + const customJsonConfig = readJsonFile( |
| 111 | + path.join(process.cwd(), "jesthtmlreporter.config.json") |
| 112 | + ); |
| 113 | + const packageJsonConfig = |
| 114 | + readJsonFile(path.join(process.cwd(), "package.json"))?.jest || {}; |
| 115 | + |
| 116 | + // Merge configurations in priority order (with sanitization) |
| 117 | + return { |
| 118 | + ...defaultValues, // Default values (lowest priority) |
| 119 | + ...sanitizeConfig(packageJsonConfig), // package.json "jest" section |
| 120 | + ...sanitizeConfig(customJsonConfig), // Custom JSON config |
| 121 | + ...sanitizeConfig(cliConfig), // CLI parameters |
| 122 | + ...sanitizeConfig(envConfig), // Environment variables (highest priority) |
| 123 | + }; |
| 124 | +} |
0 commit comments