Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Add command unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Nov 1, 2019
1 parent e0d58bd commit d3132b9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
1 change: 0 additions & 1 deletion jest.config.js
Expand Up @@ -29,5 +29,4 @@ module.exports = {

// The glob patterns Jest uses to detect test files
testMatch: ["**/test/**/?(*.)+(spec|test).js?(x)"]
//testMatch: ["**/test/link.spec.js"]
};
1 change: 1 addition & 0 deletions src/command.js
Expand Up @@ -32,6 +32,7 @@ const runAndCatch = () => {
return run().catch(err => {
console.log(chalk.red(`\nERROR: ${err.message}.\n`));
process.exitCode = 1;
return Promise.resolve();
});
};

Expand Down
69 changes: 69 additions & 0 deletions test/command.spec.js
@@ -0,0 +1,69 @@
const sinon = require("sinon");

const link = require("../src/link");
const unlink = require("../src/unlink");
const check = require("../src/check");

const command = require("../src/command");

describe("command", () => {
let sandbox;
let originalArgs;

beforeAll(() => {
originalArgs = [...process.argv];
});

beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.stub(link, "all").resolves();
sandbox.stub(link, "select").resolves();
sandbox.stub(unlink, "all").resolves();
sandbox.stub(check, "avoidFileLinks").resolves();
});

afterEach(() => {
process.argv = [...originalArgs];
sandbox.restore();
});

describe("runAndCatch method", () => {
it("should mark process to exit with error if command throws an error", async () => {
expect.assertions(2);
link.select.rejects(new Error("Foo error"));
sandbox.stub(console, "log");
await command.runAndCatch();
expect(process.exitCode).toEqual(1);
process.exitCode = 0;
expect(console.log.getCall(0).args[0]).toEqual(expect.stringContaining("Foo error"));
});

it("should link selected packages by default", async () => {
expect.assertions(1);
await command.runAndCatch();
expect(link.select.callCount).toEqual(1);
});

it("should link all packages if --all option is received", async () => {
expect.assertions(1);
process.argv.push("--all");
await command.runAndCatch();
expect(link.all.callCount).toEqual(1);
});

it("should unlink all packages if --all and --unlink options are received", async () => {
expect.assertions(1);
process.argv.push("--all");
process.argv.push("--unlink");
await command.runAndCatch();
expect(unlink.all.callCount).toEqual(1);
});

it("should check links if --check option is received", async () => {
expect.assertions(1);
process.argv.push("--check");
await command.runAndCatch();
expect(check.avoidFileLinks.callCount).toEqual(1);
});
});
});

0 comments on commit d3132b9

Please sign in to comment.