@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
106 lines • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.jestDependencies = void 0;
exports.setUpJestForAngularJson = setUpJestForAngularJson;
exports.setupJest = setupJest;
const path = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
/**
* List of dependencies needed to run jest
* @param eslintDependencies
*/
const jestDependencies = (eslintDependencies) => [
'@angular-builders/jest',
'@types/jest',
'jest',
'jest-environment-jsdom',
'jest-preset-angular',
'jest-util',
'ts-jest',
...eslintDependencies ? ['eslint-plugin-jest'] : []
];
exports.jestDependencies = jestDependencies;
/**
* Run jest on 'npm/yarn run test'
* @param workingDirectory
* @param isAngular
* @param projectName
*/
function setupJestScript(workingDirectory, isAngular, projectName) {
return (tree) => {
const packageJsonFile = tree.readJson(`${workingDirectory}/package.json`);
packageJsonFile.scripts ||= {};
packageJsonFile.scripts.test = isAngular ? `ng test ${projectName}` : 'jest';
tree.overwrite(`${workingDirectory}/package.json`, JSON.stringify(packageJsonFile, null, 2));
};
}
/**
* Set jest files and script in the generated library.
* @param projectName
*/
function setUpJestForAngularJson(projectName) {
return (tree) => {
const angularFile = tree.readJson('/angular.json');
const project = angularFile.projects[projectName];
if (project) {
project.architect ||= {};
project.architect.test = {
builder: '@angular-builders/jest:run',
options: {
tsConfig: `tsconfig.spec.json`,
config: `jest.config.js`,
setupFilesAfterEnv: './testing/setup-jest.ts'
}
};
tree.overwrite('/angular.json', JSON.stringify(angularFile, null, 2));
}
return tree;
};
}
/**
* Setup jest as recommended
* @param options
*/
function setupJest(options) {
return (tree, context) => {
const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
if (workingDirectory === undefined) {
throw new schematics_2.O3rCliError(`Could not find working directory for project ${options.projectName || ''}`);
}
const rootRelativePath = path.posix.relative(workingDirectory, tree.root.path.replace(/^\//, './'));
const isAngularSetup = tree.exists('/angular.json');
const setupJestInProject = (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./jest/templates/project'), [
(0, schematics_1.template)({
...options,
rootRelativePath,
isAngularSetup
}),
(0, schematics_1.move)(workingDirectory),
(0, schematics_1.renameTemplateFiles)()
]), schematics_1.MergeStrategy.Overwrite);
const rules = options.projectName
? [
setupJestScript(workingDirectory, isAngularSetup, options.projectName),
setupJestInProject,
setUpJestForAngularJson(options.projectName)
]
: [];
if (tree.exists('/jest.config.js')) {
context.logger.info('Jest configuration files already exist at the root of the project.');
}
else {
rules.push((0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./jest/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));
}
return (0, schematics_1.chain)(rules)(tree, context);
};
}
//# sourceMappingURL=index.js.map