diff options
author | 2022-03-17 19:18:08 -0700 | |
---|---|---|
committer | 2022-03-17 19:18:08 -0700 | |
commit | 881a3b28a7b28475a06f8508c6ff2ab375275dba (patch) | |
tree | 47812d91bf44cf2bdcb4e3751490a94a5497d268 /integration/bunjs-only-snippets/resolve.test.js | |
parent | b3f4f8db24b6f9e0fc5e88eec08174df466672a1 (diff) | |
download | bun-881a3b28a7b28475a06f8508c6ff2ab375275dba.tar.gz bun-881a3b28a7b28475a06f8508c6ff2ab375275dba.tar.zst bun-881a3b28a7b28475a06f8508c6ff2ab375275dba.zip |
[bun.js] Implement `import.meta.resolve`
Diffstat (limited to 'integration/bunjs-only-snippets/resolve.test.js')
-rw-r--r-- | integration/bunjs-only-snippets/resolve.test.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/resolve.test.js b/integration/bunjs-only-snippets/resolve.test.js new file mode 100644 index 000000000..d5d14dfef --- /dev/null +++ b/integration/bunjs-only-snippets/resolve.test.js @@ -0,0 +1,106 @@ +import { it, expect } from "bun:test"; +import { mkdirSync, writeFileSync } from "fs"; +import { join } from "path"; + +it("import.meta.resolve", async () => { + expect(await import.meta.resolve("./resolve.test.js")).toBe(import.meta.url); + + expect(await import.meta.resolve("./resolve.test.js", import.meta.url)).toBe( + import.meta.url + ); + + expect( + // optional second param can be any path, including a dir + await import.meta.resolve( + "./bunjs-only-snippets/resolve.test.js", + join(import.meta.url, "../") + ) + ).toBe(import.meta.url); + + // can be a package path + expect((await import.meta.resolve("react", import.meta.url)).length > 0).toBe( + true + ); + + // file extensions are optional + expect(await import.meta.resolve("./resolve.test")).toBe(import.meta.url); + + // works with tsconfig.json "paths" + expect(await import.meta.resolve("foo/bar")).toBe( + join(import.meta.url, "../baz.js") + ); + + // works with package.json "exports" + writePackageJSONExportsFixture(); + expect(await import.meta.resolve("package-json-exports/baz")).toBe( + join(import.meta.url, "../node_modules/package-json-exports/foo/bar.js") + ); + + expect(await import.meta.resolve("./resolve-typescript-file.tsx")).toBe( + join(import.meta.url, "../resolve-typescript-file.tsx") + ); + expect(await import.meta.resolve("./resolve-typescript-file.js")).toBe( + join(import.meta.url, "../resolve-typescript-file.tsx") + ); + + // works with typescript edgecases like: + // - If the file ends with .js and it doesn't exist, try again with .ts and .tsx + expect(await import.meta.resolve("./resolve-typescript-file.js")).toBe( + join(import.meta.url, "../resolve-typescript-file.tsx") + ); + expect(await import.meta.resolve("./resolve-typescript-file.tsx")).toBe( + join(import.meta.url, "../resolve-typescript-file.tsx") + ); + + try { + await import.meta.resolve("THIS FILE DOESNT EXIST"); + throw new Error("Test failed"); + } catch (exception) { + expect(exception instanceof ResolveError).toBe(true); + expect(exception.referrer).toBe(import.meta.url); + expect(exception.name).toBe("ResolveError"); + } +}); + +// the slightly lower level API, which doesn't prefill the second param +// and expects a directory instead of a filepath +it("Bun.resolve", async () => { + expect(await Bun.resolve("./resolve.test.js", import.meta.dir)).toBe( + import.meta.url + ); +}); + +// synchronous +it("Bun.resolveSync", () => { + expect(Bun.resolveSync("./resolve.test.js", import.meta.dir)).toBe( + import.meta.url + ); +}); + +function writePackageJSONExportsFixture() { + try { + mkdirSync( + join(import.meta.dir, "./node_modules/package-json-exports/foo"), + { + recursive: true, + } + ); + } catch (exception) {} + writeFileSync( + join(import.meta.dir, "./node_modules/package-json-exports/foo/bar.js"), + "export const bar = 1;" + ); + writeFileSync( + join(import.meta.dir, "./node_modules/package-json-exports/package.json"), + JSON.stringify( + { + name: "package-json-exports", + exports: { + "./baz": "./foo/bar.js", + }, + }, + null, + 2 + ) + ); +} |