tsconfig-loader.test.js
10.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tsconfig_loader_1 = require("../tsconfig-loader");
var path_1 = require("path");
describe("tsconfig-loader", function () {
it("should find tsconfig in cwd", function () {
var result = (0, tsconfig_loader_1.tsConfigLoader)({
cwd: "/foo/bar",
getEnv: function (_) { return undefined; },
loadSync: function (cwd) {
return {
tsConfigPath: "".concat(cwd, "/tsconfig.json"),
baseUrl: "./",
paths: {},
};
},
});
// assert.equal(result.tsConfigPath, "/foo/bar/tsconfig.json");
expect(result.tsConfigPath).toBe("/foo/bar/tsconfig.json");
});
it("should return loaderResult.tsConfigPath as undefined when not found", function () {
var result = (0, tsconfig_loader_1.tsConfigLoader)({
cwd: "/foo/bar",
getEnv: function (_) { return undefined; },
loadSync: function (_) {
return {
tsConfigPath: undefined,
baseUrl: "./",
paths: {},
};
},
});
// assert.isUndefined(result.tsConfigPath);
expect(result.tsConfigPath).toBeUndefined();
});
it("should use TS_NODE_PROJECT env if exists", function () {
var result = (0, tsconfig_loader_1.tsConfigLoader)({
cwd: "/foo/bar",
getEnv: function (key) {
return key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined;
},
loadSync: function (cwd, fileName) {
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");
expect(result.tsConfigPath).toBe("/foo/baz/tsconfig.json");
});
it("should use TS_NODE_BASEURL env if exists", function () {
var result = (0, tsconfig_loader_1.tsConfigLoader)({
cwd: "/foo/bar",
getEnv: function (key) {
return key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined;
},
loadSync: function (_0, _1, baseUrl) {
return {
tsConfigPath: undefined,
baseUrl: baseUrl,
paths: {},
};
},
});
// assert.equal(result.baseUrl, "SOME_BASEURL");
expect(result.baseUrl).toBe("SOME_BASEURL");
});
it("should not use TS_NODE_BASEURL env if it does not exist", function () {
var result = (0, tsconfig_loader_1.tsConfigLoader)({
cwd: "/foo/bar",
getEnv: function (_) {
return undefined;
},
loadSync: function (_0, _1, baseUrl) {
return {
tsConfigPath: undefined,
baseUrl: baseUrl,
paths: {},
};
},
});
// assert.equal(result.baseUrl, undefined);
expect(result.baseUrl).toBeUndefined();
});
});
describe("walkForTsConfig", function () {
it("should find tsconfig in starting directory", function () {
var pathToTsconfig = (0, path_1.join)("/root", "dir1", "tsconfig.json");
var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return path === pathToTsconfig; });
// assert.equal(res, pathToTsconfig);
expect(res).toBe(pathToTsconfig);
});
it("should find tsconfig in parent directory", function () {
var pathToTsconfig = (0, path_1.join)("/root", "tsconfig.json");
var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return path === pathToTsconfig; });
// assert.equal(res, pathToTsconfig);
expect(res).toBe(pathToTsconfig);
});
it("should return undefined when reaching the top", function () {
var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1", "kalle"), function () { return false; });
// assert.equal(res, undefined);
expect(res).toBeUndefined();
});
});
describe("loadConfig", function () {
it("It should load a config", function () {
var config = { compilerOptions: { baseUrl: "hej" } };
var res = (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return JSON.stringify(config); });
// assert.deepEqual(res, config);
expect(res).toStrictEqual(config);
});
it("It should load a config with comments", function () {
var config = { compilerOptions: { baseUrl: "hej" } };
var res = (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return "{\n // my comment\n \"compilerOptions\": { \n \"baseUrl\": \"hej\"\n }\n }"; });
// assert.deepEqual(res, config);
expect(res).toStrictEqual(config);
});
it("It should load a config with trailing commas", function () {
var config = { compilerOptions: { baseUrl: "hej" } };
var res = (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return "{\n \"compilerOptions\": { \n \"baseUrl\": \"hej\",\n },\n }"; });
// assert.deepEqual(res, config);
expect(res).toStrictEqual(config);
});
it("It should load a config with extends and overwrite all options", function () {
var firstConfig = {
extends: "../base-config.json",
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
};
var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
var baseConfig = {
compilerOptions: {
baseUrl: "olle",
paths: { foo: ["bar1"] },
strict: true,
},
};
var baseConfigPath = (0, path_1.join)("/root", "base-config.json");
var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (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,
// },
// });
expect(res).toEqual({
extends: "../base-config.json",
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true,
},
});
});
it("It should load a config with extends from node_modules and overwrite all options", function () {
var firstConfig = {
extends: "my-package/base-config.json",
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
};
var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
var baseConfig = {
compilerOptions: {
baseUrl: "olle",
paths: { foo: ["bar1"] },
strict: true,
},
};
var baseConfigPath = (0, path_1.join)("/root", "dir1", "node_modules", "my-package", "base-config.json");
var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
if (path === firstConfigPath) {
return JSON.stringify(firstConfig);
}
if (path === baseConfigPath) {
return JSON.stringify(baseConfig);
}
return "";
});
// assert.deepEqual(res, {
// extends: "my-package/base-config.json",
// compilerOptions: {
// baseUrl: "kalle",
// paths: { foo: ["bar2"] },
// strict: true,
// },
// });
expect(res).toEqual({
extends: "my-package/base-config.json",
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true,
},
});
});
it("Should use baseUrl relative to location of extended tsconfig", function () {
var firstConfig = { compilerOptions: { baseUrl: "." } };
var firstConfigPath = (0, path_1.join)("/root", "first-config.json");
var secondConfig = { extends: "../first-config.json" };
var secondConfigPath = (0, path_1.join)("/root", "dir1", "second-config.json");
var thirdConfig = { extends: "../second-config.json" };
var thirdConfigPath = (0, path_1.join)("/root", "dir1", "dir2", "third-config.json");
var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "dir2", "third-config.json"), function (path) {
return path === firstConfigPath ||
path === secondConfigPath ||
path === thirdConfigPath;
}, function (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("..", "..") },
// });
expect(res).toEqual({
extends: "../second-config.json",
compilerOptions: { baseUrl: (0, path_1.join)("..", "..") },
});
});
});
//# sourceMappingURL=tsconfig-loader.test.js.map