tsconfig-loader.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tsconfig_loader_1 = require("../tsconfig-loader");
  4. var path_1 = require("path");
  5. describe("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. expect(result.tsConfigPath).toBe("/foo/bar/tsconfig.json");
  19. });
  20. it("should return loaderResult.tsConfigPath as undefined when not found", function () {
  21. var result = (0, tsconfig_loader_1.tsConfigLoader)({
  22. cwd: "/foo/bar",
  23. getEnv: function (_) { return undefined; },
  24. loadSync: function (_) {
  25. return {
  26. tsConfigPath: undefined,
  27. baseUrl: "./",
  28. paths: {},
  29. };
  30. },
  31. });
  32. expect(result.tsConfigPath).toBeUndefined();
  33. });
  34. it("should use TS_NODE_PROJECT env if exists", function () {
  35. var result = (0, tsconfig_loader_1.tsConfigLoader)({
  36. cwd: "/foo/bar",
  37. getEnv: function (key) {
  38. return key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined;
  39. },
  40. loadSync: function (cwd, fileName) {
  41. if (cwd === "/foo/bar" && fileName === "/foo/baz") {
  42. return {
  43. tsConfigPath: "/foo/baz/tsconfig.json",
  44. baseUrl: "./",
  45. paths: {},
  46. };
  47. }
  48. return {
  49. tsConfigPath: undefined,
  50. baseUrl: "./",
  51. paths: {},
  52. };
  53. },
  54. });
  55. expect(result.tsConfigPath).toBe("/foo/baz/tsconfig.json");
  56. });
  57. it("should use TS_NODE_BASEURL env if exists", function () {
  58. var result = (0, tsconfig_loader_1.tsConfigLoader)({
  59. cwd: "/foo/bar",
  60. getEnv: function (key) {
  61. return key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined;
  62. },
  63. loadSync: function (_0, _1, baseUrl) {
  64. return {
  65. tsConfigPath: undefined,
  66. baseUrl: baseUrl,
  67. paths: {},
  68. };
  69. },
  70. });
  71. expect(result.baseUrl).toBe("SOME_BASEURL");
  72. });
  73. it("should not use TS_NODE_BASEURL env if it does not exist", function () {
  74. var result = (0, tsconfig_loader_1.tsConfigLoader)({
  75. cwd: "/foo/bar",
  76. getEnv: function (_) {
  77. return undefined;
  78. },
  79. loadSync: function (_0, _1, baseUrl) {
  80. return {
  81. tsConfigPath: undefined,
  82. baseUrl: baseUrl,
  83. paths: {},
  84. };
  85. },
  86. });
  87. expect(result.baseUrl).toBeUndefined();
  88. });
  89. });
  90. describe("walkForTsConfig", function () {
  91. it("should find tsconfig in starting directory", function () {
  92. var pathToTsconfig = (0, path_1.join)("/root", "dir1", "tsconfig.json");
  93. var mockFiles = {
  94. "/root/dir1": ["tsconfig.json"],
  95. };
  96. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return mockFiles[path] || []; });
  97. expect(res).toBe(pathToTsconfig);
  98. });
  99. it("should find jsconfig in starting directory", function () {
  100. var pathToJsconfig = (0, path_1.join)("/root", "dir1", "jsconfig.json");
  101. var mockFiles = {
  102. "/root/dir1": ["jsconfig.json"],
  103. };
  104. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return mockFiles[path] || []; });
  105. expect(res).toBe(pathToJsconfig);
  106. });
  107. // see https://github.com/Microsoft/TypeScript/issues/15869#issuecomment-301845650
  108. it("tsconfig.json take precedence over jsconfig.json when both exist", function () {
  109. var pathToTsconfig = (0, path_1.join)("/root/dir1", "tsconfig.json");
  110. var mockFiles = {
  111. "/root/dir1": ["jsconfig.json", "tsconfig.json"],
  112. };
  113. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return mockFiles[path] || []; });
  114. expect(res).toBe(pathToTsconfig);
  115. });
  116. it("should find tsconfig in parent directory", function () {
  117. var pathToTsconfig = (0, path_1.join)("/root", "tsconfig.json");
  118. var mockFiles = {
  119. "/root": ["tsconfig.json"],
  120. };
  121. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return mockFiles[path] || []; });
  122. expect(res).toBe(pathToTsconfig);
  123. });
  124. it("should find jsconfig in parent directory", function () {
  125. var pathToTsconfig = (0, path_1.join)("/root", "jsconfig.json");
  126. var mockFiles = {
  127. "/root": ["jsconfig.json"],
  128. };
  129. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1"), function (path) { return mockFiles[path] || []; });
  130. expect(res).toBe(pathToTsconfig);
  131. });
  132. it("should return undefined when reaching the top", function () {
  133. var res = (0, tsconfig_loader_1.walkForTsConfig)((0, path_1.join)("/root", "dir1", "kalle"), function () { return []; });
  134. expect(res).toBeUndefined();
  135. });
  136. });
  137. describe("loadConfig", function () {
  138. it("should load a config", function () {
  139. var config = { compilerOptions: { baseUrl: "hej" } };
  140. var res = (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return JSON.stringify(config); });
  141. expect(res).toStrictEqual(config);
  142. });
  143. it("should load a config with comments", function () {
  144. var config = { compilerOptions: { baseUrl: "hej" } };
  145. 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 }"; });
  146. expect(res).toStrictEqual(config);
  147. });
  148. it("should load a config with trailing commas", function () {
  149. var config = { compilerOptions: { baseUrl: "hej" } };
  150. 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 }"; });
  151. expect(res).toStrictEqual(config);
  152. });
  153. it("should throw an error including the file path when encountering invalid JSON5", function () {
  154. expect(function () {
  155. return (0, tsconfig_loader_1.loadTsconfig)("/root/dir1/tsconfig.json", function (path) { return path === "/root/dir1/tsconfig.json"; }, function (_) { return "{\n \"compilerOptions\": {\n }"; });
  156. }).toThrowError("/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12");
  157. });
  158. it("should load a config with string extends and overwrite all options", function () {
  159. var firstConfig = {
  160. extends: "../base-config.json",
  161. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  162. };
  163. var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
  164. var baseConfig = {
  165. compilerOptions: {
  166. baseUrl: "olle",
  167. paths: { foo: ["bar1"] },
  168. strict: true,
  169. },
  170. };
  171. var baseConfigPath = (0, path_1.join)("/root", "base-config.json");
  172. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
  173. if (path === firstConfigPath) {
  174. return JSON.stringify(firstConfig);
  175. }
  176. if (path === baseConfigPath) {
  177. return JSON.stringify(baseConfig);
  178. }
  179. return "";
  180. });
  181. expect(res).toEqual({
  182. extends: "../base-config.json",
  183. compilerOptions: {
  184. baseUrl: "kalle",
  185. paths: { foo: ["bar2"] },
  186. strict: true,
  187. },
  188. });
  189. });
  190. it("should load a config with string extends from node_modules and overwrite all options", function () {
  191. var firstConfig = {
  192. extends: "my-package/base-config.json",
  193. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  194. };
  195. var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
  196. var baseConfig = {
  197. compilerOptions: {
  198. baseUrl: "olle",
  199. paths: { foo: ["bar1"] },
  200. strict: true,
  201. },
  202. };
  203. var baseConfigPath = (0, path_1.join)("/root", "dir1", "node_modules", "my-package", "base-config.json");
  204. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
  205. if (path === firstConfigPath) {
  206. return JSON.stringify(firstConfig);
  207. }
  208. if (path === baseConfigPath) {
  209. return JSON.stringify(baseConfig);
  210. }
  211. return "";
  212. });
  213. expect(res).toEqual({
  214. extends: "my-package/base-config.json",
  215. compilerOptions: {
  216. baseUrl: "kalle",
  217. paths: { foo: ["bar2"] },
  218. strict: true,
  219. },
  220. });
  221. });
  222. it("should use baseUrl relative to location of extended tsconfig", function () {
  223. var firstConfig = { compilerOptions: { baseUrl: "." } };
  224. var firstConfigPath = (0, path_1.join)("/root", "first-config.json");
  225. var secondConfig = { extends: "../first-config.json" };
  226. var secondConfigPath = (0, path_1.join)("/root", "dir1", "second-config.json");
  227. var thirdConfig = { extends: "../second-config.json" };
  228. var thirdConfigPath = (0, path_1.join)("/root", "dir1", "dir2", "third-config.json");
  229. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "dir2", "third-config.json"), function (path) {
  230. return path === firstConfigPath ||
  231. path === secondConfigPath ||
  232. path === thirdConfigPath;
  233. }, function (path) {
  234. if (path === firstConfigPath) {
  235. return JSON.stringify(firstConfig);
  236. }
  237. if (path === secondConfigPath) {
  238. return JSON.stringify(secondConfig);
  239. }
  240. if (path === thirdConfigPath) {
  241. return JSON.stringify(thirdConfig);
  242. }
  243. return "";
  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