tsconfig-loader-tests.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { assert } from "chai";
  2. import {
  3. loadTsconfig,
  4. tsConfigLoader,
  5. walkForTsConfig
  6. } from "../src/tsconfig-loader";
  7. import { join } from "path";
  8. describe("tsconfig-loader", () => {
  9. it("should find tsconfig in cwd", () => {
  10. const result = tsConfigLoader({
  11. cwd: "/foo/bar",
  12. getEnv: (_: string) => undefined,
  13. loadSync: (cwd: string) => {
  14. return {
  15. tsConfigPath: `${cwd}/tsconfig.json`,
  16. baseUrl: "./",
  17. paths: {}
  18. };
  19. }
  20. });
  21. assert.equal(result.tsConfigPath, "/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. });
  37. it("should use TS_NODE_PROJECT env if exists", () => {
  38. const result = tsConfigLoader({
  39. cwd: "/foo/bar",
  40. getEnv: (key: string) =>
  41. key === "TS_NODE_PROJECT" ? "/foo/baz" : undefined,
  42. loadSync: (cwd: string, fileName: string) => {
  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. });
  59. });
  60. describe("walkForTsConfig", () => {
  61. it("should find tsconfig in starting directory", () => {
  62. const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
  63. const res = walkForTsConfig(
  64. join("/root", "dir1"),
  65. path => path === pathToTsconfig
  66. );
  67. assert.equal(res, pathToTsconfig);
  68. });
  69. it("should find tsconfig in parent directory", () => {
  70. const pathToTsconfig = join("/root", "tsconfig.json");
  71. const res = walkForTsConfig(
  72. join("/root", "dir1"),
  73. path => path === pathToTsconfig
  74. );
  75. assert.equal(res, pathToTsconfig);
  76. });
  77. it("should return undefined when reaching the top", () => {
  78. const res = walkForTsConfig(join("/root", "dir1", "kalle"), () => false);
  79. assert.equal(res, undefined);
  80. });
  81. });
  82. describe("loadConfig", () => {
  83. it("It should load a config", () => {
  84. const config = { compilerOptions: { baseUrl: "hej" } };
  85. const res = loadTsconfig(
  86. "/root/dir1/tsconfig.json",
  87. path => path === "/root/dir1/tsconfig.json",
  88. _ => JSON.stringify(config)
  89. );
  90. assert.deepEqual(res, config);
  91. });
  92. it("It should load a config with comments", () => {
  93. const config = { compilerOptions: { baseUrl: "hej" } };
  94. const res = loadTsconfig(
  95. "/root/dir1/tsconfig.json",
  96. path => path === "/root/dir1/tsconfig.json",
  97. _ => `{
  98. // my comment
  99. "compilerOptions": {
  100. "baseUrl": "hej"
  101. }
  102. }`
  103. );
  104. assert.deepEqual(res, config);
  105. });
  106. it("It should load a config with trailing commas", () => {
  107. const config = { compilerOptions: { baseUrl: "hej" } };
  108. const res = loadTsconfig(
  109. "/root/dir1/tsconfig.json",
  110. path => path === "/root/dir1/tsconfig.json",
  111. _ => `{
  112. "compilerOptions": {
  113. "baseUrl": "hej",
  114. },
  115. }`
  116. );
  117. assert.deepEqual(res, config);
  118. });
  119. it("It should load a config with extends and overwrite all options", () => {
  120. const firstConfig = {
  121. extends: "../base-config.json",
  122. compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }
  123. };
  124. const firstConfigPath = join("/root", "dir1", "tsconfig.json");
  125. const baseConfig = {
  126. compilerOptions: {
  127. baseUrl: "olle",
  128. paths: { foo: ["bar1"] },
  129. strict: true
  130. }
  131. };
  132. const baseConfigPath = join("/root", "base-config.json");
  133. const res = loadTsconfig(
  134. join("/root", "dir1", "tsconfig.json"),
  135. path => path === firstConfigPath || path === baseConfigPath,
  136. path => {
  137. if (path === firstConfigPath) {
  138. return JSON.stringify(firstConfig);
  139. }
  140. if (path === baseConfigPath) {
  141. return JSON.stringify(baseConfig);
  142. }
  143. return "";
  144. }
  145. );
  146. assert.deepEqual(res, {
  147. extends: "../base-config.json",
  148. compilerOptions: {
  149. baseUrl: "kalle",
  150. paths: { foo: ["bar2"] },
  151. strict: true
  152. }
  153. });
  154. });
  155. it("Should use baseUrl relative to location of extended tsconfig", () => {
  156. const firstConfig = { compilerOptions: { baseUrl: "." } };
  157. const firstConfigPath = join("/root", "first-config.json");
  158. const secondConfig = { extends: "../first-config.json" };
  159. const secondConfigPath = join("/root", "dir1", "second-config.json");
  160. const thirdConfig = { extends: "../second-config.json" };
  161. const thirdConfigPath = join("/root", "dir1", "dir2", "third-config.json");
  162. const res = loadTsconfig(
  163. join("/root", "dir1", "dir2", "third-config.json"),
  164. path =>
  165. path === firstConfigPath ||
  166. path === secondConfigPath ||
  167. path === thirdConfigPath,
  168. path => {
  169. if (path === firstConfigPath) {
  170. return JSON.stringify(firstConfig);
  171. }
  172. if (path === secondConfigPath) {
  173. return JSON.stringify(secondConfig);
  174. }
  175. if (path === thirdConfigPath) {
  176. return JSON.stringify(thirdConfig);
  177. }
  178. return "";
  179. }
  180. );
  181. assert.deepEqual(res, {
  182. extends: "../second-config.json",
  183. compilerOptions: { baseUrl: join("..", "..") }
  184. });
  185. });
  186. });