Skip to content

Commit 08240a3

Browse files
committed
Fix tests.
1 parent 444660b commit 08240a3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

apps/rush-lib/src/cli/test/RushCommandLineParser.test.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ChildProcessModuleMock, ISpawnMockConfig } from 'child_process';
3030
import { FileSystem } from '@microsoft/node-core-library';
3131
import { Interleaver } from '@microsoft/stream-collator';
3232
import { RushCommandLineParser } from '../RushCommandLineParser';
33+
import { PackageJsonUpdater } from '../../logic/PackageJsonUpdater';
3334

3435
/**
3536
* Interface definition for a test instance for the RushCommandLineParser.
@@ -284,11 +285,27 @@ describe('RushCommandLineParser', () => {
284285
});
285286

286287
describe(`in repo with tests for add`, () => {
288+
let doRushAddMock: jest.SpyInstance;
289+
let oldExitCode: number;
290+
let oldArgs: string[];
291+
292+
beforeEach(() => {
293+
doRushAddMock = jest.spyOn(PackageJsonUpdater.prototype, 'doRushAdd').mockImplementation(() => Promise.resolve());
294+
jest.spyOn(process, 'exit').mockImplementation(() => { /* stub */ });
295+
oldExitCode = process.exitCode;
296+
oldArgs = process.argv;
297+
});
298+
299+
afterEach(() => {
300+
jest.clearAllMocks();
301+
process.exitCode = oldExitCode;
302+
process.argv = oldArgs;
303+
});
304+
287305
describe(`'add' action`, () => {
288306
it(`adds a dependency to just one repo in the workspace`, () => {
289307
const startPath: string = resolve(__dirname, 'addRepo');
290-
const aPath: string = resolve(__dirname, 'addRepo/a');
291-
FileSystem.deleteFile(resolve(__dirname, `addRepo/a/package-deps.json`));
308+
const aPath: string = resolve(__dirname, 'addRepo', 'a');
292309

293310
// Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit
294311
// to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test
@@ -297,26 +314,24 @@ describe('RushCommandLineParser', () => {
297314
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath });
298315

299316
// Switching to the "a" package of addRepo
300-
process.chdir(aPath);
317+
jest.spyOn(process, 'cwd').mockReturnValue(aPath);
301318

302319
// Mock the command
303320
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', 'add', '-p', 'assert'];
304321

305-
expect.assertions(2);
306322
return expect(parser.execute()).resolves.toEqual(true)
307323
.then(() => {
308-
const packageJSON = FileSystem.readFile(resolve(__dirname, `addRepo/a/package.json`));
309-
expect(packageJSON).toEqual(expect.stringMatching('"assert"'));
324+
expect(doRushAddMock).toHaveBeenCalledTimes(1);
325+
expect(doRushAddMock.mock.calls[0][0].currentProject.packageName).toEqual('a');
326+
expect(doRushAddMock.mock.calls[0][0].packageName).toEqual('assert');
310327
});
311328
});
312329
});
313330

314331
describe(`'add' action with --all`, () => {
315332
it(`adds a dependency to all repos in the workspace`, () => {
316333
const startPath: string = resolve(__dirname, 'addRepo');
317-
const aPath: string = resolve(__dirname, 'addRepo/a');
318-
FileSystem.deleteFile(resolve(__dirname, `addRepo/a/package-deps.json`));
319-
FileSystem.deleteFile(resolve(__dirname, `addRepo/b/package-deps.json`));
334+
const aPath: string = resolve(__dirname, 'addRepo', 'a');
320335

321336
// Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit
322337
// to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test
@@ -325,18 +340,18 @@ describe('RushCommandLineParser', () => {
325340
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath });
326341

327342
// Switching to the "a" package of addRepo
328-
process.chdir(aPath);
343+
jest.spyOn(process, 'cwd').mockReturnValue(aPath);
329344

330345
// Mock the command
331346
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', 'add', '-p', 'assert', '--all'];
332347

333-
expect.assertions(3);
334348
return expect(parser.execute()).resolves.toEqual(true)
335349
.then(() => {
336-
const packageAJSON = FileSystem.readFile(resolve(__dirname, `addRepo/a/package.json`));
337-
expect(packageAJSON).toEqual(expect.stringMatching('"assert"'));
338-
const packageBJSON = FileSystem.readFile(resolve(__dirname, `addRepo/b/package.json`));
339-
expect(packageBJSON).toEqual(expect.stringMatching('"assert"'));
350+
expect(doRushAddMock).toHaveBeenCalledTimes(2);
351+
expect(doRushAddMock.mock.calls[0][0].currentProject.packageName).toEqual('a');
352+
expect(doRushAddMock.mock.calls[0][0].packageName).toEqual('assert');
353+
expect(doRushAddMock.mock.calls[1][0].currentProject.packageName).toEqual('b');
354+
expect(doRushAddMock.mock.calls[1][0].packageName).toEqual('assert');
340355
});
341356
});
342357
});

apps/rush-lib/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Optional arguments:
9090
same version of the dependency.
9191
-s, --skip-update If specified, the \\"rush update\\" command will not be
9292
run after updating the package.json files.
93-
--all If specified, the dependency will be added to all of
94-
your projects.
93+
--all If specified, the dependency will be added to all
94+
projects.
9595
"
9696
`;
9797

0 commit comments

Comments
 (0)