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