1

How can I fail a GitHub job if jest code coverage is not met?

I can see that my GitHub action goes into the success block, even though the code coverage is not met. How can I fail it?

I tried these SO answers, but did not have any luck

jest.ts:

module.exports = {
  projects: [
    {
        displayName: 'unit',
        testEnvironment: 'node',
        testPathIgnorePatterns: ['.d.ts', '.js'],
        roots: ['<rootDir>'],
        testMatch: ['<rootDir>/test/unit/**/*.test.ts'],
        transform: {
          '^.+\\.tsx?$': 'ts-jest',
        },
        clearMocks: true,
        collectCoverageFrom: [
          '**/*.ts',
          '!test/**/*',
        ],
        coverageThreshold: {
          global: {
            branches: 95,
            functions: 95,
            lines: 95,
            statements: 95,
          },
        },
    },
    ...
};

YAML:

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - name: Notify job starting...
        uses: ...
        if: success()
        id: slack
        with:
          channel: git
          status: STARTING_UNIT_TESTS
          color: warning

      - name: Use Node.js 16.x
        uses: actions/setup-node@v1
        with:
          node-version: 16.x

      - name: Install & test
        run: |
          npm ci
          npm run test-coverage
      - name: Send passing unit test metrics
        uses: ...
        if: success()
        with:
          api-key: ...

      - name: Send failing unit test metrics
        uses: ...
        if: failure()
        with:
          api-key: ...

Output:

...
PASS unit test/unit/xyz.test.ts
----------------------------------------|---------|----------|---------|---------|--------------------------------------
File                                    | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                    
----------------------------------------|---------|----------|---------|---------|--------------------------------------
All files                               |    48.7 |    14.03 |   38.68 |   50.08 |          

The jest coverage report plugin looks promising, but doesn't seem to support jest projects

Hoppe
  • 6,508
  • 17
  • 60
  • 114

1 Answers1

-1

Use a plugin like

  - name: Install
    run: npm ci

  - name: Jest unit coverage comment
    uses: ArtiomTr/jest-coverage-report-action@v2        
    with:
      test-script: npm run test-coverage

Set package.json to:

"scripts": {
  "test-coverage": "jest --selectProjects unit --coverage",

Set the jest.config.ts to the below. Note that if you are using jest projects, you must set the threshold at the top level of the JSON. It is not currently supported per project. If you are not using jest projects, you can skip all of the references to projects in this answer

module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    },
  },
Hoppe
  • 6,508
  • 17
  • 60
  • 114