UNPKG

jest-preset-angular

Version:

Jest preset configuration for Angular projects

111 lines (110 loc) 5.24 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _NgJestTransformer_ngJestLogger; 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 { constructor(tsJestConfig) { var _a; super(tsJestConfig); _NgJestTransformer_ngJestLogger.set(this, void 0); __classPrivateFieldSet(this, _NgJestTransformer_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: (_a = process.env.NG_JEST_LOG) !== null && _a !== void 0 ? _a : undefined, }), "f"); } _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)) { __classPrivateFieldGet(this, _NgJestTransformer_ngJestLogger, "f").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: Object.assign(Object.assign({}, 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; _NgJestTransformer_ngJestLogger = new WeakMap();