@@ -30,6 +30,7 @@ import { ChildProcessModuleMock, ISpawnMockConfig } from 'child_process';
3030import { FileSystem } from '@microsoft/node-core-library' ;
3131import { Interleaver } from '@microsoft/stream-collator' ;
3232import { 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 } ) ;
0 commit comments