config-loader-tests.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { assert } from "chai";
  2. import {
  3. configLoader,
  4. loadConfig,
  5. ConfigLoaderFailResult,
  6. ConfigLoaderSuccessResult
  7. } from "../src/config-loader";
  8. import { join } from "path";
  9. describe("config-loader", (): void => {
  10. it("should use explicitParams when set", () => {
  11. const result = configLoader({
  12. explicitParams: {
  13. baseUrl: "/foo/bar",
  14. paths: {
  15. asd: ["asd"]
  16. }
  17. },
  18. cwd: "/baz"
  19. });
  20. const successResult = result as ConfigLoaderSuccessResult;
  21. assert.equal(successResult.resultType, "success");
  22. assert.equal(successResult.absoluteBaseUrl, "/foo/bar");
  23. assert.equal(successResult.paths["asd"][0], "asd");
  24. });
  25. it("should use explicitParams when set and add cwd when path is relative", () => {
  26. const result = configLoader({
  27. explicitParams: {
  28. baseUrl: "bar/",
  29. paths: {
  30. asd: ["asd"]
  31. }
  32. },
  33. cwd: "/baz"
  34. });
  35. const successResult = result as ConfigLoaderSuccessResult;
  36. assert.equal(successResult.resultType, "success");
  37. assert.equal(successResult.absoluteBaseUrl, join("/baz", "bar/"));
  38. });
  39. it("should fallback to tsConfigLoader when explicitParams is not set", () => {
  40. const result = configLoader({
  41. explicitParams: undefined,
  42. cwd: "/baz",
  43. // tslint:disable-next-line:no-any
  44. tsConfigLoader: (_: any) => ({
  45. tsConfigPath: "/baz/tsconfig.json",
  46. baseUrl: "./src",
  47. paths: {}
  48. })
  49. });
  50. const successResult = result as ConfigLoaderSuccessResult;
  51. assert.equal(successResult.resultType, "success");
  52. assert.equal(successResult.absoluteBaseUrl, join("/baz", "src"));
  53. });
  54. it("should show an error message when baseUrl is missing", () => {
  55. const result = configLoader({
  56. explicitParams: undefined,
  57. cwd: "/baz",
  58. // tslint:disable-next-line:no-any
  59. tsConfigLoader: (_: any) => ({
  60. tsConfigPath: "/baz/tsconfig.json",
  61. baseUrl: undefined,
  62. paths: {}
  63. })
  64. });
  65. const failResult = result as ConfigLoaderFailResult;
  66. assert.equal(failResult.resultType, "failed");
  67. assert.isTrue(failResult.message.indexOf("baseUrl") > -1);
  68. });
  69. it("should presume cwd to be a tsconfig file when loadConfig is called with absolute path to tsconfig.json", () => {
  70. // using tsconfig-named.json to ensure that future changes to fix
  71. // https://github.com/dividab/tsconfig-paths/issues/31
  72. // do not pass this test case just because of a directory walk looking
  73. // for tsconfig.json
  74. const configFile = join(__dirname, "tsconfig-named.json");
  75. const result = loadConfig(configFile);
  76. const successResult = result as ConfigLoaderSuccessResult;
  77. assert.equal(successResult.resultType, "success");
  78. assert.equal(successResult.configFileAbsolutePath, configFile);
  79. });
  80. });