UNPKG

jest-preset-angular

Version:

Jest preset configuration for Angular projects

100 lines (99 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NgJestTransformer = void 0; const node_crypto_1 = require("node:crypto"); const bs_logger_1 = require("bs-logger"); const esbuild_1 = require("esbuild"); const ts_jest_1 = require("ts-jest"); const compiler_utils_1 = require("ts-jest/dist/legacy/compiler/compiler-utils"); const ng_jest_compiler_1 = require("./compiler/ng-jest-compiler"); const ng_jest_config_1 = require("./config/ng-jest-config"); const cache = {}; const sha1 = (...data) => { const canCache = data.length === 1 && typeof data[0] === 'string'; let cacheKey; if (canCache) { cacheKey = data[0]; if (cacheKey in cache) { return cache[cacheKey]; } } const hash = (0, node_crypto_1.createHash)('sha1'); data.forEach((item) => { if (typeof item === 'string') { hash.update(item, 'utf8'); } else { hash.update(item); } }); const res = hash.digest('hex').toString(); if (canCache) { cache[cacheKey] = res; } return res; }; class NgJestTransformer extends ts_jest_1.TsJestTransformer { #ngJestLogger; constructor(tsJestConfig) { super(tsJestConfig); this.#ngJestLogger = (0, bs_logger_1.createLogger)({ context: { [bs_logger_1.LogContexts.package]: 'jest-preset-angular', [bs_logger_1.LogContexts.logLevel]: bs_logger_1.LogLevels.trace, version: this.version, }, targets: process.env.NG_JEST_LOG ?? undefined, }); } _createConfigSet(config) { return new ng_jest_config_1.NgJestConfig(config); } _createCompiler(configSet, cacheFS) { this._compiler = new ng_jest_compiler_1.NgJestCompiler(configSet, cacheFS); } get version() { return require('../package.json').version; } process(fileContent, filePath, transformOptions) { const configSet = super._configsFor(transformOptions); if (configSet.processWithEsbuild(filePath)) { this.#ngJestLogger.debug({ filePath }, 'process with esbuild'); const compilerOpts = configSet.parsedTsConfig.options; const { code, map } = (0, esbuild_1.transformSync)(fileContent, { loader: 'js', format: transformOptions.supportsStaticESM && configSet.useESM ? 'esm' : 'cjs', target: compilerOpts.target === configSet.compilerModule.ScriptTarget.ES2015 ? 'es2015' : 'es2016', sourcemap: compilerOpts.sourceMap, sourcefile: filePath, sourcesContent: true, sourceRoot: compilerOpts.sourceRoot, }); return { code, map, }; } else { if (filePath.includes('node_modules') && filePath.endsWith('.js')) { const result = configSet.compilerModule.transpileModule(fileContent, { compilerOptions: { ...configSet.parsedTsConfig.options, module: transformOptions.supportsStaticESM && configSet.useESM ? configSet.parsedTsConfig.options.module : configSet.compilerModule.ModuleKind.CommonJS, }, fileName: filePath, }); return { code: (0, compiler_utils_1.updateOutput)(result.outputText, filePath, result.sourceMapText), }; } return super.process(fileContent, filePath, transformOptions); } } getCacheKey(fileContent, filePath, transformOptions) { return sha1(super.getCacheKey(fileContent, filePath, transformOptions), this.version); } } exports.NgJestTransformer = NgJestTransformer;