try-path-tests.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { assert } from "chai";
  2. import { getPathsToTry } from "../src/try-path";
  3. import { join } from "path";
  4. describe("mapping-entry", () => {
  5. const abosolutePathMappings = [
  6. {
  7. pattern: "longest/pre/fix/*",
  8. paths: [join("/absolute", "base", "url", "foo2", "bar")]
  9. },
  10. { pattern: "pre/fix/*", paths: [join("/absolute", "base", "url", "foo3")] },
  11. { pattern: "*", paths: [join("/absolute", "base", "url", "foo1")] }
  12. ];
  13. it("should return no paths for relative requested module", () => {
  14. const result = getPathsToTry(
  15. [".ts", "tsx"],
  16. abosolutePathMappings,
  17. "./requested-module"
  18. );
  19. assert.deepEqual(result, undefined);
  20. });
  21. it("should return no paths if no pattern match the requested module", () => {
  22. const result = getPathsToTry(
  23. [".ts", "tsx"],
  24. [
  25. {
  26. pattern: "longest/pre/fix/*",
  27. paths: [join("/absolute", "base", "url", "foo2", "bar")]
  28. },
  29. {
  30. pattern: "pre/fix/*",
  31. paths: [join("/absolute", "base", "url", "foo3")]
  32. }
  33. ],
  34. "requested-module"
  35. );
  36. assert.deepEqual(result, undefined);
  37. });
  38. it("should get all paths that matches requested module", () => {
  39. const result = getPathsToTry(
  40. [".ts", ".tsx"],
  41. abosolutePathMappings,
  42. "longest/pre/fix/requested-module"
  43. );
  44. assert.deepEqual(result, [
  45. // "longest/pre/fix/*"
  46. { type: "file", path: join("/absolute", "base", "url", "foo2", "bar") },
  47. {
  48. type: "extension",
  49. path: join("/absolute", "base", "url", "foo2", "bar.ts")
  50. },
  51. {
  52. type: "extension",
  53. path: join("/absolute", "base", "url", "foo2", "bar.tsx")
  54. },
  55. {
  56. type: "package",
  57. path: join("/absolute", "base", "url", "foo2", "bar", "package.json")
  58. },
  59. {
  60. type: "index",
  61. path: join("/absolute", "base", "url", "foo2", "bar", "index.ts")
  62. },
  63. {
  64. type: "index",
  65. path: join("/absolute", "base", "url", "foo2", "bar", "index.tsx")
  66. },
  67. // "*"
  68. { type: "file", path: join("/absolute", "base", "url", "foo1") },
  69. { type: "extension", path: join("/absolute", "base", "url", "foo1.ts") },
  70. { type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") },
  71. {
  72. type: "package",
  73. path: join("/absolute", "base", "url", "foo1", "package.json")
  74. },
  75. {
  76. type: "index",
  77. path: join("/absolute", "base", "url", "foo1", "index.ts")
  78. },
  79. {
  80. type: "index",
  81. path: join("/absolute", "base", "url", "foo1", "index.tsx")
  82. }
  83. ]);
  84. });
  85. });
  86. // describe("match-star", () => {
  87. // it("should match star in last position", () => {
  88. // const result = matchStar("lib/*", "lib/mylib");
  89. // assert.equal(result, "mylib");
  90. // });
  91. // it("should match star in first position", () => {
  92. // const result = matchStar("*/lib", "mylib/lib");
  93. // assert.equal(result, "mylib");
  94. // });
  95. // });