UNPKG

@o3r/testing

Version:

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

155 lines • 8.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngAdd = void 0; const fs = require("node:fs"); const path = require("node:path"); const prompt_1 = require("@angular/cli/src/utilities/prompt"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const fixture_1 = require("./fixture"); const playwright_1 = require("./playwright"); const devDependenciesToInstall = [ 'pixelmatch', 'pngjs', 'jest', 'jest-environment-jsdom', 'jest-preset-angular', 'ts-jest', '@angular-builders/jest', '@angular-devkit/build-angular', '@types/jest' ]; /** * Add Otter testing to an Angular Project * @param options */ function ngAddFn(options) { return async (tree, context) => { try { const testPackageJsonPath = path.resolve(__dirname, '..', '..', 'package.json'); const packageJson = JSON.parse(fs.readFileSync(testPackageJsonPath, { encoding: 'utf8' })); const depsInfo = (0, schematics_2.getO3rPeerDeps)(testPackageJsonPath); const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined; const workingDirectory = workspaceProject?.root || '.'; const projectType = workspaceProject?.projectType || 'application'; const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => { acc[dep] = { inManifest: [{ range: `${options.exactO3rVersion ? '' : '~'}${depsInfo.packageVersion}`, types: (0, schematics_2.getProjectNewDependenciesTypes)(workspaceProject) }], ngAddOptions: { exactO3rVersion: options.exactO3rVersion } }; return acc; }, (0, schematics_2.getPackageInstallConfig)(testPackageJsonPath, tree, options.projectName, true, !!options.exactO3rVersion)); Object.entries((0, schematics_2.getExternalDependenciesVersionRange)(devDependenciesToInstall, testPackageJsonPath, context.logger)) .forEach(([dep, range]) => { dependencies[dep] = { inManifest: [{ range, types: [dependencies_1.NodeDependencyType.Dev] }] }; }); let installJest; const testFramework = options.testingFramework || (0, schematics_2.getTestFramework)((0, schematics_2.getWorkspaceConfig)(tree), context); switch (testFramework) { case 'jest': { installJest = true; break; } case 'jasmine': { installJest = await (0, prompt_1.askConfirmation)(`You are currently using ${testFramework}. Do you want to setup Jest test framework? You will have to remove ${testFramework} yourself.`, true, false); break; } case undefined: { installJest = await (0, prompt_1.askConfirmation)('No test framework detected. Do you want to setup Jest test framework?', true, false); break; } default: { installJest = false; break; } } let installPlaywright = false; if (projectType === 'application') { installPlaywright = options.enablePlaywright === undefined ? await (0, prompt_1.askConfirmation)('Do you want to setup Playwright test framework for E2E?', true) : options.enablePlaywright; } const schematicsDefaultOptions = { useComponentFixtures: undefined }; const rules = [ (0, fixture_1.updateFixtureConfig)(options), (0, schematics_2.removePackages)(['@otter/testing']), (0, schematics_2.addVsCodeRecommendations)(['Orta.vscode-jest']), installPlaywright ? (0, playwright_1.updatePlaywright)(options, dependencies) : schematics_1.noop, (0, schematics_2.setupDependencies)({ projectName: options.projectName, dependencies, ngAddToRun: depsInfo.o3rPeerDeps }), (0, schematics_2.registerPackageCollectionSchematics)(packageJson), (0, schematics_2.setupSchematicsParamsForProject)({ '@o3r/core:component': schematicsDefaultOptions, '@o3r/core:component-container': schematicsDefaultOptions, '@o3r/core:component-presenter': schematicsDefaultOptions }, options.projectName), options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEditorConfig)() ]; if (installJest) { if (workingDirectory === undefined) { throw new schematics_2.O3rCliError(`Could not find working directory for project ${options.projectName || ''}`); } else { if (workspaceProject) { const packageJsonFile = tree.readJson(`${workingDirectory}/package.json`); packageJsonFile.scripts ||= {}; packageJsonFile.scripts.test = 'jest'; tree.overwrite(`${workingDirectory}/package.json`, JSON.stringify(packageJsonFile, null, 2)); const rootRelativePath = path.posix.relative(workingDirectory, tree.root.path.replace(/^\//, './')); const jestConfigFilesForProject = () => (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates/project'), [ (0, schematics_1.template)({ ...options, rootRelativePath, isAngularSetup: tree.exists('/angular.json') }), (0, schematics_1.move)(workingDirectory), (0, schematics_1.renameTemplateFiles)() ]), schematics_1.MergeStrategy.Overwrite); rules.push(jestConfigFilesForProject); } if (tree.exists('/jest.config.js')) { context.logger.info('Jest configuration files already exist at the root of the project.'); } else { const jestConfigFilesForWorkspace = () => (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./templates/workspace'), [ (0, schematics_1.template)({ ...options, tsconfigPath: `./${['tsconfig.base.json', 'tsconfig.json'].find((tsconfigBase) => tree.exists(`./${tsconfigBase}`))}` }), (0, schematics_1.move)(tree.root.path), (0, schematics_1.renameTemplateFiles)() ]), schematics_1.MergeStrategy.Default); rules.push(jestConfigFilesForWorkspace); } } } return () => (0, schematics_1.chain)(rules)(tree, context); } catch (e) { context.logger.error(`[ERROR]: Adding @o3r/testing has failed. If the error is related to missing @o3r dependencies you need to install '@o3r/core' or '@o3r/schematics' to be able to use the testing package. Please run 'ng add @o3r/core' or 'ng add @o3r/schematics'. Otherwise, use the error message as guidance.`); throw new schematics_2.O3rCliError(e); } }; } /** * Add Otter testing to an Angular Project * @param options */ exports.ngAdd = (0, schematics_2.createOtterSchematic)(ngAddFn); //# sourceMappingURL=index.js.map