Skip to content

Commit

Permalink
test: 🚨 updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 4, 2020
1 parent 9b24c29 commit 23fd06b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 44 deletions.
27 changes: 16 additions & 11 deletions __tests__/filter.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import * as ws from "../src/workspace"
import path from "path"
import { filter } from "../src/filter"

test("filter dir", async () => {
const workspace = await ws.getWorkspace("__tests__/workspace/apps/app1", true)
const workspace = await ws.getWorkspace({
cwd: "__tests__/workspace/apps/app1",
})
expect(workspace).toBeDefined()
if (workspace) {
filter(workspace, "apps/*")
const dirs = workspace.packages.map(p =>
path.relative("__tests__/workspace", p.root).replace(/\\/gu, "/")
)
const dirs = workspace
.getPackages("apps/*")
.map(p =>
path.relative("__tests__/workspace", p.root).replace(/\\/gu, "/")
)
dirs.sort()
expect(dirs).toStrictEqual(["apps/app1", "apps/app2"])
}
})

test("filter pkg", async () => {
const workspace = await ws.getWorkspace("__tests__/workspace/apps/app1", true)
const workspace = await ws.getWorkspace({
cwd: "__tests__/workspace/apps/app1",
})
expect(workspace).toBeDefined()
if (workspace) {
filter(workspace, "@scoped/*")
const dirs = workspace.packages.map(p =>
path.relative("__tests__/workspace", p.root).replace(/\\/gu, "/")
)
const dirs = workspace
.getPackages("@scoped/*")
.map(p =>
path.relative("__tests__/workspace", p.root).replace(/\\/gu, "/")
)
dirs.sort()
expect(dirs).toStrictEqual(["libs/lib3"])
}
Expand Down
33 changes: 20 additions & 13 deletions __tests__/parser.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { CommandParser } from "../src/parser"

test("should ", () => {
const parser = new CommandParser({ scripts: { test: "foo" } })
const parser = new CommandParser({ name: "test", scripts: { test: "foo" } })
const cmd = parser.parse("sleep 10")
expect(cmd.debug()).toStrictEqual("system:sleep 10")
})

test("no scripts", () => {
const parser = new CommandParser({})
const parser = new CommandParser({ name: "test" })
expect(parser.getScript("foo")).toBeUndefined()
const cmd = parser.parse("sleep 10")
expect(cmd.children).toHaveLength(1)
expect(cmd.children[0].args).toStrictEqual(["sleep", "10"])
})

test("test pre ", () => {
const parser = new CommandParser({ scripts: { pretest: "bar", test: "foo" } })
const parser = new CommandParser({
name: "test",
scripts: { pretest: "bar", test: "foo" },
})
const cmd = parser.parse("test")
expect(cmd.debug()).toStrictEqual({
"script:test": [{ "script:pretest": "system:bar" }, "system:foo"],
Expand All @@ -24,6 +27,7 @@ test("test pre ", () => {

test("test post ", () => {
const parser = new CommandParser({
name: "test",
scripts: { posttest: "bar", test: "foo" },
})
const cmd = parser.parse("test")
Expand All @@ -33,65 +37,68 @@ test("test post ", () => {
})

test("test bin ", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("jest")
expect(cmd.children).toHaveLength(1)
expect(cmd.debug()).toStrictEqual("bin:jest")
})

test("yarn bin", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("yarn jest")
expect(cmd.children).toHaveLength(1)
expect(cmd.debug()).toStrictEqual("bin:jest")
})

test("yarn script", () => {
const parser = new CommandParser({ scripts: { foo: "yarn jest" } })
const parser = new CommandParser({
name: "test",
scripts: { foo: "yarn jest" },
})
const cmd = parser.parse("foo")
expect(cmd.debug()).toStrictEqual({
"script:foo": "bin:jest",
})
})

test("test npx bin ", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("npx jest")
expect(cmd.debug()).toStrictEqual("bin:jest")
})

test("test op", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("npx jest && npx foo")
expect(cmd.debug()).toStrictEqual(["bin:jest", "op:&&", "system:npx foo"])
})

test("test op ;", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("test ; test")
expect(cmd.debug()).toStrictEqual(["system:test", "op:;", "system:test"])
})

test("test op &&", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("jest && test")
expect(cmd.debug()).toStrictEqual(["bin:jest", "op:&&", "system:test"])
})

test("test op ; 2", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("test; test")
expect(cmd.debug()).toStrictEqual(["system:test", "op:;", "system:test"])
})

test("test op only", () => {
const parser = new CommandParser({ scripts: {} })
const parser = new CommandParser({ name: "test", scripts: {} })
const cmd = parser.parse("&&")
expect(cmd.debug()).toStrictEqual("op:&&")
})

test("test non top level script", () => {
const parser = new CommandParser({ scripts: { test: "foo" } })
const parser = new CommandParser({ name: "test", scripts: { test: "foo" } })
const cmd = parser.parse("test")
expect(cmd.debug()).toStrictEqual({ "script:test": "system:foo" })
})
17 changes: 10 additions & 7 deletions __tests__/runner-spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ afterAll(() => {
test("failed command", async () => {
const runner = new Runner({ pretty: true })
const spawn = sinon.stub(runner, "spawn").rejects()
await runner.run("test", { scripts: { test: "fail" } })
await runner.run("test", { name: "test", scripts: { test: "fail" } })
spawn.should.be.calledWith("fail", [])
stubs.exit.should.be.calledWith(1)
})
Expand All @@ -44,37 +44,40 @@ test("formatDuration", () => {

test("error", async () => {
const runner = new Runner({ pretty: true })
await runner.run("test", { scripts: { test: "false", pretest: "false" } })
await runner.run("test", {
name: "test",
scripts: { test: "false", pretest: "false" },
})
stubs.exit.should.be.calledWith(1)
})

test("string error", async () => {
const runner = new Runner()
sinon.stub(runner, "spawn").throws({ foo: "bar" })
await runner.run("test", {})
await runner.run("test", { name: "test" })
stubs.exit.should.be.calledWith(1)
})

test("error --silent", async () => {
const runner = new Runner({ pretty: false, silent: true })
await runner.run("false", {})
await runner.run("false", { name: "test" })
stubs.exit.should.be.calledWith(1)
})

test("unknown command", async () => {
const runner = new Runner({ pretty: false })
await runner.run("foobar", {})
await runner.run("foobar", { name: "test" })
stubs.exit.should.be.calledWith(1)
})

test("warning", async () => {
const runner = new Runner({ pretty: true })
await runner.run("echo warning", {})
await runner.run("echo warning", { name: "test" })
stubs.write.should.be.calledWithMatch("⚠")
})

test("--no-pretty", async () => {
const runner = new Runner({ pretty: false })
await runner.run("echo foobar", {})
await runner.run("echo foobar", { name: "test" })
stubs.log.should.be.calledWithMatch("foobar")
})
4 changes: 3 additions & 1 deletion __tests__/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import sinon from "sinon"
import sinonChai from "sinon-chai"
import { defaults } from "../src/options"
import { Runner } from "../src/runner"
import { PackageJson } from "../src/workspace"
import { PackageJson } from "../src/package"

chai.use(sinonChai)
chai.should()
Expand All @@ -33,6 +33,7 @@ afterAll(() => {
})

const advancedPackage: PackageJson = {
name: "advanced-package",
scripts: {
prebuild: "yarn clean && yarn lint && yarn test",
"build:rollup": "npx rollup -c",
Expand Down Expand Up @@ -194,6 +195,7 @@ test("advanced build --raw", async () => {
test("concurrent ", async () => {
const runner = new Runner({})
await runner.run("test", {
name: "test",
scripts: { test: "jest" },
ultra: { concurrent: ["test"] },
})
Expand Down
42 changes: 30 additions & 12 deletions __tests__/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as ws from "../src/workspace"
import path from "path"
import { findUp } from "../src/package"
import { WorkspaceProviderType } from "../src/workspace.providers"

test("findUp ", () => {
const tests = [
Expand All @@ -8,15 +10,19 @@ test("findUp ", () => {
[".", "node_modules", path.resolve(__dirname, "../")],
[".", "package.json", path.resolve(__dirname, "../")],
]
expect(ws.findUp("blablabla")).toBeUndefined()
expect(findUp("blablabla")).toBeUndefined()
tests.forEach(([cwd, name, result]) => {
expect(ws.findUp(name, cwd)).toBe(result)
expect(findUp(name, cwd)).toBe(result)
})
})

test("lerna", async () => {
const packages = (await ws.getLernaWorkspace("__tests__/workspace/apps"))
?.packages
const packages = (
await ws.getWorkspace({
cwd: "__tests__/workspace/apps",
type: WorkspaceProviderType.lerna,
})
)?.getPackages()
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p =>
Expand All @@ -34,8 +40,12 @@ test("lerna", async () => {
})

test("yarn", async () => {
const packages = (await ws.getYarnWorkspace("__tests__/workspace/apps/app1"))
?.packages
const packages = (
await ws.getWorkspace({
cwd: "__tests__/workspace/apps/app1",
type: WorkspaceProviderType.yarn,
})
)?.getPackages()
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p =>
Expand All @@ -53,8 +63,12 @@ test("yarn", async () => {
})

test("pnpm", async () => {
const packages = (await ws.getPnpmWorkspace("__tests__/workspace/apps/app1"))
?.packages
const packages = (
await ws.getWorkspace({
cwd: "__tests__/workspace/apps/app1",
type: WorkspaceProviderType.pnpm,
})
)?.getPackages()
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p =>
Expand All @@ -72,8 +86,12 @@ test("pnpm", async () => {
})

test("recursive", async () => {
const packages = (await ws.getRecursiveWorkspace("__tests__/workspace"))
?.packages
const packages = (
await ws.getWorkspace({
cwd: "__tests__/workspace",
type: WorkspaceProviderType.recursive,
})
)?.getPackages()
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p =>
Expand All @@ -92,8 +110,8 @@ test("recursive", async () => {

test("workspace", async () => {
const packages = (
await ws.getWorkspace("__tests__/workspace/apps/app1", true)
)?.packages
await ws.getWorkspace({ cwd: "__tests__/workspace/apps/app1" })
)?.getPackages()
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p =>
Expand Down

0 comments on commit 23fd06b

Please sign in to comment.