mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
## Summary This PR adds a `--randomize` flag to `bun test` that shuffles test execution order. This helps developers catch test interdependencies and identify flaky tests that may depend on execution order. ## Changes - ✨ Added `--randomize` CLI flag to test command - 🔀 Implemented test shuffling using `bun.fastRandom()` as PRNG seed - 🧪 Added comprehensive tests to verify randomization behavior - 📝 Tests are shuffled at the scheduling phase, properly handling describe blocks and hooks ## Usage ```bash # Run tests in random order bun test --randomize # Works with other test flags bun test --randomize --bail bun test mytest.test.ts --randomize ``` ## Implementation Details The randomization happens in `Order.zig`'s `generateOrderDescribe` function, which shuffles the `current.entries.items` array when the randomize flag is set. This ensures: - All tests still run (just in different order) - Hooks (beforeAll, afterAll, beforeEach, afterEach) maintain proper relationships - Describe blocks and their children are shuffled independently - Each run uses a different random seed for varied execution orders ## Test Coverage Added tests in `test/cli/test/test-randomize.test.ts` that verify: - Tests run in random order with the flag - All tests execute (none are skipped) - Without the flag, tests run in consistent order - Randomization works with describe blocks ## Example Output ```bash # Without --randomize (consistent order) $ bun test mytest.js Running test 1 Running test 2 Running test 3 Running test 4 Running test 5 # With --randomize (different order each run) $ bun test mytest.js --randomize Running test 3 Running test 5 Running test 1 Running test 4 Running test 2 $ bun test mytest.js --randomize Running test 2 Running test 4 Running test 5 Running test 1 Running test 3 ``` 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: pfg <pfg@pfg.pw>