UNPKG

@angular-builders/jest

Version:

Jest runner for Angular build facade. Allows ng test run with Jest instead of Karma

66 lines 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomConfigResolver = void 0; const fs_1 = require("fs"); const core_1 = require("@angular-devkit/core"); const common_1 = require("@angular-builders/common"); const utils_1 = require("./utils"); class CustomConfigResolver { options; logger; // https://jestjs.io/docs/configuration allowedExtensions = ['js', 'ts', 'mjs', 'cjs', 'json']; constructor(options, logger) { this.options = options; this.logger = logger; } async resolveGlobal(workspaceRoot) { const packageJsonPath = (0, core_1.getSystemPath)((0, core_1.join)(workspaceRoot, 'package.json')); const packageJson = require(packageJsonPath); if (packageJson.jest) { return packageJson.jest; } const tsConfig = (0, utils_1.getTsConfigPath)(workspaceRoot, this.options); const workspaceJestConfigPaths = this.allowedExtensions.map(extension => (0, core_1.getSystemPath)((0, core_1.join)(workspaceRoot, `jest.config.${extension}`))); const workspaceJestConfigPath = workspaceJestConfigPaths.find(path => (0, fs_1.existsSync)(path)); if (!workspaceJestConfigPath) { return {}; } return await (0, common_1.loadModule)(workspaceJestConfigPath, tsConfig, this.logger); } async resolveForProject(projectRoot, config) { // If config is already an object (from angular.json), return it directly if (typeof config === 'object' && config !== null) { return config; } // At this point, config is guaranteed to be a string const configPath = config; // Try parsing string as JSON (like Jest CLI behavior) const inlineConfig = this.tryParseJsonConfig(configPath); if (inlineConfig !== null) { return inlineConfig; } // Treat as file path const jestConfigPath = (0, core_1.getSystemPath)((0, core_1.join)(projectRoot, configPath)); if (!(0, fs_1.existsSync)(jestConfigPath)) { this.logger.warn(`warning: unable to locate custom jest configuration file at path "${jestConfigPath}"`); return {}; } const tsConfig = (0, utils_1.getTsConfigPath)(projectRoot, this.options); return await (0, common_1.loadModule)(jestConfigPath, tsConfig, this.logger); } tryParseJsonConfig(config) { try { const parsed = JSON.parse(config); if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) { return parsed; } } catch { // Not valid JSON, will be treated as file path } return null; } } exports.CustomConfigResolver = CustomConfigResolver; //# sourceMappingURL=custom-config.resolver.js.map