tsconfig-loader-tests.ts
5.63 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { assert } from "chai";
import {
loadTsconfig,
tsConfigLoader,
walkForTsConfig
} from "../src/tsconfig-loader";
import { join } from "path";
describe("tsconfig-loader", () => {
it("should find tsconfig in cwd", () => {
const result = tsConfigLoader({
cwd: "/foo/bar",
getEnv: (_: string) => undefined,
loadSync: (cwd: string) => {
return {
tsConfigPath: `${cwd}/tsconfig.json`,
baseUrl: "./",
paths: {}
};
}
});
assert.equal(result.tsConfigPath, "/foo/bar/tsconfig.json");
});
it("should return loaderResult.tsConfigPath as undefined when not found", () => {
const result = tsConfigLoader({
cwd: "/foo/bar",
getEnv: (_: string) => undefined,
loadSync: (_: string) => {
return {
tsConfigPath: undefined,
baseUrl: "./",
paths: {}
};
}
});
assert.isUndefined(result.tsConfigPath);
});
it("should use TS_NODE_PROJECT env if exists", () => {
const result = tsConfigLoader({
cwd: "/foo/bar",
getEnv: (key: string) =>
key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined,
loadSync: (cwd: string, fileName: string) => {
if (cwd === "/foo/bar" && fileName === "/foo/baz") {
return {
tsConfigPath: "/foo/baz/tsconfig.json",
baseUrl: "./",
paths: {}
};
}
return {
tsConfigPath: undefined,
baseUrl: "./",
paths: {}
};
}
});
assert.equal(result.tsConfigPath, "/foo/baz/tsconfig.json");
});
});
describe("walkForTsConfig", () => {
it("should find tsconfig in starting directory", () => {
const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
path => path === pathToTsconfig
);
assert.equal(res, pathToTsconfig);
});
it("should find tsconfig in parent directory", () => {
const pathToTsconfig = join("/root", "tsconfig.json");
const res = walkForTsConfig(
join("/root", "dir1"),
path => path === pathToTsconfig
);
assert.equal(res, pathToTsconfig);
});
it("should return undefined when reaching the top", () => {
const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => false);
assert.equal(res, undefined);
});
});
describe("loadConfig", () => {
it("It should load a config", () => {
const config = { compilerOptions: { baseUrl: "hej" } };
const res = loadTsconfig(
"/root/dir1/tsconfig.json",
path => path === "/root/dir1/tsconfig.json",
_ => JSON.stringify(config)
);
assert.deepEqual(res, config);
});
it("It should load a config with comments", () => {
const config = { compilerOptions: { baseUrl: "hej" } };
const res = loadTsconfig(
"/root/dir1/tsconfig.json",
path => path === "/root/dir1/tsconfig.json",
_ => `{
// my comment
"compilerOptions": {
"baseUrl": "hej"
}
}`
);
assert.deepEqual(res, config);
});
it("It should load a config with trailing commas", () => {
const config = { compilerOptions: { baseUrl: "hej" } };
const res = loadTsconfig(
"/root/dir1/tsconfig.json",
path => path === "/root/dir1/tsconfig.json",
_ => `{
"compilerOptions": {
"baseUrl": "hej",
},
}`
);
assert.deepEqual(res, config);
});
it("It should load a config with extends and overwrite all options", () => {
const firstConfig = {
extends: "../base-config.json",
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }
};
const firstConfigPath = join("/root", "dir1", "tsconfig.json");
const baseConfig = {
compilerOptions: {
baseUrl: "olle",
paths: { foo: ["bar1"] },
strict: true
}
};
const baseConfigPath = join("/root", "base-config.json");
const res = loadTsconfig(
join("/root", "dir1", "tsconfig.json"),
path => path === firstConfigPath || path === baseConfigPath,
path => {
if (path === firstConfigPath) {
return JSON.stringify(firstConfig);
}
if (path === baseConfigPath) {
return JSON.stringify(baseConfig);
}
return "";
}
);
assert.deepEqual(res, {
extends: "../base-config.json",
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true
}
});
});
it("Should use baseUrl relative to location of extended tsconfig", () => {
const firstConfig = { compilerOptions: { baseUrl: "." } };
const firstConfigPath = join("/root", "first-config.json");
const secondConfig = { extends: "../first-config.json" };
const secondConfigPath = join("/root", "dir1", "second-config.json");
const thirdConfig = { extends: "../second-config.json" };
const thirdConfigPath = join("/root", "dir1", "dir2", "third-config.json");
const res = loadTsconfig(
join("/root", "dir1", "dir2", "third-config.json"),
path =>
path === firstConfigPath ||
path === secondConfigPath ||
path === thirdConfigPath,
path => {
if (path === firstConfigPath) {
return JSON.stringify(firstConfig);
}
if (path === secondConfigPath) {
return JSON.stringify(secondConfig);
}
if (path === thirdConfigPath) {
return JSON.stringify(thirdConfig);
}
return "";
}
);
assert.deepEqual(res, {
extends: "../second-config.json",
compilerOptions: { baseUrl: join("..", "..") }
});
});
});