Skip to content

Commit

Permalink
Refactor tests to better employ Jest patterns (lerna#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur authored Mar 20, 2017
1 parent 0749ea6 commit d34be78
Show file tree
Hide file tree
Showing 15 changed files with 597 additions and 717 deletions.
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ matrix:
fast_finish: true

cache:
- "%LOCALAPPDATA%\\Yarn"
# unpacking the yarn cache takes way too long
# - "%LOCALAPPDATA%\\Yarn"
- node_modules -> yarn.lock

install:
Expand Down
442 changes: 201 additions & 241 deletions test/BootstrapCommand.js

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions test/CleanCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import stub from "./helpers/stub";
import assertStubbedCalls from "./helpers/assertStubbedCalls";

describe("CleanCommand", () => {

describe("basic tests", () => {
let testDir;

Expand Down Expand Up @@ -58,8 +57,8 @@ describe("CleanCommand", () => {

// Both of these commands should result in the same outcome
const filters = [
{ test: "should only clean scoped packages", flag: "scope", flagValue: "package-@(1|2)"},
{ test: "should not clean ignored packages", flag: "ignore", flagValue: "package-@(3|4)"},
{ test: "should only clean scoped packages", flag: "scope", flagValue: "package-@(1|2)" },
{ test: "should not clean ignored packages", flag: "ignore", flagValue: "package-@(3|4)" },
];
filters.forEach((filter) => {
it(filter.test, (done) => {
Expand Down Expand Up @@ -88,7 +87,8 @@ describe("CleanCommand", () => {
callback();
});

cleanCommand.runCommand(exitWithCode(0, () => {
cleanCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
assert.deepEqual(ranInPackages, ["package-1", "package-2"]);
done();
}));
Expand Down Expand Up @@ -124,7 +124,8 @@ describe("CleanCommand", () => {
callback();
});

cleanCommand.runCommand(exitWithCode(0, () => {
cleanCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
const expected = ["package-1", "package-2"].map((pkg) => path.join(testDir, "packages", pkg, "node_modules"));
assert.deepEqual(ranInPackages.sort(), expected.sort());
done();
Expand Down
37 changes: 24 additions & 13 deletions test/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import assert from "assert";

import progressBar from "../src/progressBar";
import initFixture from "./helpers/initFixture";
import Command from "../src/Command";
import {exposeCommands} from "../src/Command";
import Command, { exposeCommands } from "../src/Command";
import logger from "../src/logger";
import stub from "./helpers/stub";

Expand Down Expand Up @@ -45,21 +44,21 @@ describe("Command", () => {

describe(".concurrency", () => {
it("should be added to the instance", () => {
const command = new Command(null, {concurrency: 6});
const command = new Command(null, { concurrency: 6 });
assert.equal(command.concurrency, 6);
});

it("should fall back to default if concurrency given is NaN", () => {
const command = new Command(null, {concurrency: "bla"});
const command = new Command(null, { concurrency: "bla" });
assert.equal(command.concurrency, 4);
});

it("should fall back to default if concurrency given is 0", () => {
assert.equal(new Command(null, {concurrency: 0}).concurrency, 4);
assert.equal(new Command(null, { concurrency: 0 }).concurrency, 4);
});

it("should fall back to 1 if concurrency given is smaller than 1", () => {
assert.equal(new Command(null, {concurrency: -1}).concurrency, 1);
assert.equal(new Command(null, { concurrency: -1 }).concurrency, 1);
});
});

Expand All @@ -70,22 +69,23 @@ describe("Command", () => {
});

it("is enabled when sort config is null", () => {
const command = new Command([], {sort: null});
const command = new Command([], { sort: null });
assert.equal(command.toposort, true);
});

it("is disabled when sort config is explicitly false (--no-sort)", () => {
const command = new Command([], {sort: false});
const command = new Command([], { sort: false });
assert.equal(command.toposort, false);
});
});

describe(".run()", () => {
it("should exist", (done) => {
class TestCommand extends Command {
initialize(callback) { callback(null, true); }
initialize(callback) {
callback(null, true);
}
execute() {

done();
}
}
Expand Down Expand Up @@ -125,15 +125,15 @@ describe("Command", () => {
});

it("should override command-level options with passed-in options", () => {
assert.equal(new TestCCommand([], {}).getOptions({testOption2: "p"}).testOption2, "p");
assert.equal(new TestCCommand([], {}).getOptions({ testOption2: "p" }).testOption2, "p");
});

it("should sieve properly within passed-in options", () => {
assert.equal(new TestCCommand([], {}).getOptions({testOption2: "p"}, {testOption2: "p2"}).testOption2, "p2");
assert.equal(new TestCCommand([], {}).getOptions({ testOption2: "p" }, { testOption2: "p2" }).testOption2, "p2");
});

it("should override everything with a CLI flag", () => {
assert.equal(new TestCCommand([], {testOption2: "f"}).getOptions({testOption2: "p"}).testOption2, "f");
assert.equal(new TestCCommand([], { testOption2: "f" }).getOptions({ testOption2: "p" }).testOption2, "f");
});

});
Expand All @@ -147,6 +147,7 @@ describe("Command", () => {
describe("bootstrapConfig", () => {
class BootstrapCommand extends Command {
}

it("should warn when used", () => {
let called = false;
stub(logger, "warn", (message) => {
Expand All @@ -156,15 +157,18 @@ describe("Command", () => {
new BootstrapCommand([], {}).getOptions();
assert.ok(called, "warning was emitted");
});

it("should provide a correct value", () => {
assert.equal(new BootstrapCommand([], {}).getOptions().ignore, "package-a");
});

it("should not warn with other commands", () => {
let called = false;
stub(logger, "warn", () => called = true);
new TestCommand([], {}).getOptions();
assert.ok(!called, "no warning was emitted");
});

it("should not provide a value to other commands", () => {
assert.equal(new TestCommand([], {}).getOptions().ignore, undefined);
});
Expand All @@ -173,6 +177,7 @@ describe("Command", () => {
describe("publishConfig", () => {
class PublishCommand extends Command {
}

it("should warn when used", () => {
let called = false;
stub(logger, "warn", (message) => {
Expand All @@ -182,15 +187,18 @@ describe("Command", () => {
new PublishCommand([], {}).getOptions();
assert.ok(called, "warning was emitted");
});

it("should provide a correct value", () => {
assert.equal(new PublishCommand([], {}).getOptions().ignore, "package-b");
});

it("should not warn with other commands", () => {
let called = false;
stub(logger, "warn", () => called = true);
new TestCommand([], {}).getOptions();
assert.ok(!called, "no warning was emitted");
});

it("should not provide a value to other commands", () => {
assert.equal(new TestCommand([], {}).getOptions().ignore, undefined);
});
Expand All @@ -213,12 +221,15 @@ describe("Command", () => {
bar: BarCommand,
});
});

it("fails on bad class name", () => {
assert.throws(() => exposeCommands([BadClassName]));
});

it("fails on duplicate class", () => {
assert.throws(() => exposeCommands([FooCommand, FooCommand]));
});

it("fails on class that doesn't extend Command", () => {
assert.throws(() => exposeCommands([NonCommand]));
});
Expand Down
16 changes: 8 additions & 8 deletions test/ExecCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ExecCommand from "../src/commands/ExecCommand";
import stub from "./helpers/stub";

describe("ExecCommand", () => {

describe("in a basic repo", () => {
let testDir;

Expand Down Expand Up @@ -61,7 +60,8 @@ describe("ExecCommand", () => {
callback();
});

execCommand.runCommand(exitWithCode(0, () => {
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
assert.equal(calls, 2);
done();
}));
Expand Down Expand Up @@ -90,16 +90,17 @@ describe("ExecCommand", () => {
callback();
});

execCommand.runCommand(exitWithCode(0, () => {
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
assert.equal(calls, 2);
done();
}));
});

// Both of these commands should result in the same outcome
const filters = [
{ test: "should run a command for a given scope", flag: "scope", flagValue: "package-1"},
{ test: "should not run a command for ignored packages", flag: "ignore", flagValue: "package-@(2|3|4)"},
{ test: "should run a command for a given scope", flag: "scope", flagValue: "package-1" },
{ test: "should not run a command for ignored packages", flag: "ignore", flagValue: "package-@(2|3|4)" },
];
filters.forEach((filter) => {
it(filter.test, (done) => {
Expand All @@ -114,13 +115,12 @@ describe("ExecCommand", () => {
callback();
});

execCommand.runCommand(exitWithCode(0, () => {
execCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
assert.deepEqual(ranInPackages, ["package-1"]);
done();
}));
});
});

});

});
Loading

0 comments on commit d34be78

Please sign in to comment.