|
| 1 | +// META: global=dedicatedworker-module,sharedworker-module,serviceworker-module |
| 2 | + |
| 3 | +import { importMetaOnRootModule, importMetaOnDependentModule } |
| 4 | + from "./import-meta-root.js"; |
| 5 | + |
| 6 | +test(() => { |
| 7 | + assert_equals(typeof import.meta.resolve, "function"); |
| 8 | + assert_equals(import.meta.resolve.name, "resolve"); |
| 9 | + assert_equals(import.meta.resolve.length, 1); |
| 10 | + assert_equals(Object.getPrototypeOf(import.meta.resolve), Function.prototype); |
| 11 | +}, "import.meta.resolve is a function with the right properties"); |
| 12 | + |
| 13 | +test(() => { |
| 14 | + assert_false(isConstructor(import.meta.resolve)); |
| 15 | + |
| 16 | + assert_throws_js(TypeError, () => new import.meta.resolve("./x")); |
| 17 | +}, "import.meta.resolve is not a constructor"); |
| 18 | + |
| 19 | +test(() => { |
| 20 | + // See also tests in ./import-meta-resolve-importmap.html. |
| 21 | + |
| 22 | + assert_equals(import.meta.resolve({ toString() { return "./x"; } }), resolveURL("x")); |
| 23 | + assert_throws_js(TypeError, () => import.meta.resolve(Symbol("./x")), |
| 24 | + "symbol"); |
| 25 | + assert_throws_js(TypeError, () => import.meta.resolve(), |
| 26 | + "no argument (which is treated like \"undefined\")"); |
| 27 | +}, "import.meta.resolve ToString()s its argument"); |
| 28 | + |
| 29 | +test(() => { |
| 30 | + assert_equals(import.meta.resolve("./x"), resolveURL("x"), |
| 31 | + "current module import.meta"); |
| 32 | + assert_equals(importMetaOnRootModule.resolve("./x"), resolveURL("x"), |
| 33 | + "sibling module import.meta"); |
| 34 | + assert_equals(importMetaOnDependentModule.resolve("./x"), resolveURL("x"), |
| 35 | + "dependency module import.meta"); |
| 36 | +}, "Relative URL-like specifier resolution"); |
| 37 | + |
| 38 | +test(() => { |
| 39 | + assert_equals(import.meta.resolve("https://example.com/"), "https://example.com/", |
| 40 | + "current module import.meta"); |
| 41 | + assert_equals(importMetaOnRootModule.resolve("https://example.com/"), "https://example.com/", |
| 42 | + "sibling module import.meta"); |
| 43 | + assert_equals(importMetaOnDependentModule.resolve("https://example.com/"), "https://example.com/", |
| 44 | + "dependency module import.meta"); |
| 45 | +}, "Absolute URL-like specifier resolution"); |
| 46 | + |
| 47 | +test(() => { |
| 48 | + const invalidSpecifiers = [ |
| 49 | + "https://eggplant:b/c", |
| 50 | + "pumpkins.js", |
| 51 | + ".tomato", |
| 52 | + "..zuccini.mjs", |
| 53 | + ".\\yam.es" |
| 54 | + ]; |
| 55 | + |
| 56 | + for (const specifier of invalidSpecifiers) { |
| 57 | + assert_throws_js(TypeError, () => import.meta.resolve(specifier), specifier); |
| 58 | + } |
| 59 | +}, "Invalid module specifiers"); |
| 60 | + |
| 61 | +test(() => { |
| 62 | + const { resolve } = import.meta; |
| 63 | + assert_equals(resolve("https://example.com/"), "https://example.com/", "current module import.meta"); |
| 64 | +}, "Works fine with no this value"); |
| 65 | + |
| 66 | +function resolveURL(urlRelativeToThisTest) { |
| 67 | + return (new URL(urlRelativeToThisTest, location.href)).href; |
| 68 | +} |
| 69 | + |
| 70 | +function isConstructor(o) { |
| 71 | + try { |
| 72 | + new (new Proxy(o, { construct: () => ({}) })); |
| 73 | + return true; |
| 74 | + } catch { |
| 75 | + return false; |
| 76 | + } |
| 77 | +} |
0 commit comments