Skip to content

Commit d98d228

Browse files
committed
Fix an issue with RushCommandLineParser tests on Mac and Linux.
1 parent a92c0f1 commit d98d228

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

apps/rush-lib/__mocks__/child_process.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const EventEmitter = require('events');
55

66
const childProcess = jest.genMockFromModule('child_process');
7+
const childProcessActual = jest.requireActual('child_process');
78
childProcess.spawn.mockImplementation(spawn);
89
childProcess.__setSpawnMockConfig = setSpawnMockConfig;
910

@@ -59,4 +60,9 @@ function spawn(file, args, options) {
5960
return cp;
6061
}
6162

63+
/**
64+
* Ensure the real spawnSync function is used, otherwise LockFile breaks.
65+
*/
66+
childProcess.spawnSync = childProcessActual.spawnSync;
67+
6268
module.exports = childProcess;

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@
33

44
// Mock child_process so we can verify tasks are (or are not) invoked as we expect
55
jest.mock('child_process');
6-
// Mock RushCommandLineParser itself to prevent `process.exit` to be called on failure
7-
jest.mock('../RushCommandLineParser', rushCommandLineParserMockFactory);
6+
7+
/**
8+
* Mock RushCommandLineParser itself to prevent `process.exit` to be called on failure
9+
*/
10+
jest.mock('../RushCommandLineParser', () => {
11+
// tslint:disable-next-line: no-any
12+
const actualModule: any = jest.requireActual('../RushCommandLineParser');
13+
if (actualModule.RushCommandLineParser) {
14+
// Stub out the troublesome method that calls `process.exit`
15+
actualModule.RushCommandLineParser.prototype._reportErrorAndSetExitCode = mockReportErrorAndSetExitCode;
16+
}
17+
return actualModule;
18+
19+
function mockReportErrorAndSetExitCode(error: Error): void {
20+
// Just rethrow the error so the unit tests can catch it
21+
throw error;
22+
}
23+
});
824

925
import { resolve } from 'path';
1026
import { ChildProcessModuleMock, ISpawnMockConfig } from 'child_process';
@@ -48,27 +64,6 @@ function getCommandLineParserInstance(repoName: string, taskName: string): IPars
4864
};
4965
}
5066

51-
/**
52-
* Create a monkey patch of RushCommandLineParser so unit tests can validate failure cases.
53-
*/
54-
// tslint:disable-next-line: no-any
55-
function rushCommandLineParserMockFactory(): any {
56-
// tslint:disable-next-line: no-any
57-
const actualModule: any = jest.requireActual('../RushCommandLineParser');
58-
if (actualModule.RushCommandLineParser) {
59-
// Stub out the troublesome method that calls `process.exit`
60-
actualModule.RushCommandLineParser.prototype._reportErrorAndSetExitCode = mockReportErrorAndSetExitCode;
61-
}
62-
return actualModule;
63-
64-
//////////////////
65-
66-
function mockReportErrorAndSetExitCode(error: Error): void {
67-
// Just rethrow the error so the unit tests can catch it
68-
throw error;
69-
}
70-
}
71-
7267
/**
7368
* Configure the `child_process` `spawn` mock for these tests. This relies on the mock implementation
7469
* in `__mocks__/child_process.js`.

0 commit comments

Comments
 (0)