@nx/jest 20.4.0-beta.1 → 20.4.0-beta.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/jest",
3
- "version": "20.4.0-beta.1",
3
+ "version": "20.4.0-beta.2",
4
4
  "private": false,
5
5
  "description": "The Nx Plugin for Jest contains executors and generators allowing your workspace to use the powerful Jest testing capabilities.",
6
6
  "repository": {
@@ -37,8 +37,8 @@
37
37
  "dependencies": {
38
38
  "@jest/reporters": "^29.4.1",
39
39
  "@jest/test-result": "^29.4.1",
40
- "@nx/devkit": "20.4.0-beta.1",
41
- "@nx/js": "20.4.0-beta.1",
40
+ "@nx/devkit": "20.4.0-beta.2",
41
+ "@nx/js": "20.4.0-beta.2",
42
42
  "@phenomnomnominal/tsquery": "~5.0.1",
43
43
  "identity-obj-proxy": "3.0.0",
44
44
  "jest-config": "^29.4.1",
@@ -85,6 +85,15 @@ async function configurationGeneratorInternal(tree, schema) {
85
85
  }
86
86
  if (options.isTsSolutionSetup) {
87
87
  ignoreTestOutput(tree);
88
+ // in the TS solution setup, the test target depends on the build outputs
89
+ // so we need to setup the task pipeline accordingly
90
+ const nxJson = (0, devkit_1.readNxJson)(tree);
91
+ nxJson.targetDefaults ??= {};
92
+ nxJson.targetDefaults[options.targetName] ??= {};
93
+ nxJson.targetDefaults[options.targetName].dependsOn ??= [];
94
+ nxJson.targetDefaults[options.targetName].dependsOn.push('^build');
95
+ nxJson.targetDefaults[options.targetName].dependsOn = Array.from(new Set(nxJson.targetDefaults[options.targetName].dependsOn));
96
+ (0, devkit_1.updateNxJson)(tree, nxJson);
88
97
  }
89
98
  if (!schema.skipFormat) {
90
99
  await (0, devkit_1.formatFiles)(tree);
@@ -0,0 +1,28 @@
1
+ <%_ if (transformer === '@swc/jest') { _%>
2
+ /* eslint-disable */
3
+ <% if(js) {%>const { readFileSync } = require('fs')<% } else { %>import { readFileSync } from 'fs';<% } %>
4
+
5
+ // Reading the SWC compilation config for the spec files
6
+ const swcJestConfig = JSON.parse(
7
+ readFileSync(`${__dirname}/.spec.swcrc`, 'utf-8')
8
+ );
9
+
10
+ // Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves
11
+ swcJestConfig.swcrc = false;
12
+
13
+ <%_ } _%>
14
+ <% if(js){ %>module.exports =<% } else{ %>export default<% } %> {
15
+ displayName: '<%= project %>',
16
+ preset: '<%= offsetFromRoot %>jest.preset.<%= presetExt %>',<% if(setupFile !== 'none') { %>
17
+ setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if(testEnvironment) { %>
18
+ testEnvironment: '<%= testEnvironment %>',<% } %><% if(skipSerializers){ %>
19
+ transform: {
20
+ <% if (supportTsx){ %>'^.+\\.[tj]sx?$'<% } else { %>'^.+\\.[tj]s$'<% } %>: <% if (transformerOptions) { %>['<%= transformer %>', <%- transformerOptions %>]<% } else { %>'<%= transformer %>'<% } %>
21
+ },
22
+ <% if (supportTsx) { %>moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],<% } else { %>moduleFileExtensions: ['ts', 'js', 'html'],<% } %><% } %>
23
+ coverageDirectory: '<%= coverageDirectory %>'<% if(rootProject){ %>,
24
+ testMatch: [
25
+ '<rootDir>/src/**/__tests__/**/*.[jt]s?(x)',
26
+ '<rootDir>/src/**/*(*.)@(spec|test).[jt]s?(x)',
27
+ ],<% } %>
28
+ };
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createFiles = createFiles;
4
4
  const devkit_1 = require("@nx/devkit");
5
+ const add_swc_config_1 = require("@nx/js/src/utils/swc/add-swc-config");
5
6
  const path_1 = require("path");
6
- const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
7
7
  function createFiles(tree, options, presetExt) {
8
8
  const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, options.project);
9
- const filesFolder = options.setupFile === 'angular' ? '../files-angular' : '../files';
9
+ const commonFilesFolder = options.setupFile === 'angular' ? '../files-angular' : '../files/common';
10
10
  let transformer;
11
11
  let transformerOptions = null;
12
12
  if (options.compiler === 'babel' || options.babelJest) {
@@ -14,7 +14,10 @@ function createFiles(tree, options, presetExt) {
14
14
  }
15
15
  else if (options.compiler === 'swc') {
16
16
  transformer = '@swc/jest';
17
- if (options.supportTsx) {
17
+ if (options.isTsSolutionSetup) {
18
+ transformerOptions = 'swcJestConfig';
19
+ }
20
+ else if (options.supportTsx) {
18
21
  transformerOptions =
19
22
  "{ jsc: { parser: { syntax: 'typescript', tsx: true }, transform: { react: { runtime: 'automatic' } } } }";
20
23
  }
@@ -23,18 +26,24 @@ function createFiles(tree, options, presetExt) {
23
26
  transformer = 'ts-jest';
24
27
  transformerOptions = "{ tsconfig: '<rootDir>/tsconfig.spec.json' }";
25
28
  }
26
- const isTsSolutionSetup = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree);
29
+ if (options.compiler === 'swc' && options.isTsSolutionSetup) {
30
+ (0, add_swc_config_1.addSwcTestConfig)(tree, projectConfig.root, 'es6', options.supportTsx);
31
+ }
27
32
  const projectRoot = options.rootProject
28
33
  ? options.project
29
34
  : projectConfig.root;
30
35
  const rootOffset = (0, devkit_1.offsetFromRoot)(projectConfig.root);
31
- (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, filesFolder), projectConfig.root, {
36
+ // jsdom is the default in the nx preset
37
+ const testEnvironment = options.testEnvironment === 'none' || options.testEnvironment === 'jsdom'
38
+ ? ''
39
+ : options.testEnvironment;
40
+ const coverageDirectory = options.isTsSolutionSetup
41
+ ? `test-output/jest/coverage`
42
+ : `${rootOffset}coverage/${projectRoot}`;
43
+ (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, commonFilesFolder), projectConfig.root, {
32
44
  tmpl: '',
33
45
  ...options,
34
- // jsdom is the default
35
- testEnvironment: options.testEnvironment === 'none' || options.testEnvironment === 'jsdom'
36
- ? ''
37
- : options.testEnvironment,
46
+ testEnvironment,
38
47
  transformer,
39
48
  transformerOptions,
40
49
  js: !!options.js,
@@ -42,15 +51,33 @@ function createFiles(tree, options, presetExt) {
42
51
  projectRoot,
43
52
  offsetFromRoot: rootOffset,
44
53
  presetExt,
45
- coverageDirectory: isTsSolutionSetup
46
- ? `test-output/jest/coverage`
47
- : `${rootOffset}coverage/${projectRoot}`,
48
- extendedConfig: isTsSolutionSetup
54
+ coverageDirectory,
55
+ extendedConfig: options.isTsSolutionSetup
49
56
  ? `${rootOffset}tsconfig.base.json`
50
57
  : './tsconfig.json',
51
- outDir: isTsSolutionSetup ? `./out-tsc/jest` : `${rootOffset}dist/out-tsc`,
52
- module: !isTsSolutionSetup || transformer === 'ts-jest' ? 'commonjs' : undefined,
58
+ outDir: options.isTsSolutionSetup
59
+ ? `./out-tsc/jest`
60
+ : `${rootOffset}dist/out-tsc`,
61
+ module: !options.isTsSolutionSetup || transformer === 'ts-jest'
62
+ ? 'commonjs'
63
+ : undefined,
53
64
  });
65
+ if (options.setupFile !== 'angular') {
66
+ (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, options.isTsSolutionSetup
67
+ ? '../files/jest-config-ts-solution'
68
+ : '../files/jest-config-non-ts-solution'), projectConfig.root, {
69
+ tmpl: '',
70
+ ...options,
71
+ testEnvironment,
72
+ transformer,
73
+ transformerOptions,
74
+ js: !!options.js,
75
+ rootProject: options.rootProject,
76
+ offsetFromRoot: rootOffset,
77
+ presetExt,
78
+ coverageDirectory,
79
+ });
80
+ }
54
81
  if (options.setupFile === 'none') {
55
82
  tree.delete((0, path_1.join)(projectConfig.root, './src/test-setup.ts'));
56
83
  }
@@ -4,6 +4,7 @@ exports.createJestConfig = createJestConfig;
4
4
  const devkit_1 = require("@nx/devkit");
5
5
  const project_configuration_utils_1 = require("nx/src/project-graph/utils/project-configuration-utils");
6
6
  const config_file_1 = require("../../../utils/config/config-file");
7
+ const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
7
8
  async function createJestConfig(tree, options, presetExt) {
8
9
  if (!tree.exists(`jest.preset.${presetExt}`)) {
9
10
  if (presetExt === 'mjs') {
@@ -57,7 +58,9 @@ module.exports = { ...nxPreset };`);
57
58
  // Jest target has already been updated
58
59
  return;
59
60
  }
60
- const jestProjectConfig = `jest.config.${rootProjectConfig.projectType === 'application' ? 'app' : 'lib'}.${options.js ? 'js' : 'ts'}`;
61
+ const jestProjectConfig = `jest.config.${(0, ts_solution_setup_1.getProjectType)(tree, rootProjectConfig.root, rootProjectConfig.projectType) === 'application'
62
+ ? 'app'
63
+ : 'lib'}.${options.js ? 'js' : 'ts'}`;
61
64
  tree.rename(rootJestPath, jestProjectConfig);
62
65
  const nxJson = (0, devkit_1.readNxJson)(tree);
63
66
  const targetDefaults = (0, project_configuration_utils_1.readTargetDefaultsForTarget)(jestTargetName, nxJson.targetDefaults, jestTargetConfigInGraph.executor);
@@ -2,8 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.updateTsConfig = updateTsConfig;
4
4
  const devkit_1 = require("@nx/devkit");
5
+ const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
5
6
  function updateTsConfig(host, options) {
6
- const { root, projectType } = (0, devkit_1.readProjectConfiguration)(host, options.project);
7
+ const { root, projectType: _projectType } = (0, devkit_1.readProjectConfiguration)(host, options.project);
7
8
  if (!host.exists((0, devkit_1.joinPathFragments)(root, 'tsconfig.json'))) {
8
9
  throw new Error(`Expected ${(0, devkit_1.joinPathFragments)(root, 'tsconfig.json')} to exist. Please create one.`);
9
10
  }
@@ -16,6 +17,7 @@ function updateTsConfig(host, options) {
16
17
  }
17
18
  return json;
18
19
  });
20
+ const projectType = (0, ts_solution_setup_1.getProjectType)(host, root, _projectType);
19
21
  // fall-back runtime tsconfig file path in case the user didn't provide one
20
22
  let runtimeTsconfigPath = (0, devkit_1.joinPathFragments)(root, projectType === 'application' ? 'tsconfig.app.json' : 'tsconfig.lib.json');
21
23
  if (options.runtimeTsconfigFileName) {