1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.jestInitSchematic = exports.jestInitGenerator = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const devkit_1 = require("@nrwl/devkit");
|
6 | const js_1 = require("@nrwl/js");
|
7 | const run_tasks_in_serial_1 = require("@nrwl/workspace/src/utilities/run-tasks-in-serial");
|
8 | const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
|
9 | const versions_1 = require("../../utils/versions");
|
10 | const schemaDefaults = {
|
11 | compiler: 'tsc',
|
12 | js: false,
|
13 | rootProject: false,
|
14 | testEnvironment: 'jsdom',
|
15 | };
|
16 | function generateGlobalConfig(tree, isJS) {
|
17 | const contents = isJS
|
18 | ? (0, devkit_1.stripIndents) `
|
19 | const { getJestProjects } = require('@nrwl/jest');
|
20 |
|
21 | module.exports = {
|
22 | projects: getJestProjects()
|
23 | };`
|
24 | : (0, devkit_1.stripIndents) `
|
25 | import { getJestProjects } from '@nrwl/jest';
|
26 |
|
27 | export default {
|
28 | projects: getJestProjects()
|
29 | };`;
|
30 | tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents);
|
31 | }
|
32 | function createJestConfig(tree, options) {
|
33 | var _a;
|
34 | if (!tree.exists('jest.preset.js')) {
|
35 |
|
36 | tree.write(`jest.preset.js`, `
|
37 | const nxPreset = require('@nrwl/jest/preset').default;
|
38 |
|
39 | module.exports = { ...nxPreset }`);
|
40 | addTestInputs(tree);
|
41 | }
|
42 | if (options.rootProject) {
|
43 |
|
44 |
|
45 | return;
|
46 | }
|
47 | const rootJestPath = (0, find_root_jest_files_1.findRootJestConfig)(tree);
|
48 | if (!rootJestPath) {
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | generateGlobalConfig(tree, options.js);
|
54 | return;
|
55 | }
|
56 | if (tree.exists(rootJestPath)) {
|
57 |
|
58 | const projects = (0, devkit_1.getProjects)(tree);
|
59 | const projectNames = Array.from(projects.keys());
|
60 | const rootProject = projectNames.find((projectName) => { var _a; return ((_a = projects.get(projectName)) === null || _a === void 0 ? void 0 : _a.root) === '.'; });
|
61 |
|
62 |
|
63 | if (rootProject) {
|
64 | const rootProjectConfig = projects.get(rootProject);
|
65 | const jestTarget = Object.values(rootProjectConfig.targets || {}).find((t) => (t === null || t === void 0 ? void 0 : t.executor) === '@nrwl/jest:jest');
|
66 | const isProjectConfig = ((_a = jestTarget === null || jestTarget === void 0 ? void 0 : jestTarget.options) === null || _a === void 0 ? void 0 : _a.jestConfig) === rootJestPath;
|
67 |
|
68 | if (isProjectConfig) {
|
69 | const jestAppConfig = `jest.config.app.${options.js ? 'js' : 'ts'}`;
|
70 | tree.rename(rootJestPath, jestAppConfig);
|
71 | jestTarget.options.jestConfig = jestAppConfig;
|
72 | (0, devkit_1.updateProjectConfiguration)(tree, rootProject, rootProjectConfig);
|
73 | }
|
74 |
|
75 | generateGlobalConfig(tree, options.js);
|
76 | }
|
77 | }
|
78 | }
|
79 | function addTestInputs(tree) {
|
80 | var _a, _b, _c, _d;
|
81 | var _e, _f;
|
82 | const nxJson = (0, devkit_1.readNxJson)(tree);
|
83 | const productionFileSet = (_a = nxJson.namedInputs) === null || _a === void 0 ? void 0 : _a.production;
|
84 | if (productionFileSet) {
|
85 |
|
86 | productionFileSet.push(
|
87 |
|
88 | '!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
|
89 |
|
90 | '!{projectRoot}/tsconfig.spec.json',
|
91 |
|
92 | '!{projectRoot}/jest.config.[jt]s');
|
93 |
|
94 | nxJson.namedInputs.production = Array.from(new Set(productionFileSet));
|
95 | }
|
96 |
|
97 | (_b = nxJson.targetDefaults) !== null && _b !== void 0 ? _b : (nxJson.targetDefaults = {});
|
98 | (_c = (_e = nxJson.targetDefaults).test) !== null && _c !== void 0 ? _c : (_e.test = {});
|
99 | (_d = (_f = nxJson.targetDefaults.test).inputs) !== null && _d !== void 0 ? _d : (_f.inputs = [
|
100 | 'default',
|
101 | productionFileSet ? '^production' : '^default',
|
102 | ]);
|
103 | nxJson.targetDefaults.test.inputs.push('{workspaceRoot}/jest.preset.js');
|
104 | (0, devkit_1.updateNxJson)(tree, nxJson);
|
105 | }
|
106 | function updateDependencies(tree, options) {
|
107 | const dependencies = {
|
108 | tslib: versions_1.tslibVersion,
|
109 | };
|
110 | const devDeps = {
|
111 | '@nrwl/jest': versions_1.nxVersion,
|
112 | jest: versions_1.jestVersion,
|
113 |
|
114 |
|
115 |
|
116 | 'ts-jest': versions_1.tsJestVersion,
|
117 | };
|
118 | if (options.testEnvironment !== 'none') {
|
119 | devDeps[`jest-environment-${options.testEnvironment}`] = versions_1.jestVersion;
|
120 | }
|
121 | if (!options.js) {
|
122 | devDeps['ts-node'] = versions_1.tsNodeVersion;
|
123 | devDeps['@types/jest'] = versions_1.jestTypesVersion;
|
124 | devDeps['@types/node'] = versions_1.typesNodeVersion;
|
125 | }
|
126 | if (options.compiler === 'babel' || options.babelJest) {
|
127 | devDeps['babel-jest'] = versions_1.babelJestVersion;
|
128 |
|
129 | devDeps['@nrwl/js'] = versions_1.nxVersion;
|
130 | }
|
131 | else if (options.compiler === 'swc') {
|
132 | devDeps['@swc/jest'] = versions_1.swcJestVersion;
|
133 | }
|
134 | return (0, devkit_1.addDependenciesToPackageJson)(tree, dependencies, devDeps);
|
135 | }
|
136 | function updateExtensions(host) {
|
137 | if (!host.exists('.vscode/extensions.json')) {
|
138 | return;
|
139 | }
|
140 | (0, devkit_1.updateJson)(host, '.vscode/extensions.json', (json) => {
|
141 | json.recommendations = json.recommendations || [];
|
142 | const extension = 'firsttris.vscode-jest-runner';
|
143 | if (!json.recommendations.includes(extension)) {
|
144 | json.recommendations.push(extension);
|
145 | }
|
146 | return json;
|
147 | });
|
148 | }
|
149 | function jestInitGenerator(tree, schema) {
|
150 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
151 | const options = normalizeOptions(schema);
|
152 | const tasks = [];
|
153 | tasks.push(yield (0, js_1.initGenerator)(tree, Object.assign(Object.assign({}, schema), { skipFormat: true })));
|
154 | createJestConfig(tree, options);
|
155 | if (!options.skipPackageJson) {
|
156 | (0, devkit_1.removeDependenciesFromPackageJson)(tree, ['@nrwl/jest'], []);
|
157 | const installTask = updateDependencies(tree, options);
|
158 | tasks.push(installTask);
|
159 | }
|
160 | updateExtensions(tree);
|
161 | return (0, run_tasks_in_serial_1.runTasksInSerial)(...tasks);
|
162 | });
|
163 | }
|
164 | exports.jestInitGenerator = jestInitGenerator;
|
165 | function normalizeOptions(options) {
|
166 | return Object.assign(Object.assign({}, schemaDefaults), options);
|
167 | }
|
168 | exports.default = jestInitGenerator;
|
169 | exports.jestInitSchematic = (0, devkit_1.convertNxGenerator)(jestInitGenerator);
|
170 |
|
\ | No newline at end of file |