Engineering
writing-bundler-tests avatar

writing-bundler-tests

Guides writing, debugging, and maintaining Bun bundler tests using itBundled and expectBundled to verify transpilation, minification, and code transformation.

Introduction

This skill provides a comprehensive framework for testing Bun's internal bundler, transpiler, and code transformation logic. It is designed for developers contributing to the Bun runtime who need to ensure that code bundling processes—including minification, tree-shaking, and module resolution—function correctly across various formats like ESM, CJS, and IIFE. By leveraging the itBundled utility, developers can systematically assert expectations on build outputs, runtime behavior, and error handling without setting up external infrastructure.

  • Facilitates verification of complex bundling features such as dead code elimination (DCE), minification (whitespace, identifiers, syntax), and advanced code manipulation like banner/footer injection.

  • Supports precise runtime assertions, including stdout/stderr pattern matching, environment variable injection, and exit code verification, ensuring the bundled output behaves as expected in Bun or Node.js environments.

  • Allows for granular bundle error and warning verification, enabling testers to define expected diagnostic output for specific file paths.

  • Enables post-bundle inspection using API hooks (onAfterBundle) to check for file existence, content validity via snapshots, and programmatic verification of transformation results.

  • Features robust capture mechanisms for exact transpilation verification, allowing users to embed capture() calls within source files to validate intermediate values or logic flows.

  • The primary entry point for tests is the test/bundler/ directory, utilizing the expectBundled.ts helper utility.

  • Input configuration involves defining virtual file structures, entry points, and specific bundler flags (target, format, sourceMap, splitting) within the itBundled test declaration.

  • Test files are organized by category, such as banner, minify, cjs, or splitting, allowing for efficient navigation and execution using specific filters like BUN_BUNDLER_TEST_FILTER.

  • Expected output matches include string literals, regex patterns, or complex snapshot testing for detailed structural validation.

  • Always use the dedent helper for maintaining readable, multi-line string content within test files to ensure proper formatting of test code.

Repository Stats

Stars
89,434
Forks
4,372
Open Issues
6,661
Language
Zig
Default Branch
main
Sync Status
Idle
Last Synced
Apr 29, 2026, 01:07 PM
View on GitHub