Skip to content

Commit 2c40e1b

Browse files
committed
chore(misc): configure unit tests correctly for workspace
chore(core): update test setup for devkit chore(js): fix unit test setup chore(bundling): update unit test set up chore(misc): fix export conditions and unit test setups chore(misc): update unit test setup for testing tools chore(misc): setup unit test config chore(misc): update unit test setup chore(misc): update test config for node chore(misc): update react adjacent packages unit test setup chore(misc): update angular to use new unit test setup chore(misc): update unit test setup for nx chore(misc): cleanup
1 parent d187e5c commit 2c40e1b

File tree

314 files changed

+3742
-3224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

314 files changed

+3742
-3224
lines changed

docs/local-dist-migration-plan.md

Lines changed: 0 additions & 961 deletions
This file was deleted.

jest-environment.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const NodeEnvironment = require('jest-environment-node').default;
2+
3+
/**
4+
* Custom Jest environment that patches Object.defineProperty to allow
5+
* reconfiguration of spied properties.
6+
*
7+
* This is necessary because TypeScript project references change how modules
8+
* are resolved, causing Jest spies to fail with "Cannot redefine property" errors.
9+
*/
10+
class CustomEnvironment extends NodeEnvironment {
11+
constructor(config, context) {
12+
super(config, context);
13+
}
14+
15+
async setup() {
16+
await super.setup();
17+
18+
// Patch Object.defineProperty to allow reconfiguration of spied properties
19+
const originalDefineProperty = Object.defineProperty;
20+
this.global.Object.defineProperty = function (obj, prop, descriptor) {
21+
if (descriptor && typeof descriptor.configurable === 'undefined') {
22+
descriptor.configurable = true;
23+
}
24+
try {
25+
return originalDefineProperty(obj, prop, descriptor);
26+
} catch (e) {
27+
// If we can't redefine, try to delete and define
28+
if (e.message && e.message.includes('Cannot redefine property')) {
29+
try {
30+
delete obj[prop];
31+
return originalDefineProperty(obj, prop, descriptor);
32+
} catch {
33+
// If that also fails, just return the object
34+
return obj;
35+
}
36+
}
37+
throw e;
38+
}
39+
};
40+
}
41+
}
42+
43+
module.exports = CustomEnvironment;

jest.preset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ module.exports = {
1212
moduleFileExtensions: ['ts', 'js', 'html'],
1313
coverageReporters: ['html'],
1414
maxWorkers: 1,
15-
testEnvironment: 'node',
15+
testEnvironment: require.resolve('./jest-environment.js'),
1616
setupFiles: ['../../scripts/unit-test-setup.js'],
1717
};

