1.9 KiB
1.9 KiB
Bun Guidelines
CRITICAL: Do not assume you know full Bun APIs. For ANY Bun API you use, confirm them by using bun-docs MCP tools.
Default to using Bun instead of Node.js.
- Use
bun <file>instead ofnode <file>orts-node <file> - Use
bun testinstead ofjestorvitest - Use
bun build <file.html|file.ts|file.css>instead ofwebpackoresbuild - Use
bun installinstead ofnpm installoryarn installorpnpm install - Use
bun run <script>instead ofnpm run <script>oryarn run <script>orpnpm run <script> - Use
bunx <package> <command>instead ofnpx <package> <command> - Bun automatically loads .env, so don't use dotenv.
APIs
Bun.serve()supports WebSockets, HTTPS, and routes. Don't useexpress.bun:sqlitefor SQLite. Don't usebetter-sqlite3.Bun.redisfor Redis. Don't useioredis.Bun.sqlfor Postgres. Don't usepgorpostgres.js.WebSocketis built-in. Don't usews.- Prefer
Bun.fileovernode:fs's readFile/writeFile - Bun.$
lsinstead of execa.
Testing
Quick Start
- Run tests:
bun test - Write tests in
tests/folder
Test Structure
- Use
describeblocks to group related tests - Use
testfor individual test cases - Use
beforeEach/afterEachfor setup/teardown
Assertions
- Import:
import { test, expect, describe, beforeEach, afterEach, mock } from "bun:test"; - Common:
expect(value).toBe(expected),expect(fn).rejects.toThrow() - Async:
await expect(asyncFn()).resolves.toBe(expected)
Mocking
- Mock functions:
mock(fn) - Mock globals:
global.fetch = mock(...) - Restore mocks in
afterEachorfinally
Best Practices
- Mock external APIs (fetch, file I/O)
- Test error cases and edge conditions
- Use descriptive test names
- Clean up resources in
afterEach
For more information, read the Bun API docs in node_modules/bun-types/docs/**.mdx.