source: frontend/node_modules/tsconfig-paths/lib/__tests__/tsconfig-loader.test.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 14.2 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tsconfig_loader_1 = require("../tsconfig-loader");
4var path_1 = require("path");
5describe("tsconfig-loader", function () {
6 it("should find tsconfig in cwd", function () {
7 var result = (0, tsconfig_loader_1.tsConfigLoader)({
8 cwd: "/foo/bar",
9 getEnv: function (_) { return undefined; },
10 loadSync: function (cwd) {
11 return {
12 tsConfigPath: "".concat(cwd, "/tsconfig.json"),
13 baseUrl: "./",
14 paths: {},
15 };
16 },
17 });
18 // assert.equal(result.tsConfigPath, "/foo/bar/tsconfig.json");
19 expect(result.tsConfigPath).toBe("/foo/bar/tsconfig.json");
20 });
21 it("should return loaderResult.tsConfigPath as undefined when not found", function () {
22 var result = (0, tsconfig_loader_1.tsConfigLoader)({
23 cwd: "/foo/bar",
24 getEnv: function (_) { return undefined; },
25 loadSync: function (_) {
26 return {
27 tsConfigPath: undefined,
28 baseUrl: "./",
29 paths: {},
30 };
31 },
32 });
33 // assert.isUndefined(result.tsConfigPath);
34 expect(result.tsConfigPath).toBeUndefined();
35 });
36 it("should use TS_NODE_PROJECT env if exists", function () {
37 var result = (0, tsconfig_loader_1.tsConfigLoader)({
38 cwd: "/foo/bar",
39 getEnv: function (key) {
40 return key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined;
41 },
42 loadSync: function (cwd, fileName) {
43 if (cwd === "/foo/bar" && fileName === "/foo/baz") {
44 return {
45 tsConfigPath: "/foo/baz/tsconfig.json",
46 baseUrl: "./",
47 paths: {},
48 };
49 }
50 return {
51 tsConfigPath: undefined,
52 baseUrl: "./",
53 paths: {},
54 };
55 },
56 });
57 // assert.equal(result.tsConfigPath, "/foo/baz/tsconfig.json");
58 expect(result.tsConfigPath).toBe("/foo/baz/tsconfig.json");
59 });
60 it("should use TS_NODE_BASEURL env if exists", function () {
61 var result = (0, tsconfig_loader_1.tsConfigLoader)({
62 cwd: "/foo/bar",
63 getEnv: function (key) {
64 return key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined;
65 },
66 loadSync: function (_0, _1, baseUrl) {
67 return {
68 tsConfigPath: undefined,
69 baseUrl: baseUrl,
70 paths: {},
71 };
72 },
73 });
74 // assert.equal(result.baseUrl, "SOME_BASEURL");
75 expect(result.baseUrl).toBe("SOME_BASEURL");
76 });
77 it("should not use TS_NODE_BASEURL env if it does not exist", function () {
78 var result = (0, tsconfig_loader_1.tsConfigLoader)({
79 cwd: "/foo/bar",
80 getEnv: function (_) {
81 return undefined;
82 },
83 loadSync: function (_0, _1, baseUrl) {
84 return {
85 tsConfigPath: undefined,
86 baseUrl: baseUrl,
87 paths: {},
88 };
89 },
90 });
91 // assert.equal(result.baseUrl, undefined);
92 expect(result.baseUrl).toBeUndefined();
93 });
94});
95describe("walkForTsConfig", function () {
96 it("should find tsconfig in starting directory", function () {
97 var pathToTsconfig = (0, path_1.join)("/root", "dir1", "tsconfig.json");
98 var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return path === pathToTsconfig; });
99 // assert.equal(res, pathToTsconfig);
100 expect(res).toBe(pathToTsconfig);
101 });
102 it("should find tsconfig in parent directory", function () {
103 var pathToTsconfig = (0, path_1.join)("/root", "tsconfig.json");
104 var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return path === pathToTsconfig; });
105 // assert.equal(res, pathToTsconfig);
106 expect(res).toBe(pathToTsconfig);
107 });
108 it("should return undefined when reaching the top", function () {
109 var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1", "kalle"), function () { return false; });
110 // assert.equal(res, undefined);
111 expect(res).toBeUndefined();
112 });
113});
114describe("loadConfig", function () {
115 it("should load a config", function () {
116 var config = { compilerOptions: { baseUrl: "hej" } };
117 var res = (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return JSON.stringify(config); });
118 // assert.deepEqual(res, config);
119 expect(res).toStrictEqual(config);
120 });
121 it("should load a config with comments", function () {
122 var config = { compilerOptions: { baseUrl: "hej" } };
123 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 }"; });
124 // assert.deepEqual(res, config);
125 expect(res).toStrictEqual(config);
126 });
127 it("should load a config with trailing commas", function () {
128 var config = { compilerOptions: { baseUrl: "hej" } };
129 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 }"; });
130 // assert.deepEqual(res, config);
131 expect(res).toStrictEqual(config);
132 });
133 it("should throw an error including the file path when encountering invalid JSON5", function () {
134 expect(function () {
135 return (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return "{\n \"compilerOptions\": {\n }"; });
136 }).toThrowError("/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12");
137 });
138 it("should load a config with string extends and overwrite all options", function () {
139 var firstConfig = {
140 extends: "../base-config.json",
141 compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
142 };
143 var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
144 var baseConfig = {
145 compilerOptions: {
146 baseUrl: "olle",
147 paths: { foo: ["bar1"] },
148 strict: true,
149 },
150 };
151 var baseConfigPath = (0, path_1.join)("/root", "base-config.json");
152 var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
153 if (path === firstConfigPath) {
154 return JSON.stringify(firstConfig);
155 }
156 if (path === baseConfigPath) {
157 return JSON.stringify(baseConfig);
158 }
159 return "";
160 });
161 // assert.deepEqual(res, {
162 // extends: "../base-config.json",
163 // compilerOptions: {
164 // baseUrl: "kalle",
165 // paths: { foo: ["bar2"] },
166 // strict: true,
167 // },
168 // });
169 expect(res).toEqual({
170 extends: "../base-config.json",
171 compilerOptions: {
172 baseUrl: "kalle",
173 paths: { foo: ["bar2"] },
174 strict: true,
175 },
176 });
177 });
178 it("should load a config with string extends from node_modules and overwrite all options", function () {
179 var firstConfig = {
180 extends: "my-package/base-config.json",
181 compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
182 };
183 var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
184 var baseConfig = {
185 compilerOptions: {
186 baseUrl: "olle",
187 paths: { foo: ["bar1"] },
188 strict: true,
189 },
190 };
191 var baseConfigPath = (0, path_1.join)("/root", "dir1", "node_modules", "my-package", "base-config.json");
192 var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
193 if (path === firstConfigPath) {
194 return JSON.stringify(firstConfig);
195 }
196 if (path === baseConfigPath) {
197 return JSON.stringify(baseConfig);
198 }
199 return "";
200 });
201 // assert.deepEqual(res, {
202 // extends: "my-package/base-config.json",
203 // compilerOptions: {
204 // baseUrl: "kalle",
205 // paths: { foo: ["bar2"] },
206 // strict: true,
207 // },
208 // });
209 expect(res).toEqual({
210 extends: "my-package/base-config.json",
211 compilerOptions: {
212 baseUrl: "kalle",
213 paths: { foo: ["bar2"] },
214 strict: true,
215 },
216 });
217 });
218 it("should use baseUrl relative to location of extended tsconfig", function () {
219 var firstConfig = { compilerOptions: { baseUrl: "." } };
220 var firstConfigPath = (0, path_1.join)("/root", "first-config.json");
221 var secondConfig = { extends: "../first-config.json" };
222 var secondConfigPath = (0, path_1.join)("/root", "dir1", "second-config.json");
223 var thirdConfig = { extends: "../second-config.json" };
224 var thirdConfigPath = (0, path_1.join)("/root", "dir1", "dir2", "third-config.json");
225 var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "dir2", "third-config.json"), function (path) {
226 return path === firstConfigPath ||
227 path === secondConfigPath ||
228 path === thirdConfigPath;
229 }, function (path) {
230 if (path === firstConfigPath) {
231 return JSON.stringify(firstConfig);
232 }
233 if (path === secondConfigPath) {
234 return JSON.stringify(secondConfig);
235 }
236 if (path === thirdConfigPath) {
237 return JSON.stringify(thirdConfig);
238 }
239 return "";
240 });
241 // assert.deepEqual(res, {
242 // extends: "../second-config.json",
243 // compilerOptions: { baseUrl: join("..", "..") },
244 // });
245 expect(res).toEqual({
246 extends: "../second-config.json",
247 compilerOptions: { baseUrl: (0, path_1.join)("..", "..") },
248 });
249 });
250 it("should load a config with array extends and overwrite all options", function () {
251 var baseConfig1 = {
252 compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } },
253 };
254 var baseConfig1Path = (0, path_1.join)("/root", "base-config-1.json");
255 var baseConfig2 = { compilerOptions: { baseUrl: "." } };
256 var baseConfig2Path = (0, path_1.join)("/root", "dir1", "base-config-2.json");
257 var baseConfig3 = {
258 compilerOptions: { baseUrl: ".", paths: { foo: ["bar2"] } },
259 };
260 var baseConfig3Path = (0, path_1.join)("/root", "dir1", "dir2", "base-config-3.json");
261 var actualConfig = {
262 extends: [
263 "./base-config-1.json",
264 "./dir1/base-config-2.json",
265 "./dir1/dir2/base-config-3.json",
266 ],
267 };
268 var actualConfigPath = (0, path_1.join)("/root", "tsconfig.json");
269 var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "tsconfig.json"), function (path) {
270 return [
271 baseConfig1Path,
272 baseConfig2Path,
273 baseConfig3Path,
274 actualConfigPath,
275 ].indexOf(path) >= 0;
276 }, function (path) {
277 if (path === baseConfig1Path) {
278 return JSON.stringify(baseConfig1);
279 }
280 if (path === baseConfig2Path) {
281 return JSON.stringify(baseConfig2);
282 }
283 if (path === baseConfig3Path) {
284 return JSON.stringify(baseConfig3);
285 }
286 if (path === actualConfigPath) {
287 return JSON.stringify(actualConfig);
288 }
289 return "";
290 });
291 expect(res).toEqual({
292 extends: [
293 "./base-config-1.json",
294 "./dir1/base-config-2.json",
295 "./dir1/dir2/base-config-3.json",
296 ],
297 compilerOptions: {
298 baseUrl: (0, path_1.join)("dir1", "dir2"),
299 paths: { foo: ["bar2"] },
300 },
301 });
302 });
303 it("should load a config with array extends without .json extension", function () {
304 var baseConfig = {
305 compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } },
306 };
307 var baseConfigPath = (0, path_1.join)("/root", "base-config-1.json");
308 var actualConfig = { extends: ["./base-config-1"] };
309 var actualConfigPath = (0, path_1.join)("/root", "tsconfig.json");
310 var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "tsconfig.json"), function (path) { return [baseConfigPath, actualConfigPath].indexOf(path) >= 0; }, function (path) {
311 if (path === baseConfigPath) {
312 return JSON.stringify(baseConfig);
313 }
314 if (path === actualConfigPath) {
315 return JSON.stringify(actualConfig);
316 }
317 return "";
318 });
319 expect(res).toEqual({
320 extends: ["./base-config-1"],
321 compilerOptions: {
322 baseUrl: ".",
323 paths: { foo: ["bar"] },
324 },
325 });
326 });
327});
328//# sourceMappingURL=tsconfig-loader.test.js.map
Note: See TracBrowser for help on using the repository browser.