packages/angular-rspack-compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"default": "./dist/index.js"
3737
},
3838
"./src/models/style-preprocessor-options": {
39-
"import": "./dist/models/style-preprocessor-options.js",
4039
"types": "./dist/models/style-preprocessor-options.d.ts",
41-
"@nx/nx-source": "./src/models/style-preprocessor-options.ts"
40+
"@nx/nx-source": "./src/models/style-preprocessor-options.ts",
41+
"import": "./dist/models/style-preprocessor-options.js"
4242
}
4343
},
4444
"dependencies": {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Mock for @rspack/core to avoid loading native bindings during tests
2+
module.exports = {
3+
NormalModuleReplacementPlugin: jest.fn(),
4+
rspack: jest.fn(),
5+
Configuration: jest.fn(),
6+
Compiler: jest.fn(),
7+
Stats: jest.fn(),
8+
};
File renamed without changes.

packages/angular/migrations.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import path = require('path');
2-
import json = require('./migrations.json');
1+
import path from 'path';
2+
import json from './migrations.json';
33

44
import { assertValidMigrationPaths } from '@nx/devkit/internal-testing-utils';
55

packages/angular/package.json

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,65 +22,76 @@
2222
"./generators.json": "./generators.json",
2323
"./executors.json": "./executors.json",
2424
"./generators": {
25-
"import": "./dist/generators.js",
2625
"types": "./generators.d.ts",
27-
"@nx/nx-source": "./generators.ts"
26+
"@nx/nx-source": "./generators.ts",
27+
"import": "./dist/generators.js",
28+
"default": "./dist/generators.js"
2829
},
2930
"./executors": {
30-
"import": "./dist/executors.js",
3131
"types": "./executors.d.ts",
32-
"@nx/nx-source": "./executors.ts"
32+
"@nx/nx-source": "./executors.ts",
33+
"import": "./dist/executors.js",
34+
"default": "./dist/executors.js"
3335
},
3436
"./plugin": {
35-
"import": "./dist/plugin.js",
3637
"types": "./plugin.d.ts",
37-
"@nx/nx-source": "./plugin.ts"
38+
"@nx/nx-source": "./plugin.ts",
39+
"import": "./dist/plugin.js",
40+
"default": "./dist/plugin.js"
3841
},
3942
"./tailwind": {
40-
"import": "./dist/tailwind.js",
4143
"types": "./tailwind.d.ts",
42-
"@nx/nx-source": "./tailwind.ts"
44+
"@nx/nx-source": "./tailwind.ts",
45+
"import": "./dist/tailwind.js",
46+
"default": "./dist/tailwind.js"
4347
},
4448
"./module-federation": {
45-
"import": "./dist/module-federation/index.js",
4649
"types": "./module-federation/index.d.ts",
47-
"@nx/nx-source": "./module-federation/index.ts"
50+
"@nx/nx-source": "./module-federation/index.ts",
51+
"import": "./dist/module-federation/index.js",
52+
"default": "./dist/module-federation/index.js"
4853
},
4954
"./src/utils": {
50-
"import": "./dist/src/utils/index.js",
5155
"types": "./src/utils/index.d.ts",
52-
"@nx/nx-source": "./src/utils/index.ts"
56+
"@nx/nx-source": "./src/utils/index.ts",
57+
"import": "./dist/src/utils/index.js",
58+
"default": "./dist/src/utils/index.js"
5359
},
5460
"./plugins/component-testing": {
55-
"import": "./dist/plugins/component-testing.js",
5661
"types": "./plugins/component-testing.d.ts",
57-
"@nx/nx-source": "./plugins/component-testing.ts"
62+
"@nx/nx-source": "./plugins/component-testing.ts",
63+
"import": "./dist/plugins/component-testing.js",
64+
"default": "./dist/plugins/component-testing.js"
5865
},
5966
"./src/generators/utils": {
60-
"import": "./dist/src/generators/utils/index.js",
6167
"types": "./src/generators/utils/index.d.ts",
62-
"@nx/nx-source": "./src/generators/utils/index.ts"
68+
"@nx/nx-source": "./src/generators/utils/index.ts",
69+
"import": "./dist/src/generators/utils/index.js",
70+
"default": "./dist/src/generators/utils/index.js"
6371
},
6472
"./src/generators/move/move-impl": {
65-
"import": "./dist/src/generators/move/move-impl.js",
6673
"types": "./src/generators/move/move-impl.d.ts",
67-
"@nx/nx-source": "./src/generators/move/move-impl.ts"
74+
"@nx/nx-source": "./src/generators/move/move-impl.ts",
75+
"import": "./dist/src/generators/move/move-impl.js",
76+
"default": "./dist/src/generators/move/move-impl.js"
6877
},
6978
"./src/builders/*/schema.json": "./dist/src/builders/*/schema.json",
7079
"./src/builders/*.impl": {
71-
"import": "./dist/src/builders/*.impl.js",
7280
"types": "./src/builders/*.impl.d.ts",
73-
"@nx/nx-source": "./src/builders/*.impl.ts"
81+
"@nx/nx-source": "./src/builders/*.impl.ts",
82+
"import": "./dist/src/builders/*.impl.js",
83+
"default": "./dist/src/builders/*.impl.js"
7484
},
7585
"./src/builders/*/schema": {
7686
"types": "./src/builders/*/schema.d.ts",
7787
"@nx/nx-source": "./src/builders/*/schema.ts"
7888
},
7989
"./src/executors/*/schema.json": "./dist/src/executors/*/schema.json",
8090
"./src/executors/*.impl": {
81-
"import": "./dist/src/executors/*.impl.js",
8291
"types": "./src/executors/*.impl.d.ts",
83-
"@nx/nx-source": "./src/executors/*.impl.ts"
92+
"@nx/nx-source": "./src/executors/*.impl.ts",
93+
"import": "./dist/src/executors/*.impl.js",
94+
"default": "./dist/src/executors/*.impl.js"
8495
},
8596
"./src/executors/*/schema": {
8697
"types": "./src/executors/*/schema.d.ts",

packages/angular/src/generators/application/application.spec.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1+
// need to mock cypress otherwise it'll use installed version in this repo's package.json
2+
jest.mock('@nx/cypress/src/utils/versions', () => ({
3+
...jest.requireActual('@nx/cypress/src/utils/versions'),
4+
getInstalledCypressMajorVersion: jest.fn(),
5+
}));
6+
jest.mock('enquirer');
7+
let mockFormatFiles = jest
8+
.fn()
9+
.mockImplementation(jest.requireActual('@nx/devkit').formatFiles);
10+
jest.mock('@nx/devkit', () => {
11+
const original = jest.requireActual('@nx/devkit');
12+
return {
13+
...original,
14+
ensurePackage: (pkg: string) => jest.requireActual(pkg),
15+
createProjectGraphAsync: jest.fn().mockResolvedValue({
16+
nodes: {},
17+
dependencies: {},
18+
}),
19+
formatFiles: mockFormatFiles,
20+
};
21+
});
22+
123
import { getInstalledCypressMajorVersion } from '@nx/cypress/src/utils/versions';
2-
import * as devkit from '@nx/devkit';
324
import {
25+
getProjects,
426
NxJsonConfiguration,
527
parseJson,
628
readJson,
@@ -24,24 +46,6 @@ import {
2446
import { generateTestApplication } from '../utils/testing';
2547
import type { Schema } from './schema';
2648

27-
// need to mock cypress otherwise it'll use installed version in this repo's package.json
28-
jest.mock('@nx/cypress/src/utils/versions', () => ({
29-
...jest.requireActual('@nx/cypress/src/utils/versions'),
30-
getInstalledCypressMajorVersion: jest.fn(),
31-
}));
32-
jest.mock('enquirer');
33-
jest.mock('@nx/devkit', () => {
34-
const original = jest.requireActual('@nx/devkit');
35-
return {
36-
...original,
37-
ensurePackage: (pkg: string) => jest.requireActual(pkg),
38-
createProjectGraphAsync: jest.fn().mockResolvedValue({
39-
nodes: {},
40-
dependencies: {},
41-
}),
42-
};
43-
});
44-
4549
describe('app', () => {
4650
let appTree: Tree;
4751
let mockedInstalledCypressVersion: jest.Mock<
@@ -170,7 +174,7 @@ describe('app', () => {
170174
await generateApp(appTree, 'my-app', { tags: 'one,two,my-app' });
171175

172176
// ASSERT
173-
const projects = devkit.getProjects(appTree);
177+
const projects = getProjects(appTree);
174178
expect(projects).toEqual(
175179
new Map(
176180
Object.entries({
@@ -287,7 +291,7 @@ describe('app', () => {
287291
// ARRANGE
288292
const nxJson = readNxJson(appTree);
289293
nxJson.defaultProject = 'some-awesome-project';
290-
devkit.updateNxJson(appTree, nxJson);
294+
updateNxJson(appTree, nxJson);
291295

292296
// ACT
293297
await generateApp(appTree);
@@ -309,7 +313,7 @@ describe('app', () => {
309313
await generateApp(appTree, 'my-dir/my-app', {
310314
tags: 'one,two,my-app',
311315
});
312-
const projects = devkit.getProjects(appTree);
316+
const projects = getProjects(appTree);
313317
expect(projects).toEqual(
314318
new Map(
315319
Object.entries({
@@ -584,11 +588,9 @@ describe('app', () => {
584588

585589
describe('format files', () => {
586590
it('should format files', async () => {
587-
const formatFilesSpy = jest.spyOn(devkit, 'formatFiles');
588-
589591
await generateApp(appTree, 'my-app', { skipFormat: false });
590592

591-
expect(formatFilesSpy).toHaveBeenCalled();
593+
expect(mockFormatFiles).toHaveBeenCalled();
592594
expect(
593595
appTree.read('my-app/src/app/app-module.ts', 'utf-8')
594596
).toMatchSnapshot();

packages/angular/src/generators/library-secondary-entry-point/library-secondary-entry-point.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import 'nx/src/internal-testing-utils/mock-project-graph';
1+
let mockFormatFiles = jest
2+
.fn()
3+
.mockImplementation(jest.requireActual('@nx/devkit').formatFiles);
4+
jest.mock('@nx/devkit', () => ({
5+
...jest.requireActual('@nx/devkit'),
6+
formatFiles: mockFormatFiles,
7+
createProjectGraphAsync: jest.fn().mockImplementation(async () => {
8+
return {
9+
nodes: {},
10+
dependencies: {},
11+
};
12+
}),
13+
}));
214

3-
import * as devkit from '@nx/devkit';
415
import {
516
addProjectConfiguration,
617
readJson,
@@ -228,7 +239,6 @@ describe('librarySecondaryEntryPoint generator', () => {
228239
});
229240

230241
it('should format files', async () => {
231-
jest.spyOn(devkit, 'formatFiles');
232242
addProjectConfiguration(tree, 'lib1', {
233243
root: 'libs/lib1',
234244
projectType: 'library',
@@ -243,7 +253,7 @@ describe('librarySecondaryEntryPoint generator', () => {
243253
library: 'lib1',
244254
});
245255

246-
expect(devkit.formatFiles).toHaveBeenCalled();
256+
expect(mockFormatFiles).toHaveBeenCalled();
247257
expect(
248258
tree.read('libs/lib1/testing/src/index.ts', 'utf-8')
249259
).toMatchSnapshot();

0 commit comments

Comments
 (0)