diff options
Diffstat (limited to 'test/js/bun/resolve/import-meta-resolve.test.mjs')
-rw-r--r-- | test/js/bun/resolve/import-meta-resolve.test.mjs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/test/js/bun/resolve/import-meta-resolve.test.mjs b/test/js/bun/resolve/import-meta-resolve.test.mjs new file mode 100644 index 000000000..ba28488a5 --- /dev/null +++ b/test/js/bun/resolve/import-meta-resolve.test.mjs @@ -0,0 +1,95 @@ +// You can run this test in Node.js/Deno +import assert from "node:assert"; + +const { test } = process?.versions?.bun ? Bun.jest(import.meta.path) : {}; + +function wrapped(name, f) { + if (test) { + test(name, f); + } else { + f(); + console.log("✅", name); + } +} + +function fileUrlRelTo(actual, expected_rel) { + try { + var compareTo; + wrapped(expected_rel, () => { + actual = actual(); + if (actual instanceof URL) actual = actual.toString(); + + compareTo = new URL(expected_rel, import.meta.url).toString(); + assert.strictEqual(actual, compareTo); + }); + } catch (error) { + if (typeof actual == "function") { + console.log(" ", error.message); + return; + } + console.log("❌", expected_rel); + console.log(" want: \x1b[32m%s\x1b[0m", compareTo); + console.log(" got: \x1b[31m%s\x1b[0m", actual); + return; + } +} + +function exact(actual, expected) { + try { + wrapped(expected, () => { + actual = actual(); + if (actual instanceof URL) actual = actual.toString(); + assert.strictEqual(actual, expected); + }); + } catch (error) { + console.log("❌", expected); + if (typeof actual == "function") { + console.log(" ", error.message); + return; + } + console.log(" want: \x1b[32m%s\x1b[0m", expected); + console.log(" got: \x1b[31m%s\x1b[0m", actual); + return; + } +} + +function throws(compute, label) { + if (test) { + } + try { + wrapped(label, () => { + try { + compute(); + } catch (error) { + return; + } + throw new Error("Test failed"); + }); + } catch { + console.log("❌", label); + } +} + +fileUrlRelTo(() => import.meta.resolve("./haha.mjs"), "./haha.mjs"); +fileUrlRelTo(() => import.meta.resolve("../haha.mjs"), "../haha.mjs"); +fileUrlRelTo(() => import.meta.resolve("/haha.mjs"), "/haha.mjs"); +fileUrlRelTo(() => import.meta.resolve("/haha"), "/haha"); +fileUrlRelTo(() => import.meta.resolve("/~"), "/~"); +fileUrlRelTo(() => import.meta.resolve("./🅱️un"), "./🅱️un"); + +// will fail on deno because it is `npm:*` specifier not a file path +fileUrlRelTo(() => import.meta.resolve("lodash"), "../../../node_modules/lodash/lodash.js"); + +exact(() => import.meta.resolve("node:path"), "node:path"); +exact(() => import.meta.resolve("path"), process?.versions?.bun ? "path" : "node:path"); +exact(() => import.meta.resolve("node:doesnotexist"), "node:doesnotexist"); + +if (process?.versions?.bun) { + exact(() => import.meta.resolve("bun:sqlite"), "bun:sqlite"); + exact(() => import.meta.resolve("bun:doesnotexist"), "bun:doesnotexist"); +} + +fileUrlRelTo(() => import.meta.resolve("./something.node"), "./something.node"); + +throws(() => import.meta.resolve("adsjfdasdf"), "nonexistant package"); +throws(() => import.meta.resolve(""), "empty specifier"); |