UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

87 lines 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateFixtureConfig = updateFixtureConfig; const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const ts = require("typescript"); /** * Add fixture configuration * @param options {@link RuleFactory.options} * @param options.projectName * @param options.testingFramework */ function updateFixtureConfig(options) { const oldPaths = ['@otter/testing/core', '@otter/testing/core/*']; /** * Update test tsconfig * @param tree * @param context */ const updateTestTsconfig = (tree, context) => { const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined; if (!workspaceProject) { return; } const testTarget = workspaceProject.architect && workspaceProject.architect.test; const tsconfigPath = testTarget && testTarget.options && testTarget.options.tsConfig; if (tsconfigPath && tree.exists(tsconfigPath)) { const tsconfigFile = tree.readJson(tsconfigPath); tsconfigFile.compilerOptions ||= {}; const tsconfigCompilerOptions = tsconfigFile.compilerOptions; tsconfigCompilerOptions.lib = [...(tsconfigCompilerOptions.lib || []), 'dom', 'es2020', 'scripthost' ].reduce((libs, lib) => { if (!libs.includes(lib)) { libs.push(lib); } return libs; }, []); const testFramework = options.testingFramework || (0, schematics_2.getTestFramework)((0, schematics_2.getWorkspaceConfig)(tree), context); if (testFramework === 'jest') { tsconfigCompilerOptions.types ||= []; if (!tsconfigCompilerOptions.types.includes('jest')) { tsconfigCompilerOptions.types.push('jest'); } tsconfigCompilerOptions.types = tsconfigCompilerOptions.types.filter((tsType) => tsType !== 'jasmine'); } tsconfigCompilerOptions.esModuleInterop = true; tsconfigCompilerOptions.outDir = 'test'; tsconfigFile.include ||= []; tsconfigFile.include.concat(['**/fixture/', '**/*.fixture.ts', '**/*-fixture.ts', '**/fixtures.ts']); tree.overwrite(tsconfigPath, JSON.stringify(tsconfigFile, null, 2)); } return; }; /** * Update base tsconfig paths with o3r fixtures * @param tree * @param context */ const updateBaseTsConfigWithO3rFixtures = (tree, context) => { const tsconfigs = ['tsconfig.base.json', 'tsconfig.build.json', 'tsconfig.json']; const configs = tsconfigs .filter((tsconfig) => tree.exists(tsconfig)) .map((tsconfig) => ({ tsconfig, content: ts.parseConfigFileTextToJson(tsconfig, tree.readText(tsconfig)).config })) .filter(({ content }) => !!content); if (configs.length === 0) { context.logger.warn('No base tsconfig found, the path mapping for otter fixtures will not be updated'); return tree; } const configWithPath = configs.find((config) => !!config.content?.compilerOptions?.paths) || configs[0]; configWithPath.content.compilerOptions.paths = Object.entries(configWithPath.content.compilerOptions.paths || {}).reduce((acc, [key, value]) => { if (!oldPaths.includes(key)) { acc[key] = value; } return acc; }, {}); tree.overwrite(configWithPath.tsconfig, JSON.stringify(configWithPath.content, null, 2)); return tree; }; return (0, schematics_1.chain)([updateTestTsconfig, updateBaseTsConfigWithO3rFixtures]); } //# sourceMappingURL=index.js.map