@nx/jest 0.0.0-pr-22179-271588f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +68 -0
  3. package/executors.json +10 -0
  4. package/generators.json +19 -0
  5. package/index.d.ts +8 -0
  6. package/index.js +19 -0
  7. package/migrations.json +105 -0
  8. package/package.json +57 -0
  9. package/plugin.d.ts +1 -0
  10. package/plugin.js +6 -0
  11. package/plugins/resolver.d.ts +1 -0
  12. package/plugins/resolver.js +58 -0
  13. package/preset/index.d.ts +2 -0
  14. package/preset/index.js +4 -0
  15. package/preset/jest-preset.d.ts +2 -0
  16. package/preset/jest-preset.js +33 -0
  17. package/src/executors/jest/jest.impl.d.ts +10 -0
  18. package/src/executors/jest/jest.impl.js +179 -0
  19. package/src/executors/jest/schema.d.ts +44 -0
  20. package/src/executors/jest/schema.json +226 -0
  21. package/src/executors/jest/summary.d.ts +7 -0
  22. package/src/executors/jest/summary.js +154 -0
  23. package/src/generators/configuration/configuration.d.ts +5 -0
  24. package/src/generators/configuration/configuration.js +90 -0
  25. package/src/generators/configuration/files/jest.config.ts__tmpl__ +16 -0
  26. package/src/generators/configuration/files/src/test-setup.ts__tmpl__ +0 -0
  27. package/src/generators/configuration/files/tsconfig.spec.json__tmpl__ +21 -0
  28. package/src/generators/configuration/files-angular/jest.config.ts__tmpl__ +27 -0
  29. package/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ +1 -0
  30. package/src/generators/configuration/files-angular/tsconfig.spec.json__tmpl__ +22 -0
  31. package/src/generators/configuration/lib/check-for-test-target.d.ts +3 -0
  32. package/src/generators/configuration/lib/check-for-test-target.js +11 -0
  33. package/src/generators/configuration/lib/create-files.d.ts +3 -0
  34. package/src/generators/configuration/lib/create-files.js +47 -0
  35. package/src/generators/configuration/lib/create-jest-config.d.ts +3 -0
  36. package/src/generators/configuration/lib/create-jest-config.js +102 -0
  37. package/src/generators/configuration/lib/ensure-dependencies.d.ts +3 -0
  38. package/src/generators/configuration/lib/ensure-dependencies.js +34 -0
  39. package/src/generators/configuration/lib/update-jestconfig.d.ts +3 -0
  40. package/src/generators/configuration/lib/update-jestconfig.js +26 -0
  41. package/src/generators/configuration/lib/update-tsconfig.d.ts +3 -0
  42. package/src/generators/configuration/lib/update-tsconfig.js +21 -0
  43. package/src/generators/configuration/lib/update-vscode-recommended-extensions.d.ts +2 -0
  44. package/src/generators/configuration/lib/update-vscode-recommended-extensions.js +18 -0
  45. package/src/generators/configuration/lib/update-workspace.d.ts +3 -0
  46. package/src/generators/configuration/lib/update-workspace.js +23 -0
  47. package/src/generators/configuration/schema.d.ts +31 -0
  48. package/src/generators/configuration/schema.json +80 -0
  49. package/src/generators/init/init.d.ts +5 -0
  50. package/src/generators/init/init.js +106 -0
  51. package/src/generators/init/schema.d.ts +8 -0
  52. package/src/generators/init/schema.json +35 -0
  53. package/src/migrations/update-15-0-0/add-jest-inputs.d.ts +2 -0
  54. package/src/migrations/update-15-0-0/add-jest-inputs.js +41 -0
  55. package/src/migrations/update-15-8-0/update-configs-jest-29.d.ts +3 -0
  56. package/src/migrations/update-15-8-0/update-configs-jest-29.js +146 -0
  57. package/src/migrations/update-15-8-0/update-tests-jest-29.d.ts +5 -0
  58. package/src/migrations/update-15-8-0/update-tests-jest-29.js +60 -0
  59. package/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.d.ts +2 -0
  60. package/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.js +9 -0
  61. package/src/migrations/update-16-5-0/add-test-setup-to-inputs-ignore.d.ts +3 -0
  62. package/src/migrations/update-16-5-0/add-test-setup-to-inputs-ignore.js +18 -0
  63. package/src/migrations/update-17-1-0/move-options-to-target-defaults.d.ts +2 -0
  64. package/src/migrations/update-17-1-0/move-options-to-target-defaults.js +117 -0
  65. package/src/plugins/plugin.d.ts +6 -0
  66. package/src/plugins/plugin.js +130 -0
  67. package/src/utils/ast-utils.d.ts +9 -0
  68. package/src/utils/ast-utils.js +32 -0
  69. package/src/utils/config/find-root-jest-files.d.ts +3 -0
  70. package/src/utils/config/find-root-jest-files.js +23 -0
  71. package/src/utils/config/functions.d.ts +17 -0
  72. package/src/utils/config/functions.js +211 -0
  73. package/src/utils/config/get-jest-projects.d.ts +34 -0
  74. package/src/utils/config/get-jest-projects.js +157 -0
  75. package/src/utils/config/is-preset-cjs.d.ts +2 -0
  76. package/src/utils/config/is-preset-cjs.js +15 -0
  77. package/src/utils/config/update-config.d.ts +20 -0
  78. package/src/utils/config/update-config.js +83 -0
  79. package/src/utils/versions.d.ts +9 -0
  80. package/src/utils/versions.js +12 -0
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jestConfigObject = exports.jestConfigObjectAst = exports.removeProperty = exports.addOrUpdateProperty = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const vm_1 = require("vm");
6
+ const path_1 = require("path");
7
+ const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript");
8
+ let tsModule;
9
+ function makeTextToInsert(value, precedingCommaNeeded) {
10
+ return `${precedingCommaNeeded ? ',' : ''}${value}`;
11
+ }
12
+ function findPropertyAssignment(object, propertyName) {
13
+ if (!tsModule) {
14
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
15
+ }
16
+ return object.properties.find((prop) => {
17
+ if (!tsModule.isPropertyAssignment(prop)) {
18
+ return false;
19
+ }
20
+ const propNameText = prop.name.getText();
21
+ if (propNameText.match(/^["'].+["']$/g)) {
22
+ return JSON.parse(propNameText.replace(/'/g, '"')) === propertyName;
23
+ }
24
+ return propNameText === propertyName;
25
+ });
26
+ }
27
+ function addOrUpdateProperty(tree, object, properties, value, path) {
28
+ if (!tsModule) {
29
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
30
+ }
31
+ const { SyntaxKind } = tsModule;
32
+ const propertyName = properties.shift();
33
+ const propertyAssignment = findPropertyAssignment(object, propertyName);
34
+ const originalContents = tree.read(path, 'utf-8');
35
+ if (propertyAssignment) {
36
+ if (propertyAssignment.initializer.kind === SyntaxKind.StringLiteral ||
37
+ propertyAssignment.initializer.kind === SyntaxKind.NumericLiteral ||
38
+ propertyAssignment.initializer.kind === SyntaxKind.FalseKeyword ||
39
+ propertyAssignment.initializer.kind === SyntaxKind.TrueKeyword) {
40
+ const updatedContents = (0, devkit_1.applyChangesToString)(originalContents, [
41
+ {
42
+ type: devkit_1.ChangeType.Delete,
43
+ start: propertyAssignment.initializer.pos,
44
+ length: propertyAssignment.initializer.getFullText().length,
45
+ },
46
+ {
47
+ type: devkit_1.ChangeType.Insert,
48
+ index: propertyAssignment.initializer.pos,
49
+ text: value,
50
+ },
51
+ ]);
52
+ tree.write(path, updatedContents);
53
+ return;
54
+ }
55
+ if (propertyAssignment.initializer.kind === SyntaxKind.ArrayLiteralExpression) {
56
+ const arrayLiteral = propertyAssignment.initializer;
57
+ if (arrayLiteral.elements.some((element) => {
58
+ return element.getText().replace(/'/g, '"') === value;
59
+ })) {
60
+ return [];
61
+ }
62
+ if (arrayLiteral.elements.length === 0) {
63
+ const updatedContents = (0, devkit_1.applyChangesToString)(originalContents, [
64
+ {
65
+ type: devkit_1.ChangeType.Insert,
66
+ index: arrayLiteral.elements.end,
67
+ text: value,
68
+ },
69
+ ]);
70
+ tree.write(path, updatedContents);
71
+ return;
72
+ }
73
+ else {
74
+ const text = makeTextToInsert(value, arrayLiteral.elements.length !== 0 &&
75
+ !arrayLiteral.elements.hasTrailingComma);
76
+ const updatedContents = (0, devkit_1.applyChangesToString)(originalContents, [
77
+ {
78
+ type: devkit_1.ChangeType.Insert,
79
+ index: arrayLiteral.elements.end,
80
+ text,
81
+ },
82
+ ]);
83
+ tree.write(path, updatedContents);
84
+ return;
85
+ }
86
+ }
87
+ else if (propertyAssignment.initializer.kind === SyntaxKind.ObjectLiteralExpression) {
88
+ return addOrUpdateProperty(tree, propertyAssignment.initializer, properties, value, path);
89
+ }
90
+ }
91
+ else {
92
+ if (propertyName === undefined) {
93
+ throw new Error(`Please use dot delimited paths to update an existing object. Eg. object.property `);
94
+ }
95
+ const text = makeTextToInsert(`${JSON.stringify(propertyName)}: ${value}`, object.properties.length !== 0 && !object.properties.hasTrailingComma);
96
+ const updatedContents = (0, devkit_1.applyChangesToString)(originalContents, [
97
+ {
98
+ type: devkit_1.ChangeType.Insert,
99
+ index: object.properties.end,
100
+ text,
101
+ },
102
+ ]);
103
+ tree.write(path, updatedContents);
104
+ return;
105
+ }
106
+ }
107
+ exports.addOrUpdateProperty = addOrUpdateProperty;
108
+ function removeProperty(object, properties) {
109
+ if (!tsModule) {
110
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
111
+ }
112
+ const propertyName = properties.shift();
113
+ const propertyAssignment = findPropertyAssignment(object, propertyName);
114
+ if (propertyAssignment) {
115
+ if (properties.length > 0 &&
116
+ propertyAssignment.initializer.kind ===
117
+ tsModule.SyntaxKind.ObjectLiteralExpression) {
118
+ return removeProperty(propertyAssignment.initializer, properties);
119
+ }
120
+ return propertyAssignment;
121
+ }
122
+ else {
123
+ return null;
124
+ }
125
+ }
126
+ exports.removeProperty = removeProperty;
127
+ function isModuleExport(node) {
128
+ if (!tsModule) {
129
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
130
+ }
131
+ return (tsModule.isExpressionStatement(node) &&
132
+ node.expression?.kind &&
133
+ tsModule.isBinaryExpression(node.expression) &&
134
+ node.expression.left.getText() === 'module.exports' &&
135
+ node.expression.operatorToken?.kind === tsModule.SyntaxKind.EqualsToken);
136
+ }
137
+ function isDefaultExport(node) {
138
+ if (!tsModule) {
139
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
140
+ }
141
+ return (tsModule.isExportAssignment(node) &&
142
+ node.expression?.kind &&
143
+ tsModule.isObjectLiteralExpression(node.expression) &&
144
+ node.getText().startsWith('export default'));
145
+ }
146
+ /**
147
+ * Should be used to get the jest config object as AST
148
+ */
149
+ function jestConfigObjectAst(fileContent) {
150
+ if (!tsModule) {
151
+ tsModule = (0, ensure_typescript_1.ensureTypescript)();
152
+ }
153
+ const sourceFile = tsModule.createSourceFile('jest.config.ts', fileContent, tsModule.ScriptTarget.Latest, true);
154
+ const exportStatement = sourceFile.statements.find((statement) => isModuleExport(statement) || isDefaultExport(statement));
155
+ let ast;
156
+ if (tsModule.isExpressionStatement(exportStatement)) {
157
+ const moduleExports = exportStatement.expression;
158
+ if (!moduleExports) {
159
+ throw new Error(`
160
+ The provided jest config file does not have the expected 'module.exports' expression.
161
+ See https://jestjs.io/docs/en/configuration for more details.`);
162
+ }
163
+ ast = moduleExports.right;
164
+ }
165
+ else if (tsModule.isExportAssignment(exportStatement)) {
166
+ const defaultExport = exportStatement.expression;
167
+ if (!defaultExport) {
168
+ throw new Error(`
169
+ The provided jest config file does not have the expected 'export default' expression.
170
+ See https://jestjs.io/docs/en/configuration for more details.`);
171
+ }
172
+ ast = defaultExport;
173
+ }
174
+ if (!ast) {
175
+ throw new Error(`
176
+ The provided jest config file does not have the expected 'module.exports' or 'export default' expression.
177
+ See https://jestjs.io/docs/en/configuration for more details.`);
178
+ }
179
+ if (!tsModule.isObjectLiteralExpression(ast)) {
180
+ throw new Error(`The 'export default' or 'module.exports' expression is not an object literal.`);
181
+ }
182
+ return ast;
183
+ }
184
+ exports.jestConfigObjectAst = jestConfigObjectAst;
185
+ /**
186
+ * Returns the jest config object
187
+ * @param host
188
+ * @param path
189
+ */
190
+ function jestConfigObject(host, path) {
191
+ const __filename = (0, path_1.join)(host.root, path);
192
+ const contents = host.read(path, 'utf-8');
193
+ let module = { exports: {} };
194
+ // transform the export default syntax to module.exports
195
+ // this will work for the default config, but will break if there are any other ts syntax
196
+ // TODO(caleb): use the AST to transform back to the module.exports syntax so this will keep working
197
+ // or deprecate and make a new method for getting the jest config object
198
+ const forcedModuleSyntax = contents.replace(/export\s+default/, 'module.exports =');
199
+ // Run the contents of the file with some stuff from this current context
200
+ // The module.exports will be mutated by the contents of the file...
201
+ (0, vm_1.runInContext)(forcedModuleSyntax, (0, vm_1.createContext)({
202
+ module,
203
+ require,
204
+ process,
205
+ __filename,
206
+ __dirname: (0, path_1.dirname)(__filename),
207
+ }));
208
+ // TODO: jest actually allows defining configs with async functions... we should be able to read that...
209
+ return module.exports;
210
+ }
211
+ exports.jestConfigObject = jestConfigObject;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Get a list of paths to all the jest config files
3
+ * using the Nx Jest executor.
4
+ *
5
+ * This is used to configure Jest multi-project support. To support projects
6
+ * using inferred targets @see getJestProjectsAsync
7
+ *
8
+ * To add a project not using the Nx Jest executor:
9
+ * export default {
10
+ * projects: [...getJestProjects(), '<rootDir>/path/to/jest.config.ts'];
11
+ * }
12
+ *
13
+ **/
14
+ export declare function getJestProjects(): string[];
15
+ /**
16
+ * a list of nested projects that have jest configured
17
+ * to be used in the testPathIgnorePatterns property of a given jest config
18
+ * https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring
19
+ * */
20
+ export declare function getNestedJestProjects(): string[];
21
+ /**
22
+ * Get a list of paths to all the jest config files
23
+ * using the Nx Jest executor and `@nx/run:commands`
24
+ * running `jest`.
25
+ *
26
+ * This is used to configure Jest multi-project support.
27
+ *
28
+ * To add a project not using the Nx Jest executor:
29
+ * export default async () => ({
30
+ * projects: [...(await getJestProjectsAsync()), '<rootDir>/path/to/jest.config.ts'];
31
+ * });
32
+ *
33
+ **/
34
+ export declare function getJestProjectsAsync(): Promise<string[]>;
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getJestProjectsAsync = exports.getNestedJestProjects = exports.getJestProjects = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const file_utils_1 = require("nx/src/project-graph/file-utils");
6
+ const path_1 = require("path");
7
+ const yargs = require("yargs-parser");
8
+ function getJestConfigProjectPath(projectJestConfigPath) {
9
+ return (0, path_1.join)('<rootDir>', projectJestConfigPath);
10
+ }
11
+ /**
12
+ * Get a list of paths to all the jest config files
13
+ * using the Nx Jest executor.
14
+ *
15
+ * This is used to configure Jest multi-project support. To support projects
16
+ * using inferred targets @see getJestProjectsAsync
17
+ *
18
+ * To add a project not using the Nx Jest executor:
19
+ * export default {
20
+ * projects: [...getJestProjects(), '<rootDir>/path/to/jest.config.ts'];
21
+ * }
22
+ *
23
+ **/
24
+ function getJestProjects() {
25
+ const ws = (0, file_utils_1.readWorkspaceConfig)({
26
+ format: 'nx',
27
+ });
28
+ const jestConfigurationSet = new Set();
29
+ for (const projectConfig of Object.values(ws.projects)) {
30
+ if (!projectConfig.targets) {
31
+ continue;
32
+ }
33
+ for (const targetConfiguration of Object.values(projectConfig.targets)) {
34
+ if (targetConfiguration.executor !== '@nx/jest:jest' &&
35
+ targetConfiguration.executor !== '@nrwl/jest:jest') {
36
+ continue;
37
+ }
38
+ if (targetConfiguration.options?.jestConfig) {
39
+ jestConfigurationSet.add(getJestConfigProjectPath(targetConfiguration.options.jestConfig));
40
+ }
41
+ if (targetConfiguration.configurations) {
42
+ for (const configurationObject of Object.values(targetConfiguration.configurations)) {
43
+ if (configurationObject.jestConfig) {
44
+ jestConfigurationSet.add(getJestConfigProjectPath(configurationObject.jestConfig));
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ return Array.from(jestConfigurationSet);
51
+ }
52
+ exports.getJestProjects = getJestProjects;
53
+ /**
54
+ * a list of nested projects that have jest configured
55
+ * to be used in the testPathIgnorePatterns property of a given jest config
56
+ * https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring
57
+ * */
58
+ function getNestedJestProjects() {
59
+ // TODO(caleb): get current project path and list of all projects and their rootDir
60
+ // return a list of all projects that are nested in the current projects path
61
+ // always include node_modules as that's the default
62
+ const allProjects = getJestProjects();
63
+ return ['/node_modules/'];
64
+ }
65
+ exports.getNestedJestProjects = getNestedJestProjects;
66
+ /**
67
+ * Get a list of paths to all the jest config files
68
+ * using the Nx Jest executor and `@nx/run:commands`
69
+ * running `jest`.
70
+ *
71
+ * This is used to configure Jest multi-project support.
72
+ *
73
+ * To add a project not using the Nx Jest executor:
74
+ * export default async () => ({
75
+ * projects: [...(await getJestProjectsAsync()), '<rootDir>/path/to/jest.config.ts'];
76
+ * });
77
+ *
78
+ **/
79
+ async function getJestProjectsAsync() {
80
+ const graph = await (0, devkit_1.createProjectGraphAsync)({
81
+ exitOnError: false,
82
+ resetDaemonClient: true,
83
+ });
84
+ const jestConfigurations = new Set();
85
+ for (const node of Object.values(graph.nodes)) {
86
+ const projectConfig = node.data;
87
+ if (!projectConfig.targets) {
88
+ continue;
89
+ }
90
+ for (const targetConfiguration of Object.values(projectConfig.targets)) {
91
+ if (targetConfiguration.executor === '@nx/jest:jest' ||
92
+ targetConfiguration.executor === '@nrwl/jest:jest') {
93
+ collectJestConfigFromJestExecutor(targetConfiguration, jestConfigurations);
94
+ }
95
+ else if (targetConfiguration.executor === 'nx:run-commands') {
96
+ collectJestConfigFromRunCommandsExecutor(targetConfiguration, projectConfig.root, jestConfigurations);
97
+ }
98
+ }
99
+ }
100
+ return Array.from(jestConfigurations);
101
+ }
102
+ exports.getJestProjectsAsync = getJestProjectsAsync;
103
+ function collectJestConfigFromJestExecutor(targetConfiguration, jestConfigurations) {
104
+ if (targetConfiguration.options?.jestConfig) {
105
+ jestConfigurations.add(getJestConfigProjectPath(targetConfiguration.options.jestConfig));
106
+ }
107
+ if (targetConfiguration.configurations) {
108
+ for (const configurationObject of Object.values(targetConfiguration.configurations)) {
109
+ if (configurationObject.jestConfig) {
110
+ jestConfigurations.add(getJestConfigProjectPath(configurationObject.jestConfig));
111
+ }
112
+ }
113
+ }
114
+ }
115
+ function collectJestConfigFromRunCommandsExecutor(targetConfiguration, projectRoot, jestConfigurations) {
116
+ if (targetConfiguration.options?.command) {
117
+ collectJestConfigFromCommand(targetConfiguration.options.command, targetConfiguration.options.cwd ?? projectRoot, jestConfigurations);
118
+ }
119
+ else if (targetConfiguration.options?.commands) {
120
+ for (const command of targetConfiguration.options.commands) {
121
+ const commandScript = typeof command === 'string' ? command : command.command;
122
+ collectJestConfigFromCommand(commandScript, targetConfiguration.options.cwd ?? projectRoot, jestConfigurations);
123
+ }
124
+ }
125
+ if (targetConfiguration.configurations) {
126
+ for (const configurationObject of Object.values(targetConfiguration.configurations)) {
127
+ if (configurationObject.command) {
128
+ collectJestConfigFromCommand(configurationObject.command, configurationObject.cwd ?? projectRoot, jestConfigurations);
129
+ }
130
+ else if (configurationObject.commands) {
131
+ for (const command of configurationObject.commands) {
132
+ const commandScript = typeof command === 'string' ? command : command.command;
133
+ collectJestConfigFromCommand(commandScript, configurationObject.cwd ?? projectRoot, jestConfigurations);
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+ function collectJestConfigFromCommand(command, cwd, jestConfigurations) {
140
+ const jestCommandRegex = /(?<=^|&)(?:[^&\r\n\s]* )*jest(?: [^&\r\n\s]*)*(?=$|&)/g;
141
+ const matches = command.match(jestCommandRegex);
142
+ if (!matches) {
143
+ return;
144
+ }
145
+ for (const match of matches) {
146
+ const parsed = yargs(match, {
147
+ configuration: { 'strip-dashed': true },
148
+ string: ['config'],
149
+ });
150
+ if (parsed.config) {
151
+ jestConfigurations.add(getJestConfigProjectPath((0, path_1.join)(cwd, parsed.config)));
152
+ }
153
+ else {
154
+ jestConfigurations.add(getJestConfigProjectPath(cwd));
155
+ }
156
+ }
157
+ }
@@ -0,0 +1,2 @@
1
+ import { type Tree } from '@nx/devkit';
2
+ export declare function isPresetCjs(tree: Tree): boolean;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPresetCjs = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ function isPresetCjs(tree) {
6
+ if (tree.exists('jest.preset.cjs')) {
7
+ return true;
8
+ }
9
+ const rootPkgJson = (0, devkit_1.readJson)(tree, 'package.json');
10
+ if (rootPkgJson.type && rootPkgJson.type === 'module') {
11
+ return true;
12
+ }
13
+ return false;
14
+ }
15
+ exports.isPresetCjs = isPresetCjs;
@@ -0,0 +1,20 @@
1
+ import { Tree } from '@nx/devkit';
2
+ /**
3
+ * Add a property to the jest config
4
+ * @param host
5
+ * @param path - path to the jest config file
6
+ * @param propertyName - Property to update. Can be dot delimited or an array to access deeply nested properties
7
+ * @param value
8
+ * @param options - set `valueAsString` option to true if the `value` being passed represents a string of the code that should be associated with the `propertyName`
9
+ */
10
+ export declare function addPropertyToJestConfig(host: Tree, path: string, propertyName: string | string[], value: unknown, options?: {
11
+ valueAsString: boolean;
12
+ }): void;
13
+ /**
14
+ * Remove a property value from the jest config
15
+ * @param host
16
+ * @param path
17
+ * @param propertyName - Property to remove. Can be dot delimited or an array to access deeply nested properties
18
+ */
19
+ export declare function removePropertyFromJestConfig(host: Tree, path: string, propertyName: string | string[]): void;
20
+ export declare function addImportStatementToJestConfig(host: Tree, path: string, importStatement: string): void;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addImportStatementToJestConfig = exports.removePropertyFromJestConfig = exports.addPropertyToJestConfig = void 0;
4
+ const devkit_1 = require("@nx/devkit");
5
+ const functions_1 = require("./functions");
6
+ /**
7
+ * Add a property to the jest config
8
+ * @param host
9
+ * @param path - path to the jest config file
10
+ * @param propertyName - Property to update. Can be dot delimited or an array to access deeply nested properties
11
+ * @param value
12
+ * @param options - set `valueAsString` option to true if the `value` being passed represents a string of the code that should be associated with the `propertyName`
13
+ */
14
+ function addPropertyToJestConfig(host, path, propertyName, value, options = { valueAsString: false }) {
15
+ if (!host.exists(path)) {
16
+ throw new Error(`Cannot find '${path}' in your workspace.`);
17
+ }
18
+ const { propertyString, properties } = parsePropertyName(propertyName);
19
+ try {
20
+ const configObject = (0, functions_1.jestConfigObjectAst)(host.read(path, 'utf-8'));
21
+ (0, functions_1.addOrUpdateProperty)(host, configObject, properties, options.valueAsString ? value : JSON.stringify(value), path);
22
+ }
23
+ catch (e) {
24
+ devkit_1.logger.info(`NX Please manually update ${path}`);
25
+ devkit_1.logger.warn(`Could not automatically add the following property to ${path}:`);
26
+ devkit_1.logger.warn(`${propertyString}: ${JSON.stringify(value)}`);
27
+ devkit_1.logger.warn(`Error: ${e.message}`);
28
+ }
29
+ }
30
+ exports.addPropertyToJestConfig = addPropertyToJestConfig;
31
+ /**
32
+ * Remove a property value from the jest config
33
+ * @param host
34
+ * @param path
35
+ * @param propertyName - Property to remove. Can be dot delimited or an array to access deeply nested properties
36
+ */
37
+ function removePropertyFromJestConfig(host, path, propertyName) {
38
+ if (!host.exists(path)) {
39
+ throw new Error(`Cannot find '${path}' in your workspace.`);
40
+ }
41
+ const { propertyString, properties } = parsePropertyName(propertyName);
42
+ try {
43
+ const configObject = (0, functions_1.jestConfigObjectAst)(host.read(path, 'utf-8'));
44
+ const propertyAssignment = (0, functions_1.removeProperty)(configObject, properties);
45
+ if (propertyAssignment) {
46
+ const file = host.read(path, 'utf-8');
47
+ const commaNeeded = file[propertyAssignment.end] === ',';
48
+ const updatedFile = (0, devkit_1.applyChangesToString)(file, [
49
+ {
50
+ type: devkit_1.ChangeType.Delete,
51
+ start: propertyAssignment.getStart(),
52
+ length: `${propertyAssignment.getText()}${commaNeeded ? ',' : ''}`
53
+ .length,
54
+ },
55
+ ]);
56
+ host.write(path, updatedFile);
57
+ return;
58
+ }
59
+ }
60
+ catch (e) {
61
+ devkit_1.logger.info(`NX Please manually update ${path}`);
62
+ devkit_1.logger.warn(`Could not automatically remove the '${propertyString}' property from ${path}:`);
63
+ }
64
+ }
65
+ exports.removePropertyFromJestConfig = removePropertyFromJestConfig;
66
+ function parsePropertyName(propertyName) {
67
+ return {
68
+ properties: Array.isArray(propertyName)
69
+ ? propertyName
70
+ : propertyName.split('.'),
71
+ propertyString: Array.isArray(propertyName)
72
+ ? propertyName.join('.')
73
+ : propertyName,
74
+ };
75
+ }
76
+ function addImportStatementToJestConfig(host, path, importStatement) {
77
+ const currentContents = host.read(path, 'utf-8');
78
+ const newContents = `${importStatement}
79
+
80
+ ${currentContents}`;
81
+ host.write(path, newContents);
82
+ }
83
+ exports.addImportStatementToJestConfig = addImportStatementToJestConfig;
@@ -0,0 +1,9 @@
1
+ export declare const nxVersion: any;
2
+ export declare const jestVersion = "^29.4.1";
3
+ export declare const babelJestVersion = "^29.4.1";
4
+ export declare const jestTypesVersion = "^29.4.0";
5
+ export declare const tsJestVersion = "^29.1.0";
6
+ export declare const tslibVersion = "^2.3.0";
7
+ export declare const swcJestVersion = "0.2.20";
8
+ export declare const typesNodeVersion = "18.16.9";
9
+ export declare const tsNodeVersion = "10.9.1";
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tsNodeVersion = exports.typesNodeVersion = exports.swcJestVersion = exports.tslibVersion = exports.tsJestVersion = exports.jestTypesVersion = exports.babelJestVersion = exports.jestVersion = exports.nxVersion = void 0;
4
+ exports.nxVersion = require('../../package.json').version;
5
+ exports.jestVersion = '^29.4.1';
6
+ exports.babelJestVersion = '^29.4.1';
7
+ exports.jestTypesVersion = '^29.4.0';
8
+ exports.tsJestVersion = '^29.1.0';
9
+ exports.tslibVersion = '^2.3.0';
10
+ exports.swcJestVersion = '0.2.20';
11
+ exports.typesNodeVersion = '18.16.9';
12
+ exports.tsNodeVersion = '10.9.1';