tsconfig-loader.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // 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. });
  95. describe("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. });
  114. describe("loadConfig", function () {
  115. it("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("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("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("It should load a config with extends and overwrite all options", function () {
  134. var firstConfig = {
  135. extends: "../base-config.json",
  136. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  137. };
  138. var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
  139. var baseConfig = {
  140. compilerOptions: {
  141. baseUrl: "olle",
  142. paths: { foo: ["bar1"] },
  143. strict: true,
  144. },
  145. };
  146. var baseConfigPath = (0, path_1.join)("/root", "base-config.json");
  147. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
  148. if (path === firstConfigPath) {
  149. return JSON.stringify(firstConfig);
  150. }
  151. if (path === baseConfigPath) {
  152. return JSON.stringify(baseConfig);
  153. }
  154. return "";
  155. });
  156. // assert.deepEqual(res, {
  157. // extends: "../base-config.json",
  158. // compilerOptions: {
  159. // baseUrl: "kalle",
  160. // paths: { foo: ["bar2"] },
  161. // strict: true,
  162. // },
  163. // });
  164. expect(res).toEqual({
  165. extends: "../base-config.json",
  166. compilerOptions: {
  167. baseUrl: "kalle",
  168. paths: { foo: ["bar2"] },
  169. strict: true,
  170. },
  171. });
  172. });
  173. it("It should load a config with extends from node_modules and overwrite all options", function () {
  174. var firstConfig = {
  175. extends: "my-package/base-config.json",
  176. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  177. };
  178. var firstConfigPath = (0, path_1.join)("/root", "dir1", "tsconfig.json");
  179. var baseConfig = {
  180. compilerOptions: {
  181. baseUrl: "olle",
  182. paths: { foo: ["bar1"] },
  183. strict: true,
  184. },
  185. };
  186. var baseConfigPath = (0, path_1.join)("/root", "dir1", "node_modules", "my-package", "base-config.json");
  187. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "tsconfig.json"), function (path) { return path === firstConfigPath || path === baseConfigPath; }, function (path) {
  188. if (path === firstConfigPath) {
  189. return JSON.stringify(firstConfig);
  190. }
  191. if (path === baseConfigPath) {
  192. return JSON.stringify(baseConfig);
  193. }
  194. return "";
  195. });
  196. // assert.deepEqual(res, {
  197. // extends: "my-package/base-config.json",
  198. // compilerOptions: {
  199. // baseUrl: "kalle",
  200. // paths: { foo: ["bar2"] },
  201. // strict: true,
  202. // },
  203. // });
  204. expect(res).toEqual({
  205. extends: "my-package/base-config.json",
  206. compilerOptions: {
  207. baseUrl: "kalle",
  208. paths: { foo: ["bar2"] },
  209. strict: true,
  210. },
  211. });
  212. });
  213. it("Should use baseUrl relative to location of extended tsconfig", function () {
  214. var firstConfig = { compilerOptions: { baseUrl: "." } };
  215. var firstConfigPath = (0, path_1.join)("/root", "first-config.json");
  216. var secondConfig = { extends: "../first-config.json" };
  217. var secondConfigPath = (0, path_1.join)("/root", "dir1", "second-config.json");
  218. var thirdConfig = { extends: "../second-config.json" };
  219. var thirdConfigPath = (0, path_1.join)("/root", "dir1", "dir2", "third-config.json");
  220. var res = (0, tsconfig_loader_1.loadTsconfig)((0, path_1.join)("/root", "dir1", "dir2", "third-config.json"), function (path) {
  221. return path === firstConfigPath ||
  222. path === secondConfigPath ||
  223. path === thirdConfigPath;
  224. }, function (path) {
  225. if (path === firstConfigPath) {
  226. return JSON.stringify(firstConfig);
  227. }
  228. if (path === secondConfigPath) {
  229. return JSON.stringify(secondConfig);
  230. }
  231. if (path === thirdConfigPath) {
  232. return JSON.stringify(thirdConfig);
  233. }
  234. return "";
  235. });
  236. // assert.deepEqual(res, {
  237. // extends: "../second-config.json",
  238. // compilerOptions: { baseUrl: join("..", "..") },
  239. // });
  240. expect(res).toEqual({
  241. extends: "../second-config.json",
  242. compilerOptions: { baseUrl: (0, path_1.join)("..", "..") },
  243. });
  244. });
  245. });
  246. //# sourceMappingURL=tsconfig-loader.test.js.map