-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtsconfig.spec.ts
More file actions
115 lines (95 loc) · 4.24 KB
/
tsconfig.spec.ts
File metadata and controls
115 lines (95 loc) · 4.24 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
import * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import { locateConfigFile, parseConfigFileWithSystem } from "../../src/cli/tsconfig";
import { normalizeSlashes } from "../../src/utils";
let temp: string;
beforeEach(async () => {
temp = await fs.mkdtemp(path.join(os.tmpdir(), "tstl-test-"));
process.chdir(temp);
});
const originalWorkingDirectory = process.cwd();
afterEach(async () => {
process.chdir(originalWorkingDirectory);
// TODO [node@12]: `rmdir` has `recursive` option
await fs.remove(temp);
});
const locate = (project: string | undefined, fileNames: string[] = []) =>
locateConfigFile({ errors: [], fileNames, options: { project } });
const normalize = (name: string) => normalizeSlashes(fs.realpathSync(path.resolve(temp, name)));
describe("specified", () => {
for (const separator of process.platform === "win32" ? ["/", "\\"] : ["/"]) {
for (const pointsTo of ["file", "directory"] as const) {
const findAndExpect = (project: string, expected: string) => {
project = project.replace(/[\\/]/g, separator);
if (pointsTo === "directory") {
project = path.dirname(project);
}
expect(locate(project)).toBe(normalize(expected));
};
test(`relative to ${pointsTo} separated with '${separator}'`, async () => {
await fs.outputFile("tsconfig.json", "");
await fs.mkdir("src");
process.chdir("src");
findAndExpect("../tsconfig.json", "tsconfig.json");
});
test(`absolute to ${pointsTo} separated with '${separator}'`, async () => {
await fs.outputFile("tsconfig.json", "");
findAndExpect(path.resolve("tsconfig.json"), "tsconfig.json");
});
}
}
test.each(["", ".", "./"])("current directory (%p)", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(".")).toBe(normalize("tsconfig.json"));
});
});
describe("inferred", () => {
test("in current directory", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
});
test("in parent directory", async () => {
await fs.outputFile("tsconfig.json", "");
await fs.mkdir("src");
process.chdir("src");
expect(locate(undefined)).toBe(normalize("tsconfig.json"));
});
test("not found", () => {
expect(locate(undefined)).toBeUndefined();
});
test("does not attempt when has files", async () => {
await fs.outputFile("tsconfig.json", "");
expect(locate(undefined, [""])).toBeUndefined();
});
});
describe("errors", () => {
test("specified file does not exist", () => {
expect([locate("tsconfig.json")]).toHaveDiagnostics();
});
test("specified directory does not exist", () => {
expect([locate("project")]).toHaveDiagnostics();
});
test("cannot be mixed", async () => {
await fs.outputFile("tsconfig.json", "");
expect([locate("tsconfig.json", [""])]).toHaveDiagnostics();
});
});
describe("tsconfig extends", () => {
test("correctly merges extended tsconfig files", () => {
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.json"));
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.3", noHeader: true });
});
test("can handle multiple extends", () => {
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.multi-extends.json"));
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.4", sourceMapTraceback: true });
});
test("can handle cycles in configs", () => {
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig-cycle1.json"));
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.4" });
});
test("can handle tsconfig files with comments", () => {
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.with-comments.json"));
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.3" });
});
});