Skip to content

Commit

Permalink
test: Use await expect().rejects.toThrow() instead of brittle `try/…
Browse files Browse the repository at this point in the history
…catch`

This appeases the `jest/no-try-expect` rule promoted to the "recommended" config in `eslint-plugin-jest` v23
  • Loading branch information
evocateur committed Nov 20, 2019
1 parent 406ba5a commit 560e098
Show file tree
Hide file tree
Showing 44 changed files with 495 additions and 1,019 deletions.
64 changes: 23 additions & 41 deletions commands/add/__tests__/add-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,24 @@ describe("AddCommand", () => {
getManifest.mockResolvedValue({ version: "1.0.0" });

it("should throw without packages", async () => {
expect.assertions(1);

const testDir = await initFixture("basic");
const command = lernaAdd(testDir)();

try {
await lernaAdd(testDir)();
} catch (err) {
expect(err.message).toMatch(/^Not enough non-option arguments/);
}
await expect(command).rejects.toThrow(/^Not enough non-option arguments/);
});

it("should throw for locally unsatisfiable version ranges", async () => {
expect.assertions(1);

const testDir = await initFixture("basic");
const command = lernaAdd(testDir)("@test/package-1@2");

try {
await lernaAdd(testDir)("@test/package-1@2");
} catch (err) {
expect(err.message).toMatch(/Requested range not satisfiable:/);
}
await expect(command).rejects.toThrow(/Requested range not satisfiable:/);
});

it("should throw for adding local package without specified version", async () => {
expect.assertions(1);

const testDir = await initFixture("unspecified-version");
const command = lernaAdd(testDir)("@test/package-1");

try {
await lernaAdd(testDir)("@test/package-1");
} catch (err) {
expect(err.message).toMatch(/Requested package has no version:/);
}
await expect(command).rejects.toThrow(/Requested package has no version:/);
});

it("should reference remote dependencies", async () => {
Expand Down Expand Up @@ -233,26 +218,23 @@ describe("AddCommand", () => {
return Promise.reject(err);
});

try {
await lernaAdd(testDir)(
"@my-own/private-idaho",
"--registry",
"http://registry.cuckoo-banana-pants.com/"
);
} catch (err) {
// obviously this registry doesn't exist, thus it will always error
expect(err.message).toMatch("ENOTFOUND");
expect(getManifest).toHaveBeenLastCalledWith(
expect.objectContaining({
name: "@my-own/private-idaho",
}),
expect.objectContaining({
registry: "http://registry.cuckoo-banana-pants.com/",
})
);
}

expect.hasAssertions();
const command = lernaAdd(testDir)(
"@my-own/private-idaho",
"--registry",
"http://registry.cuckoo-banana-pants.com/"
);

// obviously this registry doesn't exist, thus it will always error
await expect(command).rejects.toThrow(/ENOTFOUND/);

expect(getManifest).toHaveBeenLastCalledWith(
expect.objectContaining({
name: "@my-own/private-idaho",
}),
expect.objectContaining({
registry: "http://registry.cuckoo-banana-pants.com/",
})
);
});

it("should bootstrap changed packages", async () => {
Expand Down
62 changes: 17 additions & 45 deletions commands/bootstrap/__tests__/bootstrap-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,21 @@ describe("BootstrapCommand", () => {

describe("with --npm-client and --hoist", () => {
it("should throw", async () => {
expect.assertions(1);

const testDir = await initFixture("yarn-hoist");
const command = lernaBootstrap(testDir)();

try {
await lernaBootstrap(testDir)();
} catch (err) {
expect(err.message).toMatch(
"--hoist is not supported with --npm-client=yarn, use yarn workspaces instead"
);
}
await expect(command).rejects.toThrow(
"--hoist is not supported with --npm-client=yarn, use yarn workspaces instead"
);
});
});

describe("with --hoist and --strict", () => {
it("should throw if there's a hoist warning", async () => {
expect.assertions(1);

const testDir = await initFixture("basic");
const command = lernaBootstrap(testDir)("--hoist", "--strict");

try {
await lernaBootstrap(testDir)("--hoist", "--strict");
} catch (err) {
expect(err.message).toMatch("Package version inconsistencies found");
}
await expect(command).rejects.toThrow("Package version inconsistencies found");
});
});

Expand Down Expand Up @@ -609,17 +599,12 @@ describe("BootstrapCommand", () => {
});

it("errors when package.json workspaces exists but --use-workspaces is not enabled", async () => {
expect.assertions(1);

const testDir = await initFixture("yarn-workspaces");
const command = lernaBootstrap(testDir)("--no-use-workspaces");

try {
await lernaBootstrap(testDir)("--no-use-workspaces");
} catch (err) {
expect(err.message).toMatch(
"Yarn workspaces are configured in package.json, but not enabled in lerna.json!"
);
}
await expect(command).rejects.toThrow(
"Yarn workspaces are configured in package.json, but not enabled in lerna.json!"
);
});
});

Expand All @@ -642,29 +627,19 @@ describe("BootstrapCommand", () => {

describe("with duplicate package names", () => {
it("throws an error", async () => {
expect.assertions(1);

const testDir = await initFixture("duplicate-package-names");
const command = lernaBootstrap(testDir)();

try {
await lernaBootstrap(testDir)();
} catch (err) {
expect(err.message).toMatch(`Package name "package-1" used in multiple packages`);
}
await expect(command).rejects.toThrow(`Package name "package-1" used in multiple packages`);
});
});

describe("in a cyclical repo", () => {
it("should throw an error with --reject-cycles", async () => {
expect.assertions(1);

const testDir = await initFixture("toposort");
const command = lernaBootstrap(testDir)("--reject-cycles");

try {
await lernaBootstrap(testDir)("--reject-cycles");
} catch (err) {
expect(err.message).toMatch("Dependency cycles detected, you should fix these!");
}
await expect(command).rejects.toThrow("Dependency cycles detected, you should fix these!");
});
});

Expand All @@ -689,16 +664,13 @@ describe("BootstrapCommand", () => {
});

it("requires a git repo when using --since", async () => {
expect.assertions(1);
const testDir = await initFixture("zero-pkgs");

await fs.remove(path.join(testDir, ".git"));

try {
await lernaBootstrap(testDir)("--since", "some-branch");
} catch (err) {
expect(err.message).toMatch("this is not a git repository");
}
const command = lernaBootstrap(testDir)("--since", "some-branch");

await expect(command).rejects.toThrow("this is not a git repository");
});

describe("with force-local", () => {
Expand Down
20 changes: 5 additions & 15 deletions commands/clean/__tests__/clean-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,21 @@ describe("CleanCommand", () => {
});

it("exits non-zero when rimraf errors egregiously", async () => {
expect.assertions(1);
rimrafDir.mockImplementationOnce(() => Promise.reject(new Error("whoops")));

const testDir = await initFixture("basic");
const command = lernaClean(testDir)();

rimrafDir.mockImplementationOnce(() => Promise.reject(new Error("whoops")));

try {
await lernaClean(testDir)();
} catch (err) {
expect(err.message).toMatch("whoops");
}
await expect(command).rejects.toThrow("whoops");
});

it("requires a git repo when using --since", async () => {
expect.assertions(1);

const testDir = await initFixture("basic");

await fs.remove(path.join(testDir, ".git"));

try {
await lernaClean(testDir)("--since", "some-branch");
} catch (err) {
expect(err.message).toMatch("this is not a git repository");
}
const command = lernaClean(testDir)("--since", "some-branch");
await expect(command).rejects.toThrow("this is not a git repository");
});
});

Expand Down
22 changes: 8 additions & 14 deletions commands/create/__tests__/create-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,20 @@ describe("CreateCommand", () => {

it("requires a name argument", async () => {
const cwd = await initFixture("basic");
const command = lernaCreate(cwd)();

try {
await lernaCreate(cwd)();
} catch (err) {
expect(err.message).toMatch("Not enough non-option arguments");
}
await expect(command).rejects.toThrow("Not enough non-option arguments");
});

it("throws when adding a git dependency", async () => {
const cwd = await initRemoteFixture("basic");
const command = lernaCreate(cwd)(
"git-pkg",
"--dependencies",
"git+ssh://[email protected]/user/foo#semver:^1.2.3"
);

try {
await lernaCreate(cwd)(
"git-pkg",
"--dependencies",
"git+ssh://[email protected]/user/foo#semver:^1.2.3"
);
} catch (err) {
expect(err.message).toMatch("Do not use git dependencies");
}
await expect(command).rejects.toThrow("Do not use git dependencies");
});

it("creates a stub package", async () => {
Expand Down
21 changes: 6 additions & 15 deletions commands/diff/__tests__/diff-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ describe("DiffCommand", () => {

it("should error when attempting to diff a package that doesn't exist", async () => {
const cwd = await initFixture("basic");
const command = lernaDiff(cwd)("missing");

try {
await lernaDiff(cwd)("missing");
} catch (err) {
expect(err.message).toBe("Cannot diff, the package 'missing' does not exist.");
}
await expect(command).rejects.toThrow("Cannot diff, the package 'missing' does not exist.");
});

it("should error when running in a repository without commits", async () => {
Expand All @@ -98,11 +95,8 @@ describe("DiffCommand", () => {
await fs.remove(path.join(cwd, ".git"));
await gitInit(cwd);

try {
await lernaDiff(cwd)("package-1");
} catch (err) {
expect(err.message).toBe("Cannot diff, there are no commits in this repository yet.");
}
const command = lernaDiff(cwd)("package-1");
await expect(command).rejects.toThrow("Cannot diff, there are no commits in this repository yet.");
});

it("should error when git diff exits non-zero", async () => {
Expand All @@ -115,10 +109,7 @@ describe("DiffCommand", () => {
throw nonZero;
});

try {
await lernaDiff(cwd)("package-1");
} catch (err) {
expect(err.message).toBe("An actual non-zero, not git diff pager SIGPIPE");
}
const command = lernaDiff(cwd)("package-1");
await expect(command).rejects.toThrow("An actual non-zero, not git diff pager SIGPIPE");
});
});
44 changes: 17 additions & 27 deletions commands/exec/__tests__/exec-command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,9 @@ describe("ExecCommand", () => {
});

it("should complain if invoked without command", async () => {
try {
await lernaExec(testDir)("--parallel");
} catch (err) {
expect(err.message).toBe("A command to execute is required");
}
const command = lernaExec(testDir)("--parallel");

expect.hasAssertions();
await expect(command).rejects.toThrow("A command to execute is required");
});

it("rejects with execution error", async () => {
Expand All @@ -62,13 +58,15 @@ describe("ExecCommand", () => {
throw boom;
});

try {
await lernaExec(testDir)("boom");
} catch (err) {
expect(err.message).toBe("execution error");
expect(err.cmd).toBe("boom");
expect(process.exitCode).toBe(123);
}
const command = lernaExec(testDir)("boom");

await expect(command).rejects.toThrow(
expect.objectContaining({
message: "execution error",
cmd: "boom",
})
);
expect(process.exitCode).toBe(123);
});

it("should ignore execution errors with --no-bail", async () => {
Expand All @@ -83,13 +81,10 @@ describe("ExecCommand", () => {
return Promise.resolve(boom);
});

try {
await lernaExec(testDir)("boom", "--no-bail", "--", "--shaka", "--lakka");
} catch (err) {
expect(err.message).toBe("package-1");
expect(err.cmd).toBe("boom --shaka --lakka");
expect(process.exitCode).toBe(456);
}
await lernaExec(testDir)("boom", "--no-bail", "--", "--shaka", "--lakka");

// command doesn't throw, but it _does_ set exitCode
expect(process.exitCode).toBe(456);

expect(ChildProcessUtilities.spawn).toHaveBeenCalledTimes(2);
expect(ChildProcessUtilities.spawn).toHaveBeenLastCalledWith(
Expand Down Expand Up @@ -239,14 +234,9 @@ describe("ExecCommand", () => {

it("throws an error with --reject-cycles", async () => {
const testDir = await initFixture("toposort");
const command = lernaExec(testDir)("ls", "--reject-cycles");

try {
await lernaExec(testDir)("ls", "--reject-cycles");
} catch (err) {
expect(err.message).toMatch("Dependency cycles detected, you should fix these!");
}

expect.hasAssertions();
await expect(command).rejects.toThrow("Dependency cycles detected, you should fix these!");
});
});
});
Loading

0 comments on commit 560e098

Please sign in to comment.