Skip to content

Hiccup-za/github_actions_playground

Repository files navigation

GitHub Actions Playground

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.

Project Structure

  • 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.

Features

  • 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)

How the Mock Works

The mock in this project operates "offline" without spinning up a local server:

  1. Mock Setup: In mockSetup.ts, we use Bun's mock function to replace the global fetch function with our own implementation.
  2. In-Memory Storage: The mock uses an in-memory array (mockObjects) to store and manipulate data.
  3. 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
  4. Response Simulation: The mock creates Response objects to simulate HTTP responses, including status codes and JSON bodies.
  5. 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.
  6. Cleanup: After tests, teardownMockFetch restores the original fetch function and clears the mock data.

This approach allows for fast, isolated testing without the need for a real server or network connectivity.

Test Scenarios

The tests in index.test.ts demonstrate the following scenarios:

  1. API E2E Test: Full lifecycle of object creation, retrieval, and deletion.
  2. POST - empty name test: Handling of invalid input (empty name).
  3. GET by ID - non-existent ID test: Handling of requests for non-existent resources.
  4. DELETE by ID - non-existent ID test: Handling of deletion requests for non-existent resources.

Running the Project

To run the server:

bun start

Running Tests

To run the tests, make sure you have Bun installed, then run:

bun test

Development

To extend this project:

  1. Add new endpoints in api.fixture.ts
  2. Implement new mock responses in mockSetup.ts
  3. 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.

GitHub Actions

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.

Dependencies

  • Bun (for running TypeScript and tests)
  • TypeScript
  • figlet (for server welcome message)
  • @happy-dom/global-registrator (for DOM environment in tests)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published