MishaKav / jest-coverage-comment

Comments a pull request or commit with the jest code coverage badge, full report and tests summary
MIT License
80 stars 34 forks source link

Not reading my code coverage report files #99

Open ChrisMBarr opened 2 weeks ago

ChrisMBarr commented 2 weeks ago

I have a monorepo with a one NestJS server project using Jest for tests, and the others are front-end Angular apps using Jasmine/Karma for testing. My Jest tests work perfectly, and I am able to execute my Jasmine tests and generate the JUint and coverage JSON files, but I get this error

File read successfully "./webapp1/coverage/webapp1/coverage-final.json"
Error: Parse summary report. Cannot read properties of undefined (reading 'lines')

Please note that I am well aware that this project is specifically about Jest and not Jasmine/Karma, however it seems as though both frameworks can generate the same reporting files, so I'm not sure it matters too much.

Here is my karma.conf.js file for one of the web apps

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-junit-reporter'),
      require('karma-coverage'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    reporters: ['progress', 'kjhtml'],
    browsers: ['Chrome', 'ChromeHeadless'],
    client: {
      jasmine: { },
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true, // removes the duplicated traces
    },
    junitReporter: {
      useBrowserName: false,
      outputFile: 'junit.xml',
    },
    coverageReporter: {
      dir: require('path').join(__dirname, './coverage/webapp1'),
      subdir: '.',
      reporters: [{ type: 'html' }, { type: 'text-summary' }, { type: 'json' }],
    },
    failOnEmptyTestSuite: false,
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    singleRun: false,
    restartOnFileChange: true,
  });
};

And then the web apps are set up with this script in package.json "test-coverage-report": "ng test --no-watch --code-coverage --reporters=junit --browsers=ChromeHeadless"

When I run that script locally, it generates a junit.xml file and it generates a coverage-file.json file, which look correct to me

And then on GitHub I run all my tests like this

 - name: 🧪 Unit Tests - Server
    working-directory: server
    run: npx jest --coverage --coverageReporters=text --coverageReporters=json-summary --reporters=jest-junit --reporters=github-actions

  - name: 🧪 Unit Tests - webapp1
    working-directory: webapp1
    run: npm run test-coverage-report

  - name: 🧪 Unit Tests - webapp2
    working-directory: webapp2
    run: npm run test-coverage-report

  - name: 🧪 Unit Test Reporter
    uses: MishaKav/jest-coverage-comment@main
    if: ${{ always() }}
    with:
      title: Code Coverage Report
      multiple-files: |
        NestJS Server, ./server/coverage/coverage-summary.json
        Web App One, ./webapp1/coverage/webapp1/coverage-final.json
        Web App Two, ./webapp2/coverage/webapp2/coverage-final.json
      multiple-junitxml-files: |
        NestJS Server, ./server/junit.xml
        Web App One, ./webapp1/junit.xml
        Web App Two, ./webapp2/junit.xml

When I run this on github, this is what is produced:

Run MishaKav/jest-coverage-comment@main
  with:
    title: Code Coverage Report
    multiple-files: NestJS Server, ./server/coverage/coverage-summary.json
  Web App One, ./webapp1/coverage/webapp1/coverage-final.json
  Web App Two, ./webapp2/coverage/webapp2/coverage-final.json

    multiple-junitxml-files: NestJS Server, ./server/junit.xml
  Web App One, ./webapp1/junit.xml
  Web App Two, ./webapp2/junit.xml

    github-token: ***
    coverage-summary-path: ./coverage/coverage-summary.json
    badge-title: Coverage
    hide-summary: false
    hide-comment: false
    remove-links-to-files: false
    remove-links-to-lines: false
    create-new-comment: false
    coverage-title: Coverage Report
    report-only-changed-files: false
Uses Github URL: https://github.com
File read successfully "./server/coverage/coverage-summary.json"
NestJS Server
  coverage: [10](https://github.com/My-Company/my-project/actions/runs/123456?pr=434#step:15:10)0
  color: brightgreen
File read successfully "./webapp1/coverage/webapp1/coverage-final.json"
Error: Parse summary report. Cannot read properties of undefined (reading 'lines')
File read successfully "./webapp2/coverage/webapp2/coverage-final.json"
Error: Parse summary report. Cannot read properties of undefined (reading 'lines')

File read successfully "./server/junit.xml"
File read successfully "./webapp1/junit.xml"
File read successfully "./webapp2/junit.xml"

Found previous comment, updating

Since the output says it can read the file, but then it cannot read a lines property, my suspicion is that it it trying to read the coverageThreshhold.global.lines property of the jest.config.js file, which does not exist in these web app projects. Is there some way to tell it to read these simple percentage properties from a different file? Here is how karma sets those up: https://github.com/karma-runner/karma-coverage/blob/master/docs/configuration.md#check

MishaKav commented 1 week ago

I haven't worked with karma before, so it's hard to say if it should work or no. Need to look at the files that karma provide and compare their format to the jest format file.

Can you provide some example files that karma generates? If you can't provide files, which is fine, you can compare the files that you have with the current files format that this action is working: examples