Hone logo
Hone
Problems

Automating Testing with CI: Integrating Jest into GitHub Actions

This challenge focuses on setting up Continuous Integration (CI) using GitHub Actions to automatically run Jest tests whenever changes are pushed to a TypeScript project. Automating testing is crucial for maintaining code quality and preventing regressions, and GitHub Actions provides a flexible and powerful platform for this. You'll configure a workflow that checks out the code, installs dependencies, and executes your Jest tests, reporting the results directly in your GitHub repository.

Problem Description

You are tasked with creating a GitHub Actions workflow file (.github/workflows/main.yml) that automates the execution of Jest tests for a TypeScript project. The workflow should:

  1. Checkout Code: Retrieve the latest code from the repository.
  2. Setup Node.js: Install a specific version of Node.js. Use Node.js version 18.
  3. Install Dependencies: Install the project's dependencies using npm install or yarn install (choose one and be consistent).
  4. Run Tests: Execute the Jest test suite using npm test or yarn test (corresponding to the package manager chosen).
  5. Report Results: Ensure that the test results are visible in the GitHub Actions workflow run summary. This typically involves Jest generating a report that GitHub Actions can interpret.

Key Requirements:

  • The workflow should run on every push to the main branch.
  • The workflow should fail if any tests fail.
  • The workflow should use a reasonable Node.js version (18).
  • The workflow file should be named main.yml and located in the .github/workflows directory.
  • The project already has a package.json file with a test script that runs Jest (e.g., "test": "jest").
  • The project has a jest.config.js or similar Jest configuration file.

Expected Behavior:

When a push occurs to the main branch, the GitHub Actions workflow will:

  1. Start a new run.
  2. Checkout the code.
  3. Install Node.js 18.
  4. Install project dependencies.
  5. Run the Jest test suite.
  6. Display the test results in the GitHub Actions interface.
  7. Mark the workflow run as successful if all tests pass, or failed if any tests fail.

Edge Cases to Consider:

  • The project might use yarn instead of npm. The workflow should be adaptable to either.
  • The test script in package.json might have additional arguments. The workflow should pass those arguments through.
  • The project might have a custom Jest reporter that generates a specific output format. Ensure the workflow correctly interprets this format.

Examples

Example 1:

Input: A TypeScript project with a package.json containing:
{
  "name": "my-typescript-project",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}
and a .github/workflows/main.yml file (initial state - empty)

Output: A .github/workflows/main.yml file that successfully checks out the code, installs dependencies, and runs the tests, reporting the results in the GitHub Actions interface.  The workflow run status will be "success" if all tests pass.

Explanation: The workflow will execute the "test" script defined in package.json, which runs Jest.

Example 2:

Input: A TypeScript project using yarn with a package.json containing:
{
  "name": "my-typescript-project",
  "version": "1.0.0",
  "scripts": {
    "test": "yarn test"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}
and a .github/workflows/main.yml file (initial state - empty)

Output: A .github/workflows/main.yml file that successfully checks out the code, installs dependencies using yarn, and runs the tests, reporting the results in the GitHub Actions interface. The workflow run status will be "success" if all tests pass.

Explanation: The workflow will execute the "test" script defined in package.json, which runs `yarn test`.

Constraints

  • The workflow file must be named main.yml.
  • The workflow must run on pushes to the main branch.
  • Node.js version 18 must be used.
  • The workflow must use either npm or yarn consistently.
  • The workflow must correctly execute the test script defined in package.json.
  • The workflow must fail if any tests fail.

Notes

  • Consider using the actions/checkout@v3 action to checkout the code.
  • Use the actions/setup-node@v4 action to set up Node.js.
  • The test script in package.json is assumed to be correctly configured to run Jest.
  • You can find more information about GitHub Actions and Jest integration in the GitHub Actions documentation and the Jest documentation.
  • Focus on the core functionality of running the tests and reporting the results. Advanced features like code coverage reporting are beyond the scope of this challenge.
  • The goal is to create a functional and reliable CI pipeline for your Jest tests.
Loading editor...
typescript