1import { join, dirname } from "path"; 2import { removeExtension } from "../../src/filesystem"; 3 4export interface OneTest { 5 readonly name: string; 6 readonly only?: boolean; 7 readonly skip?: boolean; 8 readonly absoluteBaseUrl: string; 9 readonly paths: { [key: string]: Array<string> }; 10 readonly mainFields?: string[]; 11 readonly addMatchAll?: boolean; 12 readonly existingFiles: ReadonlyArray<string>; 13 readonly requestedModule: string; 14 readonly extensions?: ReadonlyArray<string>; 15 readonly packageJson?: {}; 16 readonly expectedPath: string | undefined; 17} 18 19export const tests: ReadonlyArray<OneTest> = [ 20 { 21 name: "should locate path that matches with star and exists", 22 absoluteBaseUrl: "/root/", 23 paths: { 24 "lib/*": ["location/*"] 25 }, 26 existingFiles: [join("/root", "location", "mylib", "index.ts")], 27 requestedModule: "lib/mylib", 28 expectedPath: dirname(join("/root", "location", "mylib", "index.ts")) 29 }, 30 { 31 name: "should resolve to correct path when many are specified", 32 absoluteBaseUrl: "/root/", 33 paths: { 34 "lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"] 35 }, 36 existingFiles: [join("/root", "location", "mylib", "index.ts")], 37 requestedModule: "lib/mylib", 38 extensions: [".ts"], 39 expectedPath: dirname(join("/root", "location", "mylib", "index.ts")) 40 }, 41 { 42 name: 43 "should locate path that matches with star and prioritize pattern with longest prefix", 44 absoluteBaseUrl: "/root/", 45 paths: { 46 "*": ["location/*"], 47 "lib/*": ["location/*"] 48 }, 49 existingFiles: [ 50 join("/root", "location", "lib", "mylib", "index.ts"), 51 join("/root", "location", "mylib", "index.ts") 52 ], 53 requestedModule: "lib/mylib", 54 expectedPath: dirname(join("/root", "location", "mylib", "index.ts")) 55 }, 56 { 57 name: "should locate path that matches with star and exists with extension", 58 absoluteBaseUrl: "/root/", 59 paths: { "lib/*": ["location/*"] }, 60 existingFiles: [join("/root", "location", "mylib.myext")], 61 requestedModule: "lib/mylib", 62 extensions: [".js", ".myext"], 63 expectedPath: removeExtension(join("/root", "location", "mylib.myext")) 64 }, 65 { 66 name: "should resolve request with extension specified", 67 absoluteBaseUrl: "/root/", 68 paths: { "lib/*": ["location/*"] }, 69 existingFiles: [join("/root", "location", "test.jpg")], 70 requestedModule: "lib/test.jpg", 71 expectedPath: join("/root", "location", "test.jpg") 72 }, 73 { 74 name: "should locate path that matches without star and exists", 75 absoluteBaseUrl: "/root/", 76 paths: { 77 "lib/foo": ["location/foo"] 78 }, 79 existingFiles: [join("/root", "location", "foo.ts")], 80 requestedModule: "lib/foo", 81 expectedPath: removeExtension(join("/root", "location", "foo.ts")) 82 }, 83 { 84 name: "should resolve to parent folder when filename is in subfolder", 85 absoluteBaseUrl: "/root/", 86 paths: { "lib/*": ["location/*"] }, 87 existingFiles: [join("/root", "location", "mylib", "index.ts")], 88 requestedModule: "lib/mylib", 89 expectedPath: dirname(join("/root", "location", "mylib", "index.ts")) 90 }, 91 { 92 name: "should resolve from main field in package.json", 93 absoluteBaseUrl: "/root/", 94 paths: { "lib/*": ["location/*"] }, 95 existingFiles: [join("/root", "location", "mylib", "kalle.ts")], 96 packageJson: { main: "./kalle.ts" }, 97 requestedModule: "lib/mylib", 98 expectedPath: removeExtension( 99 join("/root", "location", "mylib", "kalle.ts") 100 ) 101 }, 102 { 103 name: "should resolve from main field in package.json (js)", 104 absoluteBaseUrl: "/root", 105 paths: { "lib/*": ["location/*"] }, 106 existingFiles: [join("/root", "location", "mylib.js", "kalle.js")], 107 packageJson: { main: "./kalle.js" }, 108 requestedModule: "lib/mylib.js", 109 extensions: [".ts", ".js"], 110 expectedPath: removeExtension( 111 join("/root", "location", "mylib.js", "kalle.js") 112 ) 113 }, 114 { 115 name: 116 "should resolve from main field in package.json and correctly remove file extension", 117 absoluteBaseUrl: "/root/", 118 paths: { "lib/*": ["location/*"] }, 119 existingFiles: [join("/root", "location", "mylibjs", "kalle.js")], 120 packageJson: { main: "./kalle.js" }, 121 extensions: [".ts", ".js"], 122 requestedModule: "lib/mylibjs", 123 expectedPath: removeExtension( 124 join("/root", "location", "mylibjs", "kalle.js") 125 ) 126 }, 127 { 128 name: "should resolve from list of fields by priority in package.json", 129 absoluteBaseUrl: "/root/", 130 paths: { "lib/*": ["location/*"] }, 131 mainFields: ["missing", "browser", "main"], 132 packageJson: { main: "./main.js", browser: "./browser.js" }, 133 existingFiles: [ 134 join("/root", "location", "mylibjs", "main.js"), // mainFilePath 135 join("/root", "location", "mylibjs", "browser.js") // browserFilePath 136 ], 137 extensions: [".ts", ".js"], 138 requestedModule: "lib/mylibjs", 139 expectedPath: removeExtension( 140 join("/root", "location", "mylibjs", "browser.js") 141 ) 142 }, 143 { 144 name: "should ignore field mappings to missing files in package.json", 145 absoluteBaseUrl: "/root/", 146 paths: { "lib/*": ["location/*"] }, 147 mainFields: ["browser", "main"], 148 existingFiles: [join("/root", "location", "mylibjs", "kalle.js")], 149 requestedModule: "lib/mylibjs", 150 packageJson: { 151 main: "./kalle.js", 152 browser: "./nope.js" 153 }, 154 extensions: [".ts", ".js"], 155 expectedPath: removeExtension( 156 join("/root", "location", "mylibjs", "kalle.js") 157 ) 158 }, 159 { 160 name: "should ignore advanced field mappings in package.json", 161 absoluteBaseUrl: "/root/", 162 paths: { "lib/*": ["location/*"] }, 163 existingFiles: [ 164 join("/root", "location", "mylibjs", "kalle.js"), 165 join("/root", "location", "mylibjs", "browser.js") 166 ], 167 requestedModule: "lib/mylibjs", 168 packageJson: { 169 main: "./kalle.js", 170 browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" } 171 }, 172 extensions: [".ts", ".js"], 173 expectedPath: removeExtension( 174 join("/root", "location", "mylibjs", "kalle.js") 175 ) 176 }, 177 { 178 name: "should resolve to with the help of baseUrl when not explicitly set", 179 absoluteBaseUrl: "/root/", 180 paths: {}, 181 existingFiles: [join("/root", "mylib", "index.ts")], 182 requestedModule: "mylib", 183 expectedPath: dirname(join("/root", "mylib", "index.ts")) 184 }, 185 { 186 name: "should not resolve with the help of baseUrl when asked not to", 187 absoluteBaseUrl: "/root/", 188 paths: {}, 189 addMatchAll: false, 190 existingFiles: [join("/root", "mylib", "index.ts")], 191 requestedModule: "mylib", 192 expectedPath: undefined 193 }, 194 { 195 name: "should not locate path that does not match", 196 absoluteBaseUrl: "/root/", 197 paths: { "lib/*": ["location/*"] }, 198 existingFiles: [join("root", "location", "mylib")], 199 requestedModule: "mylib", 200 expectedPath: undefined 201 }, 202 { 203 name: "should not resolve typings file (index.d.ts)", 204 absoluteBaseUrl: "/root/", 205 paths: { 206 "lib/*": ["location/*"] 207 }, 208 existingFiles: [join("/root", "location", "mylib", "index.d.ts")], 209 requestedModule: "lib/mylib", 210 expectedPath: undefined 211 } 212]; 213