X Tutup
Skip to content

Commit fcc5266

Browse files
committed
tests work now
1 parent d70dd20 commit fcc5266

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

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

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// Mock child_process so we can verify tasks are (or are not) invoked as we expect
55
jest.mock('child_process');
66

7+
// add needs more than the default 5 seconds
8+
jest.setTimeout(30000);
9+
710
/**
811
* Mock RushCommandLineParser itself to prevent `process.exit` to be called on failure
912
*/
@@ -39,7 +42,7 @@ interface IParserTestInstance {
3942
/**
4043
* Helper to set up a test instance for RushCommandLineParser.
4144
*/
42-
function getCommandLineParserInstance(repoName: string, taskName: string, ...otherParams: string[]): IParserTestInstance {
45+
function getCommandLineParserInstance(repoName: string, taskName: string): IParserTestInstance {
4346
// Point to the test repo folder
4447
const startPath: string = resolve(__dirname, repoName);
4548

@@ -55,7 +58,7 @@ function getCommandLineParserInstance(repoName: string, taskName: string, ...oth
5558
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath });
5659

5760
// Mock the command
58-
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', taskName, ...otherParams];
61+
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', taskName];
5962
const spawnMock: jest.Mock = setSpawnMock();
6063

6164
return {
@@ -283,15 +286,57 @@ describe('RushCommandLineParser', () => {
283286
describe(`in repo with tests for add`, () => {
284287
describe(`'add' action`, () => {
285288
it(`adds a dependency to just one sub-repo`, () => {
286-
const repoName: string = 'addRepo';
287-
const instance: IParserTestInstance = getCommandLineParserInstance(repoName, 'add', 'assert', '-t', 'a');
289+
const startPath: string = resolve(__dirname, 'addRepo');
290+
const aPath: string = resolve(__dirname, 'addRepo/a');
291+
FileSystem.deleteFile(resolve(__dirname, `addRepo/a/package-deps.json`));
288292

289-
expect.assertions(1);
290-
return expect(instance.parser.execute()).resolves.toEqual(true)
293+
// Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit
294+
// to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test
295+
// repo will fail due to contention over the same lock which is kept until the test runner process
296+
// ends.
297+
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath });
298+
299+
// Switching to the "a" package of addRepo
300+
process.chdir(aPath);
301+
302+
// Mock the command
303+
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', 'add', '-p', 'assert'];
304+
305+
expect.assertions(2);
306+
return expect(parser.execute()).resolves.toEqual(true)
307+
.then(() => {
308+
const packageJSON = FileSystem.readFile(resolve(__dirname, `addRepo/a/package.json`));
309+
expect(packageJSON).toEqual(expect.stringMatching('"assert"'));
310+
});
311+
});
312+
});
313+
314+
describe(`'add' action with --all`, () => {
315+
it(`adds a dependency to just one sub-repo`, () => {
316+
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`));
320+
321+
// Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit
322+
// to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test
323+
// repo will fail due to contention over the same lock which is kept until the test runner process
324+
// ends.
325+
const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath });
326+
327+
// Switching to the "a" package of addRepo
328+
process.chdir(aPath);
329+
330+
// Mock the command
331+
process.argv = ['pretend-this-is-node.exe', 'pretend-this-is-rush', 'add', '-p', 'assert', '--all'];
332+
333+
expect.assertions(3);
334+
return expect(parser.execute()).resolves.toEqual(true)
291335
.then(() => {
292-
// There should be 1 build per package
293-
const packageCount: number = instance.spawnMock.mock.calls.length;
294-
expect(packageCount).toEqual(1);
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"'));
295340
});
296341
});
297342
});

0 commit comments

Comments
 (0)
X Tutup