| 1 | import { getPathsToTry } from "../try-path";
|
|---|
| 2 | import { join } from "path";
|
|---|
| 3 |
|
|---|
| 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 | const abosolutePathMappingsStarstWithSlash = [
|
|---|
| 14 | {
|
|---|
| 15 | pattern: "/opt/*",
|
|---|
| 16 | paths: [join("/absolute", "src", "aws-layer")],
|
|---|
| 17 | },
|
|---|
| 18 | {
|
|---|
| 19 | pattern: "*",
|
|---|
| 20 | paths: [join("/absolute", "src")],
|
|---|
| 21 | },
|
|---|
| 22 | ];
|
|---|
| 23 | it("should return no paths for relative requested module", () => {
|
|---|
| 24 | const result = getPathsToTry(
|
|---|
| 25 | [".ts", "tsx"],
|
|---|
| 26 | abosolutePathMappings,
|
|---|
| 27 | "./requested-module"
|
|---|
| 28 | );
|
|---|
| 29 | // assert.deepEqual(result, undefined);
|
|---|
| 30 | expect(result).toBeUndefined();
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | it("should return no paths if no pattern match the requested module", () => {
|
|---|
| 34 | const result = getPathsToTry(
|
|---|
| 35 | [".ts", "tsx"],
|
|---|
| 36 | [
|
|---|
| 37 | {
|
|---|
| 38 | pattern: "longest/pre/fix/*",
|
|---|
| 39 | paths: [join("/absolute", "base", "url", "foo2", "bar")],
|
|---|
| 40 | },
|
|---|
| 41 | {
|
|---|
| 42 | pattern: "pre/fix/*",
|
|---|
| 43 | paths: [join("/absolute", "base", "url", "foo3")],
|
|---|
| 44 | },
|
|---|
| 45 | ],
|
|---|
| 46 | "requested-module"
|
|---|
| 47 | );
|
|---|
| 48 | expect(result).toBeUndefined();
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | it("should get all paths that matches requested module", () => {
|
|---|
| 52 | const result = getPathsToTry(
|
|---|
| 53 | [".ts", ".tsx"],
|
|---|
| 54 | abosolutePathMappings,
|
|---|
| 55 | "longest/pre/fix/requested-module"
|
|---|
| 56 | );
|
|---|
| 57 | // assert.deepEqual(result, [
|
|---|
| 58 | // // "longest/pre/fix/*"
|
|---|
| 59 | // { type: "file", path: join("/absolute", "base", "url", "foo2", "bar") },
|
|---|
| 60 | // {
|
|---|
| 61 | // type: "extension",
|
|---|
| 62 | // path: join("/absolute", "base", "url", "foo2", "bar.ts"),
|
|---|
| 63 | // },
|
|---|
| 64 | // {
|
|---|
| 65 | // type: "extension",
|
|---|
| 66 | // path: join("/absolute", "base", "url", "foo2", "bar.tsx"),
|
|---|
| 67 | // },
|
|---|
| 68 | // {
|
|---|
| 69 | // type: "package",
|
|---|
| 70 | // path: join("/absolute", "base", "url", "foo2", "bar", "package.json"),
|
|---|
| 71 | // },
|
|---|
| 72 | // {
|
|---|
| 73 | // type: "index",
|
|---|
| 74 | // path: join("/absolute", "base", "url", "foo2", "bar", "index.ts"),
|
|---|
| 75 | // },
|
|---|
| 76 | // {
|
|---|
| 77 | // type: "index",
|
|---|
| 78 | // path: join("/absolute", "base", "url", "foo2", "bar", "index.tsx"),
|
|---|
| 79 | // },
|
|---|
| 80 | // // "*"
|
|---|
| 81 | // { type: "file", path: join("/absolute", "base", "url", "foo1") },
|
|---|
| 82 | // { type: "extension", path: join("/absolute", "base", "url", "foo1.ts") },
|
|---|
| 83 | // { type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") },
|
|---|
| 84 | // {
|
|---|
| 85 | // type: "package",
|
|---|
| 86 | // path: join("/absolute", "base", "url", "foo1", "package.json"),
|
|---|
| 87 | // },
|
|---|
| 88 | // {
|
|---|
| 89 | // type: "index",
|
|---|
| 90 | // path: join("/absolute", "base", "url", "foo1", "index.ts"),
|
|---|
| 91 | // },
|
|---|
| 92 | // {
|
|---|
| 93 | // type: "index",
|
|---|
| 94 | // path: join("/absolute", "base", "url", "foo1", "index.tsx"),
|
|---|
| 95 | // },
|
|---|
| 96 | // ]);
|
|---|
| 97 | expect(result).toEqual([
|
|---|
| 98 | // "longest/pre/fix/*"
|
|---|
| 99 | { type: "file", path: join("/absolute", "base", "url", "foo2", "bar") },
|
|---|
| 100 | {
|
|---|
| 101 | type: "extension",
|
|---|
| 102 | path: join("/absolute", "base", "url", "foo2", "bar.ts"),
|
|---|
| 103 | },
|
|---|
| 104 | {
|
|---|
| 105 | type: "extension",
|
|---|
| 106 | path: join("/absolute", "base", "url", "foo2", "bar.tsx"),
|
|---|
| 107 | },
|
|---|
| 108 | {
|
|---|
| 109 | type: "package",
|
|---|
| 110 | path: join("/absolute", "base", "url", "foo2", "bar", "package.json"),
|
|---|
| 111 | },
|
|---|
| 112 | {
|
|---|
| 113 | type: "index",
|
|---|
| 114 | path: join("/absolute", "base", "url", "foo2", "bar", "index.ts"),
|
|---|
| 115 | },
|
|---|
| 116 | {
|
|---|
| 117 | type: "index",
|
|---|
| 118 | path: join("/absolute", "base", "url", "foo2", "bar", "index.tsx"),
|
|---|
| 119 | },
|
|---|
| 120 | // "*"
|
|---|
| 121 | { type: "file", path: join("/absolute", "base", "url", "foo1") },
|
|---|
| 122 | { type: "extension", path: join("/absolute", "base", "url", "foo1.ts") },
|
|---|
| 123 | { type: "extension", path: join("/absolute", "base", "url", "foo1.tsx") },
|
|---|
| 124 | {
|
|---|
| 125 | type: "package",
|
|---|
| 126 | path: join("/absolute", "base", "url", "foo1", "package.json"),
|
|---|
| 127 | },
|
|---|
| 128 | {
|
|---|
| 129 | type: "index",
|
|---|
| 130 | path: join("/absolute", "base", "url", "foo1", "index.ts"),
|
|---|
| 131 | },
|
|---|
| 132 | {
|
|---|
| 133 | type: "index",
|
|---|
| 134 | path: join("/absolute", "base", "url", "foo1", "index.tsx"),
|
|---|
| 135 | },
|
|---|
| 136 | ]);
|
|---|
| 137 | });
|
|---|
| 138 |
|
|---|
| 139 | it("should resolve paths starting with a slash", () => {
|
|---|
| 140 | const result = getPathsToTry(
|
|---|
| 141 | [".ts"],
|
|---|
| 142 | abosolutePathMappingsStarstWithSlash,
|
|---|
| 143 | "/opt/utils"
|
|---|
| 144 | );
|
|---|
| 145 | expect(result).toEqual([
|
|---|
| 146 | // "opt/*"
|
|---|
| 147 | {
|
|---|
| 148 | path: join("/absolute", "src", "aws-layer"),
|
|---|
| 149 | type: "file",
|
|---|
| 150 | },
|
|---|
| 151 | {
|
|---|
| 152 | path: join("/absolute", "src", "aws-layer.ts"),
|
|---|
| 153 | type: "extension",
|
|---|
| 154 | },
|
|---|
| 155 | {
|
|---|
| 156 | path: join("/absolute", "src", "aws-layer", "package.json"),
|
|---|
| 157 | type: "package",
|
|---|
| 158 | },
|
|---|
| 159 | {
|
|---|
| 160 | path: join("/absolute", "src", "aws-layer", "index.ts"),
|
|---|
| 161 | type: "index",
|
|---|
| 162 | },
|
|---|
| 163 | // "*"
|
|---|
| 164 | {
|
|---|
| 165 | path: join("/absolute", "src"),
|
|---|
| 166 | type: "file",
|
|---|
| 167 | },
|
|---|
| 168 | {
|
|---|
| 169 | path: join("/absolute", "src.ts"),
|
|---|
| 170 | type: "extension",
|
|---|
| 171 | },
|
|---|
| 172 | {
|
|---|
| 173 | path: join("/absolute", "src", "package.json"),
|
|---|
| 174 | type: "package",
|
|---|
| 175 | },
|
|---|
| 176 | {
|
|---|
| 177 | path: join("/absolute", "src", "index.ts"),
|
|---|
| 178 | type: "index",
|
|---|
| 179 | },
|
|---|
| 180 | ]);
|
|---|
| 181 | });
|
|---|
| 182 | });
|
|---|
| 183 |
|
|---|
| 184 | // describe("match-star", () => {
|
|---|
| 185 | // it("should match star in last position", () => {
|
|---|
| 186 | // const result = matchStar("lib/*", "lib/mylib");
|
|---|
| 187 | // assert.equal(result, "mylib");
|
|---|
| 188 | // });
|
|---|
| 189 | // it("should match star in first position", () => {
|
|---|
| 190 | // const result = matchStar("*/lib", "mylib/lib");
|
|---|
| 191 | // assert.equal(result, "mylib");
|
|---|
| 192 | // });
|
|---|
| 193 | // });
|
|---|