tsconfig-loader.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {
  2. loadTsconfig,
  3. tsConfigLoader,
  4. walkForTsConfig,
  5. } from "../tsconfig-loader";
  6. import { join } from "path";
  7. describe("tsconfig-loader", () => {
  8. it("should find tsconfig in cwd", () => {
  9. const result = tsConfigLoader({
  10. cwd: "/foo/bar",
  11. getEnv: (_: string) => undefined,
  12. loadSync: (cwd: string) => {
  13. return {
  14. tsConfigPath: `${cwd}/tsconfig.json`,
  15. baseUrl: "./",
  16. paths: {},
  17. };
  18. },
  19. });
  20. // assert.equal(result.tsConfigPath, "/foo/bar/tsconfig.json");
  21. expect(result.tsConfigPath).toBe("/foo/bar/tsconfig.json");
  22. });
  23. it("should return loaderResult.tsConfigPath as undefined when not found", () => {
  24. const result = tsConfigLoader({
  25. cwd: "/foo/bar",
  26. getEnv: (_: string) => undefined,
  27. loadSync: (_: string) => {
  28. return {
  29. tsConfigPath: undefined,
  30. baseUrl: "./",
  31. paths: {},
  32. };
  33. },
  34. });
  35. // assert.isUndefined(result.tsConfigPath);
  36. expect(result.tsConfigPath).toBeUndefined();
  37. });
  38. it("should use TS_NODE_PROJECT env if exists", () => {
  39. const result = tsConfigLoader({
  40. cwd: "/foo/bar",
  41. getEnv: (key: string) =>
  42. key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined,
  43. loadSync: (cwd: string, fileName: string) => {
  44. if (cwd === "/foo/bar" && fileName === "/foo/baz") {
  45. return {
  46. tsConfigPath: "/foo/baz/tsconfig.json",
  47. baseUrl: "./",
  48. paths: {},
  49. };
  50. }
  51. return {
  52. tsConfigPath: undefined,
  53. baseUrl: "./",
  54. paths: {},
  55. };
  56. },
  57. });
  58. // assert.equal(result.tsConfigPath, "/foo/baz/tsconfig.json");
  59. expect(result.tsConfigPath).toBe("/foo/baz/tsconfig.json");
  60. });
  61. it("should use TS_NODE_BASEURL env if exists", () => {
  62. const result = tsConfigLoader({
  63. cwd: "/foo/bar",
  64. getEnv: (key: string) =>
  65. key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined,
  66. loadSync: (_0: string, _1: string, baseUrl: string) => {
  67. return {
  68. tsConfigPath: undefined,
  69. 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", () => {
  78. const result = tsConfigLoader({
  79. cwd: "/foo/bar",
  80. getEnv: (_: string) => {
  81. return undefined;
  82. },
  83. loadSync: (_0: string, _1: string, baseUrl: string) => {
  84. return {
  85. tsConfigPath: undefined,
  86. baseUrl,
  87. paths: {},
  88. };
  89. },
  90. });
  91. // assert.equal(result.baseUrl, undefined);
  92. expect(result.baseUrl).toBeUndefined();
  93. });
  94. });
  95. describe("walkForTsConfig", () => {
  96. it("should find tsconfig in starting directory", () => {
  97. const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
  98. const res = walkForTsConfig(
  99. join("/root", "dir1"),
  100. (path) => path === pathToTsconfig
  101. );
  102. // assert.equal(res, pathToTsconfig);
  103. expect(res).toBe(pathToTsconfig);
  104. });
  105. it("should find tsconfig in parent directory", () => {
  106. const pathToTsconfig = join("/root", "tsconfig.json");
  107. const res = walkForTsConfig(
  108. join("/root", "dir1"),
  109. (path) => path === pathToTsconfig
  110. );
  111. // assert.equal(res, pathToTsconfig);
  112. expect(res).toBe(pathToTsconfig);
  113. });
  114. it("should return undefined when reaching the top", () => {
  115. const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => false);
  116. // assert.equal(res, undefined);
  117. expect(res).toBeUndefined();
  118. });
  119. });
  120. describe("loadConfig", () => {
  121. it("It should load a config", () => {
  122. const config = { compilerOptions: { baseUrl: "hej" } };
  123. const res = loadTsconfig(
  124. "/root/dir1/tsconfig.json",
  125. (path) => path === "/root/dir1/tsconfig.json",
  126. (_) => JSON.stringify(config)
  127. );
  128. // assert.deepEqual(res, config);
  129. expect(res).toStrictEqual(config);
  130. });
  131. it("It should load a config with comments", () => {
  132. const config = { compilerOptions: { baseUrl: "hej" } };
  133. const res = loadTsconfig(
  134. "/root/dir1/tsconfig.json",
  135. (path) => path === "/root/dir1/tsconfig.json",
  136. (_) => `{
  137. // my comment
  138. "compilerOptions": {
  139. "baseUrl": "hej"
  140. }
  141. }`
  142. );
  143. // assert.deepEqual(res, config);
  144. expect(res).toStrictEqual(config);
  145. });
  146. it("It should load a config with trailing commas", () => {
  147. const config = { compilerOptions: { baseUrl: "hej" } };
  148. const res = loadTsconfig(
  149. "/root/dir1/tsconfig.json",
  150. (path) => path === "/root/dir1/tsconfig.json",
  151. (_) => `{
  152. "compilerOptions": {
  153. "baseUrl": "hej",
  154. },
  155. }`
  156. );
  157. // assert.deepEqual(res, config);
  158. expect(res).toStrictEqual(config);
  159. });
  160. it("It should load a config with extends and overwrite all options", () => {
  161. const firstConfig = {
  162. extends: "../base-config.json",
  163. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  164. };
  165. const firstConfigPath = join("/root", "dir1", "tsconfig.json");
  166. const baseConfig = {
  167. compilerOptions: {
  168. baseUrl: "olle",
  169. paths: { foo: ["bar1"] },
  170. strict: true,
  171. },
  172. };
  173. const baseConfigPath = join("/root", "base-config.json");
  174. const res = loadTsconfig(
  175. join("/root", "dir1", "tsconfig.json"),
  176. (path) => path === firstConfigPath || path === baseConfigPath,
  177. (path) => {
  178. if (path === firstConfigPath) {
  179. return JSON.stringify(firstConfig);
  180. }
  181. if (path === baseConfigPath) {
  182. return JSON.stringify(baseConfig);
  183. }
  184. return "";
  185. }
  186. );
  187. // assert.deepEqual(res, {
  188. // extends: "../base-config.json",
  189. // compilerOptions: {
  190. // baseUrl: "kalle",
  191. // paths: { foo: ["bar2"] },
  192. // strict: true,
  193. // },
  194. // });
  195. expect(res).toEqual({
  196. extends: "../base-config.json",
  197. compilerOptions: {
  198. baseUrl: "kalle",
  199. paths: { foo: ["bar2"] },
  200. strict: true,
  201. },
  202. });
  203. });
  204. it("It should load a config with extends from node_modules and overwrite all options", () => {
  205. const firstConfig = {
  206. extends: "my-package/base-config.json",
  207. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } },
  208. };
  209. const firstConfigPath = join("/root", "dir1", "tsconfig.json");
  210. const baseConfig = {
  211. compilerOptions: {
  212. baseUrl: "olle",
  213. paths: { foo: ["bar1"] },
  214. strict: true,
  215. },
  216. };
  217. const baseConfigPath = join(
  218. "/root",
  219. "dir1",
  220. "node_modules",
  221. "my-package",
  222. "base-config.json"
  223. );
  224. const res = loadTsconfig(
  225. join("/root", "dir1", "tsconfig.json"),
  226. (path) => path === firstConfigPath || path === baseConfigPath,
  227. (path) => {
  228. if (path === firstConfigPath) {
  229. return JSON.stringify(firstConfig);
  230. }
  231. if (path === baseConfigPath) {
  232. return JSON.stringify(baseConfig);
  233. }
  234. return "";
  235. }
  236. );
  237. // assert.deepEqual(res, {
  238. // extends: "my-package/base-config.json",
  239. // compilerOptions: {
  240. // baseUrl: "kalle",
  241. // paths: { foo: ["bar2"] },
  242. // strict: true,
  243. // },
  244. // });
  245. expect(res).toEqual({
  246. extends: "my-package/base-config.json",
  247. compilerOptions: {
  248. baseUrl: "kalle",
  249. paths: { foo: ["bar2"] },
  250. strict: true,
  251. },
  252. });
  253. });
  254. it("Should use baseUrl relative to location of extended tsconfig", () => {
  255. const firstConfig = { compilerOptions: { baseUrl: "." } };
  256. const firstConfigPath = join("/root", "first-config.json");
  257. const secondConfig = { extends: "../first-config.json" };
  258. const secondConfigPath = join("/root", "dir1", "second-config.json");
  259. const thirdConfig = { extends: "../second-config.json" };
  260. const thirdConfigPath = join("/root", "dir1", "dir2", "third-config.json");
  261. const res = loadTsconfig(
  262. join("/root", "dir1", "dir2", "third-config.json"),
  263. (path) =>
  264. path === firstConfigPath ||
  265. path === secondConfigPath ||
  266. path === thirdConfigPath,
  267. (path) => {
  268. if (path === firstConfigPath) {
  269. return JSON.stringify(firstConfig);
  270. }
  271. if (path === secondConfigPath) {
  272. return JSON.stringify(secondConfig);
  273. }
  274. if (path === thirdConfigPath) {
  275. return JSON.stringify(thirdConfig);
  276. }
  277. return "";
  278. }
  279. );
  280. // assert.deepEqual(res, {
  281. // extends: "../second-config.json",
  282. // compilerOptions: { baseUrl: join("..", "..") },
  283. // });
  284. expect(res).toEqual({
  285. extends: "../second-config.json",
  286. compilerOptions: { baseUrl: join("..", "..") },
  287. });
  288. });
  289. });