-1

I have a next.js project with a husky hook running my jest test suite. I've set the code coverage requirement to be 95% code coverage however the test suite finishes running and commits anyway. I can't get jest to return an error code even though the documentation says jest should fail. I was hoping jest would return a fail code and stop the commit from occurring.

jest.config.js:

const nextJest = require('next/jest');

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './',
});

// Add any custom config to be passed to Jest
const customJestConfig = {
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleNameMapper: {
    // Handle module aliases (this will be automatically configured for you soon)
    '^@/components/(.*)$': '<rootDir>/src/components/$1',
    '^@/lib/(.*)$': '<rootDir>/src/lib/$1',
    '^@/core/(.*)$': '<rootDir>/src/core/$1',
    '^@/pages/(.*)$': '<rootDir>/src/pages/$1',
    '^@/public/(.*)$': '<rootDir>/public/$1',
  },
  testEnvironment: 'jest-environment-jsdom',
  moduleDirectories: ['node_modules', 'src'],
  verbose: true,
  collectCoverage: true,
  collectCoverageFrom: ['<rootDir>/src/pages/**/[^_]*.tsx'],
  coverageThreshold: {
    '<rootDir>/src/pages/**/*.tsx': {
      lines: 95,
    },
  },
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

package.json:

{
  "name": "next-gen",
  "version": "1.0.0",
  "scripts": {
    "dev": "next dev -p 7000",
    "build": "next build",
    "start": "next start",
    "build-stats": "cross-env ANALYZE=true npm run build",
    "export": "next export",
    "build-prod": "run-s clean build export",
    "clean": "rimraf .next out",
    "lint": "next lint",
    "build-types": "tsc --noEmit --pretty",
    "prepare": "husky install",
    "test": "jest",
    "coverage": "npm run test -- --coverage --watchAll=false || exit 0"
  },
  "dependencies": {
    "@next/bundle-analyzer": "^12.1.0",
    "cors": "^2.8.5",
    "next": "^12.1.0",
    "next-seo": "^5.1.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "styled-jsx-plugin-postcss": "^4.0.1"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.16.3",
    "@testing-library/react": "^12.1.4",
    "@testing-library/user-event": "^13.5.0",
    "@types/node": "^17.0.18",
    "@types/react": "^17.0.39",
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "autoprefixer": "^10.4.2",
    "cross-env": "^7.0.3",
    "cssnano": "^5.0.17",
    "eslint": "^8.9.0",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-config-airbnb-typescript": "^16.1.0",
    "eslint-config-next": "^12.1.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-jsx-a11y": "^6.5.1",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.28.0",
    "eslint-plugin-react-hooks": "^4.3.0",
    "eslint-plugin-tailwindcss": "^3.4.4",
    "eslint-plugin-unused-imports": "^2.0.0",
    "husky": "^4.3.8",
    "jest": "^27.5.1",
    "lint-staged": "^12.3.4",
    "npm-run-all": "^4.1.5",
    "postcss": "^8.4.6",
    "prettier": "^2.5.1",
    "rimraf": "^3.0.2",
    "tailwindcss": "^3.0.23",
    "typescript": "^4.5.5"
  },
  "license": "UNLICENSED",
  "private": true,
  "husky": {
    "hooks": {
      "pre-commit": "npm run coverage"
    }
  }
}

jest output when committing:

[2022-03-29T04:34:27.805Z] husky > pre-commit (node v16.9.1)

> next-gen@1.0.0 coverage
> npm run test -- --coverage --watchAll=false || exit 0


> next-gen@1.0.0 test
> jest "--coverage" "--watchAll=false"

  console.error
    Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>.
        at option
        at select
        at div
        at div
        at form
        at div
        at div
        at div
        at div
        at div
        at Main (C:\Users\ahmefa04\Documents\SourceCode\temp-cd-next\src\components\templates\Main.tsx:14:12)
        at Index

      at printWarning (node_modules/react-dom/cjs/react-dom.development.js:67:30)
      at error (node_modules/react-dom/cjs/react-dom.development.js:43:5)
      at validateProps (node_modules/react-dom/cjs/react-dom.development.js:1781:7)
      at setInitialProperties (node_modules/react-dom/cjs/react-dom.development.js:9108:7)
      at finalizeInitialChildren (node_modules/react-dom/cjs/react-dom.development.js:10201:3)
      at completeWork (node_modules/react-dom/cjs/react-dom.development.js:19470:17)
      at completeUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22812:16)

PASS src/__tests__/index.test.tsx
  Index
    √ renders element (74 ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |   57.14 |      100 |     100 |   57.14 |                   
 fcra.tsx  |       0 |      100 |     100 |       0 | 1-10              
 index.tsx |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------
Jest: Coverage data for <rootDir>/src/pages/**/*.tsx was not found.
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.688 s
Ran all test suites.
Faahmed
  • 375
  • 4
  • 21
  • 2
    You explicitly suppress the non-zero exit _in the coverage script_. If you don't want that behaviour, have you considered **not** doing that? – jonrsharpe Mar 29 '22 at 07:42
  • That did it! Thank you for showing me my misguidance. I didn't have that originally but other things were broken back then and I forgot about it. – Faahmed Mar 29 '22 at 09:36

1 Answers1

0

Thanks to jonrsharpe, made me realize the mistake.

I left the " || exit 0" after test script suppressing the error from Jest.

Removing that fixed the issue.

Faahmed
  • 375
  • 4
  • 21