-
Notifications
You must be signed in to change notification settings - Fork 2
80 lines (66 loc) · 2.86 KB
/
branch-push.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 1: Checkout the code
- name: Checkout repository
uses: actions/checkout@v4
# Step 2: Set up Node.js with the required version
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
# Step 3: Install dependencies
- name: Install dependencies
run: npm install
# Step 4: Run Jest tests
- name: Run Jest tests
run: npm test -- --coverage
env:
CI: true # Ensures Jest runs in CI mode
# Step 5: Upload test coverage results (optional, if you want to store it in the workflow run artifacts)
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage/lcov-report/ # Path to the HTML coverage report
retention-days: 7
# Step 6: Fail the job if there are uncovered files (js, ts, jsx, tsx)
- name: Check uncovered files
run: |
# Step 1: Fetch the origin/main branch to compare the HEAD with
git fetch origin main
# Step 2: 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 3: 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 4: 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