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

Dynamic multiple-files issue #87

Closed SirPhemmiey closed 5 months ago

SirPhemmiey commented 5 months ago

It's sortof a duplicate to this but the answer there didn't help me.

here's a snippet of my workflow

- name: Prepare coverage input
   id: prepare-coverage-input
    run: |
        input=""
         for dir in coverage-report/*/*/coverage-summary.json; do
          appname=$(basename $(dirname $(dirname $dir)))
          input="${input}${appname}, ${dir}\n"
          done
          echo "${input}" > coverage-input.txt

  - name: Show coverage input content
     run: cat coverage-input.txt

 - name: Read coverage-inputs.txt
    id: multiplefiles
    uses: jaywcjlove/github-action-read-file@main
    with:
      path: coverage-input.txt

- name: Jest Coverage Comment
   uses: MishaKav/jest-coverage-comment@main
    if: github.event_name == 'pull_request'
    with:
      multiple-files: ${{ steps.multiplefiles.outputs.content }}

I am getting this error when i am not even passing /coverage/coverage-summary.json:

Screenshot 2024-05-14 at 17 06 51

This is also the content of coverage-inputs.txt to show that it's not empty

Screenshot 2024-05-14 at 17 08 37

Looking forward to your help on this, thanks!

SirPhemmiey commented 5 months ago

@MishaKav

MishaKav commented 5 months ago

From what I see in the screenshot, your file contains \n as text and not as a break-line. Try to play with your file generation.

SirPhemmiey commented 5 months ago

Hi @MishaKav, i fixed the format and it's still the same

Screenshot 2024-05-15 at 11 09 20
MishaKav commented 5 months ago

The paths you provide start with "coverage-report/...", as you can see in #72 the paths should be relative or be absolute or start with "./coverage-report/..." or similar. Please try this.

SirPhemmiey commented 5 months ago

Happy to say it worked when i used another file reader github action.

Here's the full working version (without formatting the YAML) for anyone having the same issue

- name: Prepare coverage input
        id: prepare-coverage-input
        run: |
          input=""
          for dir in coverage-report/*/*/coverage-summary.json; do
            appname=$(basename $(dirname $(dirname $dir)))
            input="${input}${appname}, ${dir}\n"
          done
          printf "${input}" > coverage-input.txt

      - name: Read coverage-inputs.txt
        id: multiplefiles
        uses: juliangruber/read-file-action@v1
        with:
          path: coverage-input.txt

      - name: Jest Coverage Comment
        uses: MishaKav/jest-coverage-comment@main
        if: github.event_name == 'pull_request'
        with:
          title: Test Coverage Summary
          summary-title: Summary
          badge-title: Coverage
          hide-comment: false
          create-new-comment: false
          hide-summary: false
          coverage-title: Coverage Analysis
          coverage-path: coverage-input.txt
          multiple-files: ${{ steps.multiplefiles.outputs.content }}
SirPhemmiey commented 5 months ago

Thank you @MishaKav !

MishaKav commented 5 months ago

Thank you for sharing the whole solution.

SirPhemmiey commented 5 months ago

Hi @MishaKav, quick question.

When using the multiple files approach can i still put a value to coverage-path? if yes, what should the value be?

MishaKav commented 5 months ago

Yes, you can using multiple-files with coverage-path. In the main readme, you have an example. It can be something like this:

- name: Run Tests
  run: npm run coverage | tee ./coverage.txt && exit ${PIPESTATUS[0]}

- name: Jest Coverage Comment
  uses: MishaKav/jest-coverage-comment@main
  with:
    coverage-path: ./coverage.txt
    multiple-files: |
      My-Title-1, ./coverage/coverage-summary.json
      My-Title-2, ./coverage/coverage-summary.json

The result will be as in the screenshot below:

image
SirPhemmiey commented 5 months ago

@MishaKav What's the expected content of coverage.txt? When i run a command similar to npm run coverage, it emits/outputs other things because i'm using nx monorepo.

MishaKav commented 5 months ago

it's the output of jest itself. Here you can see the whole example with all commands and output https://github.com/MishaKav/api-testing-example/actions/runs/9108937444/job/25040742353

SirPhemmiey commented 5 months ago

@MishaKav i see. I think you should add it in the docs that the jest.config.ts should have the coverage reporters: ['json-summary', 'text', 'text-summary']. Not everyone would usually use those coverage reporters

MishaKav commented 5 months ago

It's not exactly the truth. Specific in your case you may use only ['text'] to achieve what you want.