Test isolation
File-level isolation
Each test file runs in its own module scope:Test-level isolation
Tests within the same file share the module scope:beforeEach to reset state:
Execution model
Sequential execution
By default, tests in a file run sequentially:Concurrent execution
Mark tests as concurrent:File-level concurrency
Run multiple test files in parallel:bunfig.toml:
Global state
Process environment
Environment variables are shared across all tests:beforeEach:
Global objects
Changes to global objects persist:Module caching
Modules are cached per test file:Resource management
Automatic cleanup with using
Bun supports JavaScript’susing keyword:
Manual cleanup
UseafterEach or afterAll for cleanup:
Error handling
Unhandled promise rejections
Bun catches unhandled rejections and fails the test:Uncaught exceptions
Uncaught exceptions fail the test:Async test completion
Async tests must complete:Memory management
Garbage collection
Bun runs garbage collection between test files:Memory leaks
Avoid keeping references in global scope:Performance characteristics
Test startup time
First test in a file includes:- Module loading
- Dependency resolution
- Global setup
Optimization tips
-
Minimize global imports:
-
Use lazy imports for heavy dependencies:
-
Share expensive setup with beforeAll: