@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
113 lines • 5.2 kB
JavaScript
;
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 fixture_1 = require("./fixture");
const jest_1 = require("./jest");
const playwright_1 = require("./playwright");
/**
* List of external dependencies to be added to the project as peer dependencies
*/
const dependenciesToInstall = [];
/**
* List of external dependencies to be added to the project as dev dependencies
*/
const devDependenciesToInstall = [
'pixelmatch',
'pngjs',
'uuid',
'@angular-devkit/build-angular',
'@angular/core',
'@angular/common'
];
/**
* 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 workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
const projectType = workspaceProject?.projectType || 'application';
let installJest;
let isEslintInstalled;
try {
require.resolve('@o3r/eslint-config');
isEslintInstalled = true;
}
catch {
isEslintInstalled = false;
}
const testFramework = options.testingFramework || (0, schematics_2.getTestFramework)((0, schematics_2.getWorkspaceConfig)(tree), context);
switch (testFramework) {
case 'jest': {
installJest = true;
break;
}
case 'jasmine':
case 'vitest': {
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)
]
: [],
...installJest ? [(0, jest_1.setupJest)(options)] : [],
(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),
(0, schematics_2.ngAddDependenciesRule)(options, testPackageJsonPath, {
dependenciesToInstall,
devDependenciesToInstall: devDependenciesToInstall.concat(installJest ? (0, jest_1.jestDependencies)(isEslintInstalled) : [], installPlaywright ? playwright_1.playwrightDependencies : [])
}),
options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEditorConfig)()
];
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