Jest.js 在github操作中,使用jq只打印json-summary文件中的覆盖率百分比

nhn9ugyo  于 2023-01-28  发布在  Jest
关注(0)|答案(1)|浏览(124)

我想把json-coverage jest的结果漂亮地打印到github actions中。我有一个ci.yml文件,包含以下内容:

- name: Run Unit Tests
        run: |
          yarn test:next:unit --runInBand --coverage --coverageReporters="text-summary"
      - name: Echo Coverage
        run: |
          cat coverage/coverage-summary.json | head -1 > coverage-testing/coverage.json

这将在作业终端中打印jest的coverage-summary的第一行,如下所示:

{"total": {"lines":{"total":18326,"covered":11,"skipped":0,"pct":0.06},"statements":{"total":62625,"covered":11,"skipped":0,"pct":0.01},"functions":{"total":19047,"covered":4,"skipped":0,"pct":0.02},"branches":{"total":60202,"covered":11,"skipped":0,"pct":0.01},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
}

我可以通过将其添加到ci.yml中,将其直接打印到UI中运行的github操作中:

- name: Print to github
        run: cat coverage-testing/coverage.json >> $GITHUB_STEP_SUMMARY

它显示在github操作运行的注解下

jest-next summary
{"total": {"lines":{"total":52,"covered":11,"skipped":0,"pct":21.15},"statements":{"total":54,"covered":11,"skipped":0,"pct":20.37},"functions":{"total":16,"covered":4,"skipped":0,"pct":25},"branches":{"total":18,"covered":11,"skipped":0,"pct":61.11},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}

Job summary generated at run-time

现在来看看漂亮的打印。我已经尝试了一些jq命令,包括迭代to_entries,使github操作作业摘要看起来更像是markdown。我得到了错误,因为数组是嵌套的,我通过将-r改为-R并添加try来消除这些错误,但没有任何东西打印到github。由于这些尝试都是在CI中进行的,而且jest测试很不可靠,修改一个东西然后等待是很麻烦的。有人碰巧已经有这样的脚本了吗?我不挑剔它的格式,我只是希望它看起来不像json而更像markdown。

4ioopgfo

4ioopgfo1#

通过输入流,可以很快地将其表示为路径/值对的列表,然后可以将路径连接成更可读的形式并转储它。

$ jq -r --stream 'select(length == 2)|"\(.[0]|join("_")): \t\(.[1])"' input.json
total_lines_total:      18326
total_lines_covered:    11
total_lines_skipped:    0
total_lines_pct:        0.06
total_statements_total:         62625
total_statements_covered:       11
total_statements_skipped:       0
total_statements_pct:   0.01
total_functions_total:  19047
total_functions_covered:        4
total_functions_skipped:        0
total_functions_pct:    0.02
total_branches_total:   60202
total_branches_covered:         11
total_branches_skipped:         0
total_branches_pct:     0.01
total_branchesTrue_total:       0
total_branchesTrue_covered:     0
total_branchesTrue_skipped:     0
total_branchesTrue_pct:         100

如果你想保留一些结构,你需要首先决定...

相关问题