Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213/** * Scenario 11 — Edge Cases, Errors, and Stress Tests * * Tests error handling, aliasing, cyclic dependencies, deep nesting, * VFS precedence, and stress scenarios with many transitive deps. * * @see docs/scenarios/11-edge-cases-and-errors.md * @module */
import { describe, test } from "@std/testing/bdd";import { expect } from "@std/expect";
import { resolveModern, getResolverConditions, manifest, importArgs, resolveOpts, buildPackage, buildWithEntry, getOutputText, outputContains,} from "./helpers.ts";
// =============================================================================// Unit tests — deep nesting and edge cases// =============================================================================
describe("11 · Edge Cases and Errors", () => { // --------------------------------------------------------------------------- // 11.14 — Very deep exports nesting // --------------------------------------------------------------------------- describe("11.14 — Very deep exports nesting", () => { const pkg = manifest({ exports: { ".": { browser: { production: { import: { default: "./dist/browser.prod.esm.js", }, }, default: "./dist/browser.esm.js", }, default: "./dist/index.js", }, }, });
test("resolves through 4 levels of nesting with matching conditions", () => { const conds = getResolverConditions( importArgs("import-statement"), resolveOpts({ platform: "browser", conditions: ["production"], }), ); const result = resolveModern(pkg, ".", conds);
// Should drill down: browser → production → import → default expect(result.path).toBe("./dist/browser.prod.esm.js"); });
test("stops at browser.default when production is not in conditions", () => { const conds = getResolverConditions( importArgs("import-statement"), resolveOpts({ platform: "browser" }), ); const result = resolveModern(pkg, ".", conds);
// Without "production" condition, falls to browser.default expect(result.path).toBe("./dist/browser.esm.js"); });
test("falls to top-level default when browser is not matched", () => { const conds = getResolverConditions( importArgs("import-statement"), resolveOpts({ platform: "node" }), ); const result = resolveModern(pkg, ".", conds);
expect(result.path).toBe("./dist/index.js"); }); });
// =========================================================================== // Integration tests — aliasing // ===========================================================================
describe("11.5 — AliasPlugin rewrites before resolution", () => { test("alias react → preact/compat uses preact (not React)", async () => { await using result = await buildWithEntry( `export { useState } from "react";`, { alias: { react: "preact@10.25.4/compat" }, }, );
const text = getOutputText(result); // The bundle should contain preact code, not React expect(result.contents.length).toBeGreaterThan(0); expect(result.errors.length).toBe(0); }); });
// =========================================================================== // Integration tests — error handling // ===========================================================================
describe("11.9 — Nonexistent package", () => { test("returns an error for a package that does not exist", async () => { try { await using result = await buildPackage("this-package-does-not-exist-12345"); // Should either throw or produce errors expect(result.errors.length).toBeGreaterThan(0); } catch (_e) { // Throwing is an acceptable error response expect(true).toBe(true); } }); });
describe("11.10 — Nonexistent version", () => { test("returns an error for a version that does not exist", async () => { try { await using result = await buildPackage("react@999.0.0"); expect(result.errors.length).toBeGreaterThan(0); } catch (_e) { expect(true).toBe(true); } }); });
// =========================================================================== // Integration tests — cyclic / peer dependencies // ===========================================================================
describe("11.7 — Peer dependency cycle", () => { test("react-dom@19.0.0 builds despite peer-depending on react", async () => { await using result = await buildPackage("react-dom@19.0.0");
// Should complete without infinite loops expect(result.contents.length).toBeGreaterThan(0); expect(result.errors.length).toBe(0); }); });
// =========================================================================== // Integration tests — special characters in package names // ===========================================================================
describe("11.15 — Scoped packages with special characters", () => { test("@anthropic-ai/sdk resolves correctly (scoped + hyphens)", async () => { // This tests that URL encoding handles @scope/name-with-hyphens await using result = await buildPackage("@anthropic-ai/sdk@0.39.0");
expect(result.contents.length).toBeGreaterThan(0); expect(result.errors.length).toBe(0); }); });
// =========================================================================== // Integration tests — VFS precedence // ===========================================================================
describe("11.16 — VFS file takes precedence over CDN", () => { test("entry file in VFS resolves from memory", async () => { await using result = await buildWithEntry( `export const hello = "world";`, );
// The entry file in VFS is the only content — should not hit CDN expect(result.errors.length).toBe(0); expect(outputContains(result, "world")).toBe(true); }); });
describe("11.17 — Relative imports between VFS files", () => { test("VFS-to-VFS relative imports resolve in memory", async () => { // buildWithEntry writes /index.tsx; we can test that the entry itself // resolves correctly without hitting the network. A full VFS-relative // test would require setFile for both files, but this validates // that VFS-based resolution does work for the entry point. await using result = await buildWithEntry( `const msg: string = "hello from VFS";\nexport { msg };`, );
expect(result.errors.length).toBe(0); expect(outputContains(result, "hello from VFS")).toBe(true); }); });
// =========================================================================== // Integration tests — multi-package queries // ===========================================================================
describe("11.13 — Multiple packages", () => { test("separate builds for multiple packages all succeed", async () => { // bundlejs resolves each package independently; here we verify // that multiple packages can be built in succession without // cross-contamination. await using r1 = await buildPackage("preact@10.25.4"); await using r2 = await buildPackage("chalk@5.4.1");
expect(r1.errors.length).toBe(0); expect(r2.errors.length).toBe(0); expect(r1.contents.length).toBeGreaterThan(0); expect(r2.contents.length).toBeGreaterThan(0); }); });});