UNPKG

7.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.jestInitSchematic = exports.jestInitGenerator = void 0;
4const tslib_1 = require("tslib");
5const devkit_1 = require("@nrwl/devkit");
6const js_1 = require("@nrwl/js");
7const run_tasks_in_serial_1 = require("@nrwl/workspace/src/utilities/run-tasks-in-serial");
8const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
9const versions_1 = require("../../utils/versions");
10const schemaDefaults = {
11 compiler: 'tsc',
12 js: false,
13 rootProject: false,
14 testEnvironment: 'jsdom',
15};
16function 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}
32function createJestConfig(tree, options) {
33 var _a;
34 if (!tree.exists('jest.preset.js')) {
35 // preset is always js file.
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 // we don't want any config to be made because the `jestProjectGenerator`
44 // will copy the template config file
45 return;
46 }
47 const rootJestPath = (0, find_root_jest_files_1.findRootJestConfig)(tree);
48 if (!rootJestPath) {
49 // if there's not root jest config, we will create one and return
50 // this can happen when:
51 // - root jest config was renamed => in which case there is migration needed
52 // - root project didn't have jest setup => again, no migration is needed
53 generateGlobalConfig(tree, options.js);
54 return;
55 }
56 if (tree.exists(rootJestPath)) {
57 // moving from root project config to monorepo-style config
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 // root project might have been removed,
62 // if it's missing there's nothing to migrate
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 // if root project doesn't have jest target, there's nothing to migrate
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 // generate new global config as it was move to project config or is missing
75 generateGlobalConfig(tree, options.js);
76 }
77 }
78}
79function 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 // This is one of the patterns in the default jest patterns
86 productionFileSet.push(
87 // Remove spec, test, and snapshots from the production fileset
88 '!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
89 // Remove tsconfig.spec.json
90 '!{projectRoot}/tsconfig.spec.json',
91 // Remove jest.config.js/ts
92 '!{projectRoot}/jest.config.[jt]s');
93 // Dedupe and set
94 nxJson.namedInputs.production = Array.from(new Set(productionFileSet));
95 }
96 // Test targets depend on all their project's sources + production sources of dependencies
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}
106function 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 // because the default jest-preset uses ts-jest,
114 // jest will throw an error if it's not installed
115 // even if not using it in overriding transformers
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 // in some cases @nrwl/js will not already be present i.e. node only projects
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}
136function 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}
149function 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}
164exports.jestInitGenerator = jestInitGenerator;
165function normalizeOptions(options) {
166 return Object.assign(Object.assign({}, schemaDefaults), options);
167}
168exports.default = jestInitGenerator;
169exports.jestInitSchematic = (0, devkit_1.convertNxGenerator)(jestInitGenerator);
170//# sourceMappingURL=init.js.map
\No newline at end of file