Repository navigation
Expand file tree
/
Copy pathpullRequestFileOrder.logic.test.ts
More file actions
141 lines (127 loc) · 4.19 KB
/
Copy pathpullRequestFileOrder.logic.test.ts
File metadata and controls
141 lines (127 loc) · 4.19 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { FileDiffMetadata } from "@pierre/diffs";
import { describe, expect, it } from "vite-plus/test";
import { orderDiffFiles } from "./pullRequestFileOrder.logic";
function file(name: string, additionLines: ReadonlyArray<string> = []): FileDiffMetadata {
return { name, hunks: [], additionLines, deletionLines: [] } as unknown as FileDiffMetadata;
}
function order(files: ReadonlyArray<FileDiffMetadata>): Array<string> {
return orderDiffFiles(files).map((entry) => entry.name);
}
describe("orderDiffFiles", () => {
it("places source before tests and generated files across path conventions", () => {
const source = ["src/app.ts", "src/dist.ts", "src/testing.ts"];
const tests = [
"src/__tests__/app.ts",
"src/app.spec.tsx",
"src/app.test.ts",
"test/app.ts",
"tests/helpers/app.ts",
];
const generated = [
"apps/web/package-lock.json",
"dist/app.js",
"packages/core/vendor/lib.js",
"pnpm-lock.yaml",
"public/app.min.js",
"src/__snapshots__/app.ts",
"src/api.generated.ts",
"src/app.test.ts.snap",
];
expect(
order([...generated, ...tests, ...source].toReversed().map((path) => file(path))),
).toEqual([...source, ...tests, ...generated]);
});
it("resolves a specifier that climbs out of the importer's directory", () => {
expect(
order([file("src/ui/a.ts", ['import { b } from "../lib/b";']), file("src/lib/b.ts")]),
).toEqual(["src/lib/b.ts", "src/ui/a.ts"]);
});
it("resolves a directory specifier to that directory's index file", () => {
expect(
order([file("src/a.ts", ['import { b } from "./lib";']), file("src/lib/index.ts")]),
).toEqual(["src/lib/index.ts", "src/a.ts"]);
});
it("falls back to the specifier's last segment when the path does not line up", () => {
expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
"lib/b.ts",
"src/a.ts",
]);
});
it("ignores an ambiguous last segment rather than guessing", () => {
expect(
order([
file("src/a.ts", ['import { b } from "~/somewhere/b";']),
file("one/b.ts"),
file("two/b.ts"),
]),
).toEqual(["one/b.ts", "src/a.ts", "two/b.ts"]);
});
it("resolves a specifier naming an extension, including the .js beside a .ts", () => {
expect(order([file("src/a.ts", ['import { b } from "./b.js";']), file("src/b.ts")])).toEqual([
"src/b.ts",
"src/a.ts",
]);
});
it("reads require and bare imports too", () => {
expect(
order([
file("src/a.ts", ['const b = require("./b");', 'import "./c";']),
file("src/b.ts"),
file("src/c.ts"),
]),
).toEqual(["src/b.ts", "src/c.ts", "src/a.ts"]);
});
it("falls back to path order inside a cycle", () => {
expect(
order([
file("src/b.ts", ['import { a } from "./a";']),
file("src/a.ts", ['import { b } from "./b";']),
]),
).toEqual(["src/a.ts", "src/b.ts"]);
});
it("keeps the tiers apart and orders tests by the source they cover", () => {
expect(
order([
file("pnpm-lock.yaml"),
file("src/a.test.ts"),
file("src/__tests__/b.ts"),
file("src/a.ts", ['import { b } from "./b";']),
file("src/b.ts"),
file("dist/a.js"),
]),
).toEqual([
"src/b.ts",
"src/a.ts",
"src/__tests__/b.ts",
"src/a.test.ts",
"dist/a.js",
"pnpm-lock.yaml",
]);
});
it("puts a test with no source in the diff after the ones that have theirs", () => {
expect(order([file("src/orphan.spec.ts"), file("src/a.test.ts"), file("src/a.ts")])).toEqual([
"src/a.ts",
"src/a.test.ts",
"src/orphan.spec.ts",
]);
});
it("pairs a test with the source whose name carries its own dotted suffix", () => {
expect(
order([
file("src/c.browser.test.tsx"),
file("src/b.test.ts"),
file("src/a.logic.test.ts"),
file("src/c.tsx"),
file("src/b.ts"),
file("src/a.logic.ts"),
]),
).toEqual([
"src/a.logic.ts",
"src/b.ts",
"src/c.tsx",
"src/a.logic.test.ts",
"src/b.test.ts",
"src/c.browser.test.tsx",
]);
});
});