Skip to content

Commit 331ebce

Browse files
authored
chore(repo): stop update-package-json spec from writing to disk (#35395)
## Current Behavior Running `nx run rollup:test` produces three unexpected writes to the workspace root that are not declared as outputs: - `dist/index.js/index.cjs.default.js` - `dist/index.js/index.cjs.mjs` - `dist/index.js/package.json` These show up as sandbox violations because the spec file at `packages/rollup/src/plugins/package-json/update-package-json.spec.ts` is, in reality, writing to the filesystem during test runs: 1. `jest.spyOn(utils, 'writeJsonFile')` is used without `.mockImplementation(...)`. `spyOn` wraps the function to record calls but does not replace its behavior, so the real `writeJsonFile` runs and writes `package.json`. 2. `update-package-json.ts` also calls `fs.writeFileSync` directly (for the `.cjs.mjs` / `.cjs.default.js` CJS-interop shims), which the spec never mocked at all. Every run pollutes the workspace root with a `dist/index.js/` directory. ## Expected Behavior Tests verify call arguments via the spies without touching the real filesystem. No `dist/index.js/` directory is created, and the three sandbox violations for `rollup:test` go away. - All 11 `writeJsonFile` spies now use `.mockImplementation(() => undefined)`. - A `beforeEach` / `afterEach` adds a `jest.spyOn(fs, 'writeFileSync').mockImplementation(() => undefined)` using `require('fs')` so the spy lands on the real fs module (using `import * as fs from 'fs'` would go through `esModuleInterop`'s `__importStar` copy and miss the call site in `update-package-json.ts`). - Assertions continue to work — `toHaveBeenCalledWith(...)` tracks calls on spies with or without a mock implementation. Verified: `nx test rollup --testPathPatterns=update-package-json.spec` → 11/11 passing, and no `dist/index.js/` is created during the run. ## Related Issue(s) N/A — caught via the sandbox-violation report for `rollup:test`.
1 parent 3027707 commit 331ebce

1 file changed

Lines changed: 48 additions & 11 deletions

File tree

packages/rollup/src/plugins/package-json/update-package-json.spec.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import * as utils from 'nx/src/utils/fileutils';
33
import { PackageJson } from 'nx/src/utils/package-json';
44

55
describe('updatePackageJson', () => {
6+
let writeFileSyncSpy: jest.SpyInstance;
7+
8+
beforeEach(() => {
9+
// Use require to get the real fs module object (esModuleInterop's __importStar
10+
// would create a copy, and spies on the copy wouldn't affect the callsite).
11+
const fs = require('fs');
12+
writeFileSyncSpy = jest
13+
.spyOn(fs, 'writeFileSync')
14+
.mockImplementation(() => undefined);
15+
});
16+
17+
afterEach(() => {
18+
writeFileSyncSpy.mockRestore();
19+
});
20+
621
const commonOptions = {
722
outputPath: 'dist/index.js',
823
tsConfig: './tsconfig.json',
@@ -16,7 +31,9 @@ describe('updatePackageJson', () => {
1631

1732
describe('generateExportsField: true', () => {
1833
it('should support ESM', () => {
19-
const spy = jest.spyOn(utils, 'writeJsonFile');
34+
const spy = jest
35+
.spyOn(utils, 'writeJsonFile')
36+
.mockImplementation(() => undefined);
2037

2138
updatePackageJson(
2239
{
@@ -45,7 +62,9 @@ describe('updatePackageJson', () => {
4562
});
4663

4764
it('should support CJS', () => {
48-
const spy = jest.spyOn(utils, 'writeJsonFile');
65+
const spy = jest
66+
.spyOn(utils, 'writeJsonFile')
67+
.mockImplementation(() => undefined);
4968

5069
updatePackageJson(
5170
{
@@ -70,7 +89,9 @@ describe('updatePackageJson', () => {
7089
});
7190

7291
it('should support ESM + CJS', () => {
73-
const spy = jest.spyOn(utils, 'writeJsonFile');
92+
const spy = jest
93+
.spyOn(utils, 'writeJsonFile')
94+
.mockImplementation(() => undefined);
7495

7596
updatePackageJson(
7697
{
@@ -100,7 +121,9 @@ describe('updatePackageJson', () => {
100121
});
101122

102123
it('should support custom exports field', () => {
103-
const spy = jest.spyOn(utils, 'writeJsonFile');
124+
const spy = jest
125+
.spyOn(utils, 'writeJsonFile')
126+
.mockImplementation(() => undefined);
104127

105128
updatePackageJson(
106129
{
@@ -142,7 +165,9 @@ describe('updatePackageJson', () => {
142165

143166
describe('generateExportsField: false', () => {
144167
it('should support ESM', () => {
145-
const spy = jest.spyOn(utils, 'writeJsonFile');
168+
const spy = jest
169+
.spyOn(utils, 'writeJsonFile')
170+
.mockImplementation(() => undefined);
146171

147172
updatePackageJson(
148173
{
@@ -163,7 +188,9 @@ describe('updatePackageJson', () => {
163188
});
164189

165190
it('should support CJS', () => {
166-
const spy = jest.spyOn(utils, 'writeJsonFile');
191+
const spy = jest
192+
.spyOn(utils, 'writeJsonFile')
193+
.mockImplementation(() => undefined);
167194

168195
updatePackageJson(
169196
{
@@ -183,7 +210,9 @@ describe('updatePackageJson', () => {
183210
});
184211

185212
it('should support ESM + CJS', () => {
186-
const spy = jest.spyOn(utils, 'writeJsonFile');
213+
const spy = jest
214+
.spyOn(utils, 'writeJsonFile')
215+
.mockImplementation(() => undefined);
187216

188217
updatePackageJson(
189218
{
@@ -203,7 +232,9 @@ describe('updatePackageJson', () => {
203232
});
204233

205234
it('should support custom exports field', () => {
206-
const spy = jest.spyOn(utils, 'writeJsonFile');
235+
const spy = jest
236+
.spyOn(utils, 'writeJsonFile')
237+
.mockImplementation(() => undefined);
207238

208239
updatePackageJson(
209240
{
@@ -233,7 +264,9 @@ describe('updatePackageJson', () => {
233264

234265
describe('skipTypeField', () => {
235266
it('should not include type field if skipTypeField is true', () => {
236-
const spy = jest.spyOn(utils, 'writeJsonFile');
267+
const spy = jest
268+
.spyOn(utils, 'writeJsonFile')
269+
.mockImplementation(() => undefined);
237270

238271
updatePackageJson(
239272
{
@@ -261,7 +294,9 @@ describe('updatePackageJson', () => {
261294
});
262295

263296
it('should include type field if skipTypeField is undefined', () => {
264-
const spy = jest.spyOn(utils, 'writeJsonFile');
297+
const spy = jest
298+
.spyOn(utils, 'writeJsonFile')
299+
.mockImplementation(() => undefined);
265300

266301
updatePackageJson(
267302
{
@@ -289,7 +324,9 @@ describe('updatePackageJson', () => {
289324
});
290325

291326
it('should include type field if skipTypeField is false', () => {
292-
const spy = jest.spyOn(utils, 'writeJsonFile');
327+
const spy = jest
328+
.spyOn(utils, 'writeJsonFile')
329+
.mockImplementation(() => undefined);
293330

294331
updatePackageJson(
295332
{

0 commit comments

Comments
 (0)