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.path); expect(await import.meta.resolve("./resolve.test.js", import.meta.path)).toBe( import.meta.path ); expect( // optional second param can be any path, including a dir await import.meta.resolve( "./bunjs-only-snippets/resolve.test.js", join(import.meta.path, "../") ) ).toBe(import.meta.path); // can be a package path expect( (await import.meta.resolve("react", import.meta.path)).length > 0 ).toBe(true); // file extensions are optional expect(await import.meta.resolve("./resolve.test")).toBe(import.meta.path); // works with tsconfig.json "paths" expect(await import.meta.resolve("foo/bar")).toBe( join(import.meta.path, "../baz.js") ); // works with package.json "exports" writePackageJSONExportsFixture(); expect(await import.meta.resolve("package-json-exports/baz")).toBe( join(import.meta.path, "../node_modules/package-json-exports/foo/bar.js") ); // works with TypeScript compiler 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.path, "../resolve-typescript-file.tsx") ); expect(await import.meta.resolve("./resolve-typescript-file.tsx")).toBe( join(import.meta.path, "../resolve-typescript-file.tsx") ); // throws a ResolveError on failure 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.path); 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.path ); }); // synchronous it("Bun.resolveSync", () => { expect(Bun.resolveSync("./resolve.test.js", import.meta.dir)).toBe( import.meta.path ); }); 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 ) ); } /bindings-mark-jscinitialize Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/headers.mjs (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2023-10-09fix(AbortSignal/fetch) fix AbortSignal.timeout, fetch lock behavior and fetch...Gravatar Ciro Spaciari 29-61/+303
2023-10-09Fix npm tag for canary bun-types, againGravatar Ashcon Partovi 2-56/+10
2023-10-09Add Fedora build instructions to development.md (#6359)Gravatar otterDeveloper 1-0/+10
2023-10-09added commands (#6314)Gravatar babar 1-1/+2
2023-10-09Update README.md (#6291)Gravatar TPLJ 1-1/+1
2023-10-09docs: fixing a couple typos (#6331)Gravatar Michael Di Prisco 2-2/+2
2023-10-09fix: support uint8 exit code range (#6303)Gravatar Liz 2-2/+11
2023-10-09Fix array variables preview in debugger (#6379)Gravatar 2hu 1-1/+4
2023-10-07feat(KeyObject) (#5940)Gravatar Ciro Spaciari 106-67/+9342
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+1
2023-10-07Exclude more filesGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-1/+2
2023-10-07Update settings.jsonGravatar Jarred Sumner 1-2/+3
2023-10-06fix a couple install testsGravatar Dylan Conway 1-8/+8
2023-10-06formatGravatar Dylan Conway 1-1/+2
2023-10-06Fix memory leak in fetch() (#6350)Gravatar Jarred Sumner 1-2/+0
2023-10-06[types] allow onLoad plugin callbacks to return undefined (#6346)Gravatar Silver 1-1/+1
2023-10-06docs: `file.stream()` is not a promise (#6337)Gravatar Paul Nodet 1-1/+1