Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git workflow: Check missing tests #105

Open
devbysp opened this issue Sep 27, 2024 · 3 comments
Open

Git workflow: Check missing tests #105

devbysp opened this issue Sep 27, 2024 · 3 comments
Assignees
Labels
technical Has only technical and no business requirements

Comments

@devbysp
Copy link
Contributor

devbysp commented Sep 27, 2024

Description

The git workflow checks only coverage for files for which a test has already been written.
Files which have been changed but there are no test at all will not considered in the test coverage.

Acceptance criteria

  • The workflow hes been extended with new steps which fails if a file was changed but there is no test created for it.
  • All the files are listed which do not fulfill this condition.
@devbysp devbysp self-assigned this Sep 27, 2024
@devbysp
Copy link
Contributor Author

devbysp commented Sep 27, 2024

Some work has already been done.

.github/workflows/branch-push.yaml

name: CI pipeline

on:
  pull_request:
    branches:
      - "**"  # Runs on all pull requests

jobs:
  check-test-coverage:
    # The coverage is configured inside 'jest.config.js' by setting the 'coverageThreshold'
    name: Checking test coverage (must be over 90%)
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [ 20.17 ]  # Define Node.js latest LTS version (update as needed)

    steps:
     ...

      # Step 6: Make 'check-missing-tests.sh' executable
      - name: Make script 'check-missing-tests.sh' executable
        run: chmod +x ./check-missing-tests.sh

      # Step 7: Fail the job if there are uncovered files (js, ts, jsx, tsx)
      - name: Check missing tests
        run: ./check-missing-tests.sh

check-missing-tests.sh

#!/bin/bash
git fetch origin main

# Step 1: Get the list of changed JS, TS, JSX and TSX files in the src directory
CHANGED_FILES=$(git diff --name-only origin/main..HEAD -- 'src/*.[jt]s' 'src/*.[jt]sx')

# Initialize an empty array to hold files without tests
MISSING_TEST_FILES=()

# Step 2: Loop through each changed file and check if it has a corresponding test file
for FILE in $CHANGED_FILES; do
  # Extract the file path and replace "src/" with "tests/" and add ".test" postfix
  TEST_FILE=$(echo "$FILE" | sed 's|^src/|tests/|' | sed 's|\.[jt]sx\?$|.test&|')

  # Check if the corresponding test file exists
  if [[ ! -f "$TEST_FILE" ]]; then
    MISSING_TEST_FILES+=("$FILE") # Add to missing test files array if test is not found
  fi
done

# Step 3: Report and fail if there are any missing test files
if [ ${#MISSING_TEST_FILES[@]} -ne 0 ]; then
  echo "The following source files do not have corresponding test files:"
  for MISSING_FILE in "${MISSING_TEST_FILES[@]}"; do
    echo "$MISSING_FILE"
  done
  exit 1 # Fail the workflow
else
  echo "All changed files have corresponding test files."
  exit 0 # Success
fi

image

@devbysp devbysp changed the title Check missing tests Git workflow: Check missing tests Sep 27, 2024
@devbysp
Copy link
Contributor Author

devbysp commented Sep 30, 2024

Pull Request

Description

  • Test coverage by itself does not detect if there is not a single test for a file and does not include it in the test coverage statistics.
  • This extra step in the Git workflow ensures that there is at least one test for every modified file.

@devbysp
Copy link
Contributor Author

devbysp commented Oct 11, 2024

Pull Request 2-3

Fixed issues

image

  • The npm run lint -- --fix did not do fix any failing rules described in the .eslintrc.json
  • There are two package.json scripts to detect and fix issues
    • npm run lint and npm run lint:fix
  • The git workflow step where missing tests were detected was wrong, because required tests for deleted files too.

@devbysp devbysp added the technical Has only technical and no business requirements label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical Has only technical and no business requirements
Projects
None yet
Development

No branches or pull requests

2 participants