-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
126 lines (103 loc) · 3.84 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import initFixture from "./testutils/readFixture"
import findWorkspacePackages from "./src/findWorkspacePackages"
import buildPackageGraph from "./src/buildPackageGraph"
import findCycles from "./src/findCycles"
describe("findWorkspacePackages", () => {
it("finds workspace packages when explicitly declared", async () => {
initFixture("default")
const workspaces = await findWorkspacePackages()
expect(workspaces).toStrictEqual(["example1/package.json", "example2/package.json"])
})
it("finds workspace packages declared with *", async () => {
initFixture("star")
const workspaces = await findWorkspacePackages()
expect(workspaces).toStrictEqual([
"packages/example1/package.json",
"packages/example2/package.json",
])
})
it("finds workspace packages declared with **", async () => {
initFixture("doublestar")
const workspaces = await findWorkspacePackages()
expect(workspaces.sort()).toStrictEqual(
[
"packages/backend/one/package.json",
"packages/backend/two/package.json",
"packages/frontend/one/package.json",
"packages/frontend/two/package.json",
].sort()
)
})
it("excludes node_modules", async () => {
initFixture("exclude-node-modules")
const workspaces = await findWorkspacePackages()
expect(workspaces.sort()).toStrictEqual(
[
"packages/backend/one/package.json",
"packages/backend/two/package.json",
"packages/frontend/one/package.json",
"packages/frontend/two/package.json",
].sort()
)
})
it("throws error if workspace definition is missing", async () => {
initFixture("missing-workspace")
await expect(findWorkspacePackages()).rejects.toEqual(Error("Missing workspace definition"))
})
it("throws error if no package", async () => {
initFixture("missing-root-package")
await expect(findWorkspacePackages()).rejects.toEqual(
Error("Missing package.json in working directory")
)
})
})
describe("buildPackageGraph", () => {
it("returns adjacency list of workspace packages", async () => {
initFixture("default")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
expect(graph).toEqual({ example1: ["example2"], example2: [] })
})
})
describe("findCycles", () => {
it("returns none if no cycle", async () => {
initFixture("default")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
const cycles = await findCycles(graph)
expect(cycles).toStrictEqual([])
})
it("finds a direct cycle", async () => {
initFixture("cycle")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
const cycles = await findCycles(graph)
expect(cycles).toStrictEqual([["example1", "example2", "example1"]])
})
it("finds larger cycle", async () => {
initFixture("cycle-larger")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
const cycles = await findCycles(graph)
expect(cycles).toStrictEqual([
["eight", "nine", "one", "two", "three", "four", "five", "six", "seven", "eight"],
])
})
it("finds small cycle in larger project", async () => {
initFixture("cycle-subset")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
const cycles = await findCycles(graph)
expect(cycles).toStrictEqual([["d", "e", "f", "d"]])
})
it("finds multiple cycles", async () => {
initFixture("cycle-multi")
const workspaces = await findWorkspacePackages()
const graph = await buildPackageGraph(workspaces)
const cycles = await findCycles(graph)
expect(cycles).toStrictEqual([
["a", "b", "c", "a"],
["d", "e", "f", "g", "d"],
])
})
})