|
| 1 | +name: tests |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "**" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "**" ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + test: |
| 11 | + name: tests |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout |
| 15 | + uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Setup Node.js |
| 18 | + uses: actions/setup-node@v4 |
| 19 | + with: |
| 20 | + node-version: 20 |
| 21 | + cache: npm |
| 22 | + |
| 23 | + - name: Install dependencies (lockfile) |
| 24 | + if: ${{ hashFiles('package-lock.json') != '' }} |
| 25 | + run: npm ci |
| 26 | + |
| 27 | + - name: Install dependencies |
| 28 | + if: ${{ hashFiles('package-lock.json') == '' }} |
| 29 | + run: npm install |
| 30 | + |
| 31 | + - name: Run tests with coverage |
| 32 | + run: npx jest --runInBand --coverage --coverageReporters=text-summary --coverageReporters=lcov --coverageReporters=json-summary |
| 33 | + |
| 34 | + - name: Upload coverage artifact |
| 35 | + if: always() && hashFiles('coverage/coverage-summary.json') != '' |
| 36 | + uses: actions/upload-artifact@v4 |
| 37 | + with: |
| 38 | + name: coverage |
| 39 | + path: coverage/** |
| 40 | + |
| 41 | + - name: Add coverage summary to job summary |
| 42 | + if: always() && hashFiles('coverage/coverage-summary.json') != '' |
| 43 | + shell: bash |
| 44 | + run: | |
| 45 | + node <<'NODE' |
| 46 | + const fs = require('fs'); |
| 47 | + const p = 'coverage/coverage-summary.json'; |
| 48 | + const out = process.env.GITHUB_STEP_SUMMARY; |
| 49 | + if (!out) { |
| 50 | + console.log('GITHUB_STEP_SUMMARY not set; skipping'); |
| 51 | + process.exit(0); |
| 52 | + } |
| 53 | + const s = JSON.parse(fs.readFileSync(p, 'utf8')); |
| 54 | + const c = s.total; |
| 55 | + const lines = [ |
| 56 | + '## Coverage summary', |
| 57 | + '', |
| 58 | + `- Statements: ${c.statements.pct}% (${c.statements.covered}/${c.statements.total})`, |
| 59 | + `- Branches: ${c.branches.pct}% (${c.branches.covered}/${c.branches.total})`, |
| 60 | + `- Functions: ${c.functions.pct}% (${c.functions.covered}/${c.functions.total})`, |
| 61 | + `- Lines: ${c.lines.pct}% (${c.lines.covered}/${c.lines.total})`, |
| 62 | + '', |
| 63 | + ]; |
| 64 | + fs.appendFileSync(out, lines.join('\n')); |
| 65 | + NODE |
0 commit comments