This project demonstrates a simple API testing setup using Bun and mocked API endpoints. It's designed to showcase how to structure tests, use fixtures, and mock API responses in a TypeScript environment.
Most of the code was generated by Cursor using the Claude 3.5 Sonnet model.
index.ts
: Main server file that runs a simple Bun server.index.test.ts
: Contains the main test suite demonstrating various API request scenarios.api.fixture.ts
: Defines API endpoints, mock responses, and utility functions.mockSetup.ts
: Sets up the mock fetch function for API testing..github/workflows/test.yml
: GitHub Actions workflow for running tests.happydom.ts
: Sets up the DOM environment for testing.bunfig.toml
: Configuration file for Bun.package.json
: Project dependencies and scripts.
- Mocked API endpoints for testing without a real backend
- Simulated API request chains (GET, POST, DELETE)
- UUID generation for mock object IDs
- Use of Bun's test runner and assertions
- Separation of concerns: mock setup is isolated from test files
- Error handling for edge cases (e.g., empty names, non-existent IDs)
The mock in this project operates "offline" without spinning up a local server:
- Mock Setup: In
mockSetup.ts
, we use Bun'smock
function to replace the globalfetch
function with our own implementation. - In-Memory Storage: The mock uses an in-memory array (
mockObjects
) to store and manipulate data. - Request Handling: When a test makes a
fetch
call, it's intercepted by our mock implementation. The mock then:- Parses the URL and method
- Performs the appropriate action based on the request (GET, POST, DELETE)
- Manipulates the
mockObjects
array as needed - Returns a mocked
Response
object
- Response Simulation: The mock creates
Response
objects to simulate HTTP responses, including status codes and JSON bodies. - Offline Operation: All of this happens in memory, without any network requests or server involvement. The tests interact with this mock as if it were a real API, but everything is contained within the test environment.
- Cleanup: After tests,
teardownMockFetch
restores the originalfetch
function and clears the mock data.
This approach allows for fast, isolated testing without the need for a real server or network connectivity.
The tests in index.test.ts
demonstrate the following scenarios:
- API E2E Test: Full lifecycle of object creation, retrieval, and deletion.
- POST - empty name test: Handling of invalid input (empty name).
- GET by ID - non-existent ID test: Handling of requests for non-existent resources.
- DELETE by ID - non-existent ID test: Handling of deletion requests for non-existent resources.
To run the server:
bun start
To run the tests, make sure you have Bun installed, then run:
bun test
To extend this project:
- Add new endpoints in
api.fixture.ts
- Implement new mock responses in
mockSetup.ts
- Write additional tests in
index.test.ts
or create new test files as needed
When creating new test files, remember to import and use the setupMockFetch
and teardownMockFetch
functions from mockSetup.ts
.
The project uses GitHub Actions to automatically run tests on push or pull request to the main branch. Check .github/workflows/test.yml
for the workflow configuration.
- Bun (for running TypeScript and tests)
- TypeScript
- figlet (for server welcome message)
- @happy-dom/global-registrator (for DOM environment in tests)