Standardize testing by leveraging established patterns and helper functions throughout your test suite. Extract repetitive test setup into lifecycle hooks like `beforeEach` or shared fixtures. Use existing test helpers rather than duplicating logic, and organize tests logically by functionality.
Standardize testing by leveraging established patterns and helper functions throughout your test suite. Extract repetitive test setup into lifecycle hooks like beforeEach
or shared fixtures. Use existing test helpers rather than duplicating logic, and organize tests logically by functionality.
For example, instead of repeating server setup in multiple tests:
test("test case 1", async () => {
using server = Bun.serve({
port: 0,
async fetch(req, res) { /* ... */ },
});
// test logic
});
test("test case 2", async () => {
using server = Bun.serve({
port: 0,
async fetch(req, res) { /* ... */ },
});
// test logic
});
Prefer using lifecycle hooks:
describe("server tests", () => {
let server;
beforeEach(() => {
server = Bun.serve({
port: 0,
async fetch(req, res) { /* ... */ },
});
});
afterEach(() => {
server.stop();
});
test("test case 1", async () => {
// test logic using server
});
test("test case 2", async () => {
// test logic using server
});
});
Similarly, use established helper functions (like fixtureURL
, itBundled
) for resource loading and specific testing scenarios. Group related tests into logical units and place tests in files that match their functionality. This approach reduces code duplication, improves maintainability, and makes tests easier to understand.
Enter the URL of a public GitHub repository