1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.checkDeps = exports.updateJestConfig = exports.updateConfigsJest28 = void 0;
|
4 | const devkit_1 = require("@nrwl/devkit");
|
5 | const executor_options_utils_1 = require("@nrwl/workspace/src/utilities/executor-options-utils");
|
6 | const tsquery_1 = require("@phenomnomnominal/tsquery");
|
7 | const typescript_1 = require("typescript");
|
8 | const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
|
9 | const jestVersion = '28.1.1';
|
10 | const JASMINE_TEST_RUNNER = /(testRunner:\s*['"`])(jest-jasmine2)(['"`])/g;
|
11 | const JSDOM_TEST_ENV = /(testEnvironment:\s*['"`])(jsdom)(['"`])/g;
|
12 | function updateConfigsJest28(tree) {
|
13 | let devDeps = checkDeps(tree);
|
14 | (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/jest:jest', (options) => {
|
15 | if (options.jestConfig && tree.exists(options.jestConfig)) {
|
16 | const updatedConfig = updateJestConfig(tree.read(options.jestConfig, 'utf-8'));
|
17 | tree.write(options.jestConfig, updatedConfig);
|
18 | const projectConfigCheck = testFileForDep(updatedConfig);
|
19 | devDeps = Object.assign(Object.assign({}, devDeps), projectConfigCheck);
|
20 | }
|
21 | });
|
22 | return (0, devkit_1.addDependenciesToPackageJson)(tree, {}, devDeps);
|
23 | }
|
24 | exports.updateConfigsJest28 = updateConfigsJest28;
|
25 | function updateJestConfig(config) {
|
26 | let content = tsquery_1.tsquery.replace(config, 'PropertyAssignment:has(Identifier[name="testURL"])', (node) => {
|
27 | var _a;
|
28 | const value = (_a = node === null || node === void 0 ? void 0 : node.initializer) === null || _a === void 0 ? void 0 : _a.getText();
|
29 | return `testEnvironmentOptions: {url: ${value}}`;
|
30 | });
|
31 | content = tsquery_1.tsquery.replace(content, 'PropertyAssignment > Identifier[name="extraGlobals"]', () => {
|
32 | return 'sandboxInjectedGlobals';
|
33 | });
|
34 | return tsquery_1.tsquery.replace(content, 'PropertyAssignment:has(Identifier[name="timers"])', (node) => {
|
35 |
|
36 | if ((node === null || node === void 0 ? void 0 : node.initializer) && (0, typescript_1.isStringLiteralLike)(node.initializer)) {
|
37 | const value = node === null || node === void 0 ? void 0 : node.initializer.getText().trim();
|
38 |
|
39 | if (value.includes('fake') || value.includes('modern')) {
|
40 | return `fakeTimers: { enableGlobally: true }`;
|
41 | }
|
42 | if (value.includes('real')) {
|
43 | return `fakeTimers: { enableGlobally: false }`;
|
44 | }
|
45 | if (value.includes('legacy')) {
|
46 | return `fakeTimers: { enableGlobally: true, legacyFakeTimers: true }`;
|
47 | }
|
48 | }
|
49 | });
|
50 | }
|
51 | exports.updateJestConfig = updateJestConfig;
|
52 | function checkDeps(tree) {
|
53 | const packageJson = (0, devkit_1.readJson)(tree, 'package.json');
|
54 | let devDeps = {};
|
55 | if (packageJson.devDependencies['jest-preset-angular']) {
|
56 | devDeps['jest-environment-jsdom'] = jestVersion;
|
57 | }
|
58 | const rootJestConfig = (0, find_root_jest_files_1.findRootJestConfig)(tree);
|
59 | if (rootJestConfig) {
|
60 | const rootConfigCheck = testFileForDep(tree.read(rootJestConfig, 'utf-8'));
|
61 | devDeps = Object.assign(Object.assign({}, devDeps), rootConfigCheck);
|
62 | }
|
63 | const rootJestPreset = (0, find_root_jest_files_1.findRootJestPreset)(tree);
|
64 | if (rootJestPreset) {
|
65 | const rootPresetCheck = testFileForDep(tree.read(rootJestPreset, 'utf-8'));
|
66 | devDeps = Object.assign(Object.assign({}, devDeps), rootPresetCheck);
|
67 | }
|
68 | return devDeps;
|
69 | }
|
70 | exports.checkDeps = checkDeps;
|
71 | function testFileForDep(config) {
|
72 | const deps = {};
|
73 | if (JASMINE_TEST_RUNNER.test(config)) {
|
74 | deps['jest-jasmine2'] = jestVersion;
|
75 | }
|
76 | if (JSDOM_TEST_ENV.test(config)) {
|
77 | deps['jest-environment-jsdom'] = jestVersion;
|
78 | }
|
79 | return deps;
|
80 | }
|
81 | exports.default = updateConfigsJest28;
|
82 |
|
\ | No newline at end of file |