|
3 | 3 |
|
4 | 4 | // Mock child_process so we can verify tasks are (or are not) invoked as we expect |
5 | 5 | 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 | +}); |
8 | 24 |
|
9 | 25 | import { resolve } from 'path'; |
10 | 26 | import { ChildProcessModuleMock, ISpawnMockConfig } from 'child_process'; |
@@ -48,27 +64,6 @@ function getCommandLineParserInstance(repoName: string, taskName: string): IPars |
48 | 64 | }; |
49 | 65 | } |
50 | 66 |
|
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 | | - |
72 | 67 | /** |
73 | 68 | * Configure the `child_process` `spawn` mock for these tests. This relies on the mock implementation |
74 | 69 | * in `__mocks__/child_process.js`. |
|
0 commit comments