@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
83 lines • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePlaywright = updatePlaywright;
const fs = require("node:fs");
const path = require("node:path");
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
/**
* Add Playwright to Otter application
* @param options @see RuleFactory.options
* @param dependencies
*/
function updatePlaywright(options, dependencies) {
const corePackageJsonPath = path.resolve(__dirname, '..', '..', '..', 'package.json');
const ownPackageJson = JSON.parse(fs.readFileSync(corePackageJsonPath, { encoding: 'utf8' }));
dependencies['@playwright/test'] = {
inManifest: [{
range: ownPackageJson.devDependencies['@playwright/test'],
types: [dependencies_1.NodeDependencyType.Dev]
}]
};
dependencies.rimraf = {
inManifest: [{
range: ownPackageJson.devDependencies.rimraf,
types: [dependencies_1.NodeDependencyType.Dev]
}]
};
return (tree, context) => {
const workingDirectory = (options?.projectName && (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName]?.root) || '.';
// update gitignore
const gitignorePath = '.gitignore';
if (tree.exists(gitignorePath)) {
let gitignore = tree.readText(gitignorePath);
if (!gitignore.includes('dist*') && !gitignore.includes('dist-e2e-playwright') && !gitignore.includes('playwright-reports')) {
gitignore
+= `
# Playwright
dist-e2e-playwright
playwright-reports
test-results
`;
tree.overwrite(gitignorePath, gitignore);
}
}
// register scripts
const packageJsonPath = path.posix.join(workingDirectory, 'package.json');
if (tree.exists(packageJsonPath)) {
const packageJson = tree.readJson(packageJsonPath);
packageJson.scripts ||= {};
packageJson.scripts['test:playwright'] ||= 'playwright test --config=e2e-playwright/playwright-config.ts';
packageJson.scripts['test:playwright:sanity'] ||= 'playwright test --config=e2e-playwright/playwright-config.sanity.ts';
tree.overwrite(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
// generate files
if (!tree.exists(path.posix.join(workingDirectory, 'e2e-playwright', 'playwright-config.ts'))) {
const name = 'my-scenario';
const scenarioName = core_1.strings.capitalize(core_1.strings.camelize(name));
const sanity = 'my-sanity';
const sanityName = core_1.strings.capitalize(core_1.strings.camelize(sanity));
const startCommand = `${(0, schematics_2.getPackageManager)()} run start`;
const project = options?.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
const serverPort = project?.architect?.serve?.options?.port || '4200';
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./playwright/templates'), [
(0, schematics_1.template)({
...core_1.strings,
name,
scenarioName,
sanity,
sanityName,
serverPort,
startCommand
}),
(0, schematics_1.renameTemplateFiles)(),
(0, schematics_1.move)(workingDirectory)
]);
return (0, schematics_1.mergeWith)(templateSource, schematics_1.MergeStrategy.Overwrite)(tree, context);
}
return tree;
};
}
//# sourceMappingURL=index.js.map