Skip to content

Commit

Permalink
test: 🚨 updated jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 2, 2020
1 parent 79a9dcd commit 084ef92
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 34 deletions.
2 changes: 1 addition & 1 deletion __tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ test("false", async () => {

test("no params", async () => {
await run(["", ""])
stubs.log.should.be.calledWithMatch("Available Scripts")
stubs.log.should.be.calledWithMatch("See --list for available scripts")
stubs.exit.should.be.calledWith(1)
})
29 changes: 29 additions & 0 deletions __tests__/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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)
expect(workspace).toBeDefined()
if (workspace) {
filter(workspace, "apps/*")
const dirs = workspace.packages.map(p =>
path.relative("__tests__/workspace", p.root)
)
dirs.sort()
expect(dirs).toStrictEqual(["apps/app1", "apps/app2"])
}
})

test("filter pkg", async () => {
const workspace = await ws.getWorkspace("__tests__/workspace/apps/app1", true)
expect(workspace).toBeDefined()
if (workspace) {
filter(workspace, "@scoped/*")
const dirs = workspace.packages.map(p =>
path.relative("__tests__/workspace", p.root)
)
dirs.sort()
expect(dirs).toStrictEqual(["libs/lib3"])
}
})
15 changes: 15 additions & 0 deletions __tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ test("test bin ", () => {
expect(cmd.debug()).toStrictEqual("bin:jest")
})

