generated from AnandChowdhary/node.ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.ts
105 lines (92 loc) 路 3.54 KB
/
index.spec.ts
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
import { eject } from "./index";
import { pathExists, readFile, writeFile, remove, ensureDir } from "fs-extra";
let originalIndexTS: string;
let originalIndexSpecTS: string;
describe("eject-dependencies", () => {
beforeAll(async () => {
originalIndexTS = await readFile("./index.ts", "utf8");
originalIndexSpecTS = await readFile("./index.spec.ts", "utf8");
await ensureDir("./ejected");
await remove("./ejected");
});
it("copies a dependency", async () => {
await eject();
expect(await pathExists("./ejected/fs-extra")).toBeTruthy();
});
it("throws if node_modules is not found", () => {
eject({ sourceDir: "/404" })
.then(() => {})
.catch(error => expect(error).toBeDefined());
});
it("returns updated files", async () => {
const result = await eject();
expect(result.updatedFiles).toEqual(new Set().add("index.ts"));
});
it("returns updated dependencies", async () => {
const result = await eject();
expect(result.updatedDependencies).toEqual(
new Set().add("fast-glob").add("fs-extra")
);
});
it("does not update file without dependencies", async () => {
await writeFile("./no-dependencies.js", "//");
const result = await eject();
expect(result.updatedDependencies.has("no-dependencies.js")).toBeFalsy();
});
it("updates with destination dir", async () => {
await writeFile("./dest-dir.ts", `import { readFile } from "fs-extra"`);
await eject({ destDir: "path/to/ejected" });
const destDirFile = await readFile("./dest-dir.ts", "utf8");
expect(destDirFile.includes(`from "./path/to/ejected`)).toBeTruthy();
});
it("updates specfile", async () => {
await eject({ updateTestFiles: true });
const indexSpecFile = await readFile("./index.spec.ts", "utf8");
expect(indexSpecFile.includes(`from "./ejected`)).toBeTruthy();
});
it("updates nested file", async () => {
await ensureDir("./n/e/s/t/e");
await writeFile("./n/e/s/t/e/d.ts", `import { readFile } from "fs-extra"`);
await eject({ codeFiles: ["n/e/s/**/*.ts"] });
const destDirFile = await readFile("./n/e/s/t/e/d.ts", "utf8");
expect(destDirFile.includes(`from "../../../../../`)).toBeTruthy();
});
it("updates nested file with destination dir", async () => {
await ensureDir("./n/e/s/t/e");
await writeFile("./n/e/s/t/e/dest.ts", `import fg from "fast-glob"`);
await eject({ destDir: "path/to/", codeFiles: ["n/e/s/t/e/dest.ts"] });
const destDirFile = await readFile("./n/e/s/t/e/dest.ts", "utf8");
console.log(destDirFile);
expect(destDirFile.includes(`from "../`)).toBeTruthy();
});
it("updates with dependencies filter", async () => {
await writeFile(
"./filter.ts",
`import { readFile } from "fs-extra";
import fg from "fast-glob";`
);
await eject({
codeFiles: ["./filter.ts"],
dependenciesFilter: files => {
files.delete("fast-glob");
return files;
}
});
const filterFile = await readFile("./filter.ts", "utf8");
expect(
filterFile.includes(`import { readFile } from "./ejected/fs-extra"`)
).toBeTruthy();
expect(filterFile.includes(`import fg from "fast-glob"`)).toBeTruthy();
});
afterAll(async () => {
await writeFile("./index.ts", originalIndexTS);
await writeFile("./index.spec.ts", originalIndexSpecTS);
await ensureDir("./ejected");
await remove("./ejected");
await remove("./path/to");
await remove("./no-dependencies.js");
await remove("./dest-dir.ts");
await remove("./n");
await remove("./filter.ts");
});
});