test("yarn bin", () => {
const parser = new CommandParser({ 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 cmd = parser.parse("foo")
expect(cmd.debug()).toStrictEqual({
"script:foo": "bin:jest",
})
})

test("test npx bin ", () => {
const parser = new CommandParser({ scripts: {} })
const cmd = parser.parse("npx jest")
Expand Down
31 changes: 14 additions & 17 deletions __tests__/runner-spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ afterAll(() => {
})

test("failed command", async () => {
const runner = new Runner({ scripts: { test: "fail" } }, { fancy: true })
const runner = new Runner({ fancy: true })
const spawn = sinon.stub(runner, "spawn").rejects()
await runner.run("test")
await runner.run("test", { scripts: { test: "fail" } })
spawn.should.be.calledWith("fail", [])
stubs.exit.should.be.calledWith(1)
})
Expand All @@ -40,41 +40,38 @@ test("formatDuration", () => {
})

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

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

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

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

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

test("--no-fancy", async () => {
const runner = new Runner({}, { fancy: false })
await runner.run("echo foobar")
const runner = new Runner({ fancy: false })
await runner.run("echo foobar", {})
stubs.log.should.be.calledWithMatch("foobar")
})
32 changes: 16 additions & 16 deletions __tests__/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sinon from "sinon"
import chai from "chai"
import sinonChai from "sinon-chai"
import chalk from "chalk"
import { PackageScripts } from "../src/parser"
import { PackageJson } from "../src/workspace"
import * as path from "path"

chai.use(sinonChai)
Expand All @@ -28,7 +28,7 @@ afterAll(() => {
chalk.level = chalkLevel
})

const advancedPackage: PackageScripts = {
const advancedPackage: PackageJson = {
scripts: {
prebuild: "yarn clean && yarn lint && yarn test",
"build:rollup": "npx rollup -c",
Expand All @@ -44,18 +44,18 @@ const advancedPackage: PackageScripts = {

test("constructor", () => {
chai.expect(new Runner({}).options).to.be.empty
chai.expect(new Runner({}, { parallel: true }).options.concurrent).to.be.true
chai.expect(new Runner({ parallel: true }).options.concurrent).to.be.true
})

test("advanced build --dry-run", async () => {
const runner = new Runner(advancedPackage, { dryRun: true })
await runner.run("build")
const runner = new Runner({ dryRun: true })
await runner.run("build", advancedPackage)
chai.expect(stubs.spawn).not.to.be.called
})

test("advanced build --no-fancy", async () => {
const runner = new Runner(advancedPackage)
await runner.run("build")
const runner = new Runner()
await runner.run("build", advancedPackage)

stubs.log.should.be.calledWith("❯ lint")
stubs.log.should.be.calledWith("❯ prebuild")
Expand Down Expand Up @@ -101,8 +101,8 @@ test("advanced build --no-fancy", async () => {
})

test("advanced build --fancy", async () => {
const runner = new Runner(advancedPackage, { fancy: true })
await runner.run("build")
const runner = new Runner({ fancy: true })
await runner.run("build", advancedPackage)

stubs.write.should.be.calledWithMatch("✔")

Expand Down Expand Up @@ -146,8 +146,8 @@ test("advanced build --fancy", async () => {
})

test("advanced build --raw", async () => {
const runner = new Runner(advancedPackage, { raw: true })
await runner.run("build")
const runner = new Runner({ raw: true })
await runner.run("build", advancedPackage)

stubs.spawn.should.be.callCount(6)
stubs.spawn
Expand Down Expand Up @@ -189,11 +189,11 @@ test("advanced build --raw", async () => {
})

test("concurrent ", async () => {
const runner = new Runner(
{ scripts: { test: "jest" }, ultra: { concurrent: ["test"] } },
{}
)
await runner.run("test")
const runner = new Runner({})
await runner.run("test", {
scripts: { test: "jest" },
ultra: { concurrent: ["test"] },
})
stubs.spawn.should.be.calledOnce
stubs.spawn
.getCall(0)
Expand Down
101 changes: 101 additions & 0 deletions __tests__/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as ws from "../src/workspace"
import path from "path"

test("findUp ", () => {
const tests = [
["./foo/fa/boo", ".git", path.resolve(__dirname, "../")],
[".", ".git", path.resolve(__dirname, "../")],
[".", "node_modules", path.resolve(__dirname, "../")],
[".", "package.json", path.resolve(__dirname, "../")],
]
expect(ws.findUp("blablabla")).toBeUndefined()
tests.forEach(([cwd, name, result]) => {
expect(ws.findUp(name, cwd)).toBe(result)
})
})

test("lerna", async () => {
const packages = (await ws.getLernaWorkspace("__tests__/workspace/apps"))
?.packages
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p => path.relative(".", p.root))
dirs.sort()
expect(dirs).toStrictEqual([
"__tests__/workspace/apps/app1",
"__tests__/workspace/apps/app2",
"__tests__/workspace/libs/lib1",
"__tests__/workspace/libs/lib2",
"__tests__/workspace/libs/lib3",
])
}
})

test("yarn", async () => {
const packages = (await ws.getYarnWorkspace("__tests__/workspace/apps/app1"))
?.packages
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p => path.relative(".", p.root))
dirs.sort()
expect(dirs).toStrictEqual([
"__tests__/workspace/apps/app1",
"__tests__/workspace/apps/app2",
"__tests__/workspace/libs/lib1",
"__tests__/workspace/libs/lib2",
"__tests__/workspace/libs/lib3",
])
}
})

test("pnpm", async () => {
const packages = (await ws.getPnpmWorkspace("__tests__/workspace/apps/app1"))
?.packages
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p => path.relative(".", p.root))
dirs.sort()
expect(dirs).toStrictEqual([
"__tests__/workspace/apps/app1",
"__tests__/workspace/apps/app2",
"__tests__/workspace/libs/lib1",
"__tests__/workspace/libs/lib2",
"__tests__/workspace/libs/lib3",
])
}
})

test("recursive", async () => {
const packages = (await ws.getRecursiveWorkspace("__tests__/workspace"))
?.packages
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p => path.relative(".", p.root))
dirs.sort()
expect(dirs).toStrictEqual([
"__tests__/workspace/apps/app1",
"__tests__/workspace/apps/app2",
"__tests__/workspace/libs/lib1",
"__tests__/workspace/libs/lib2",
"__tests__/workspace/libs/lib3",
])
}
})

test("workspace", async () => {
const packages = (
await ws.getWorkspace("__tests__/workspace/apps/app1", true)
)?.packages
expect(packages).toBeDefined()
if (packages) {
const dirs = packages.map(p => path.relative(".", p.root))
dirs.sort()
expect(dirs).toStrictEqual([
"__tests__/workspace/apps/app1",
"__tests__/workspace/apps/app2",
"__tests__/workspace/libs/lib1",
"__tests__/workspace/libs/lib2",
"__tests__/workspace/libs/lib3",
])
}
})

0 comments on commit 084ef92

Please sign in to comment.