我有一个 React 应用程序,集成了 jest 用于测试目的。
如果我跑步,
yarn web:ui:test
生成测试报告。
**test suites:** 1 Failed, 1 Total
**Tests:** 1 Failed, 13 passed ,14 total
**Snapshot:** 0Total
**Time**: 14.753 sec
但是如果我将相同的步骤集成到 yml 文件中的 github/工作流程的 ci 中。
name: Run UI Tests with Enhanced Reporting
on:
push:
branches: ["main"] # Adjust branch(es) as needed
jobs:
test:
runs-on: ubuntu-latest # Adjust runner OS as needed
steps:
- uses: actions/checkout@v3 # Checkout the repository code
- name: Use Node.js with yarn
uses: actions/setup-node@v3
with:
node-version: 19.9.0 # Adjust Node.js version as needed
- name: get yarn cache dir path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v3
id: yarn-cache
# use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install project dependencies
# if: steps.yarn-cache.outputs.cache-hit != 'false'
run: yarn --prefer-offline #yarn install
- name: Run UI tests
continue-on-error: true
run: |
cd ./packages
yarn web:test:ui > test-results.txt # Capture test output
- name: Upload test results
uses: actions/upload-artifact@v3
with:
name: test-results.txt
path: test-results.txt
- name: Store exit code
id: store-exit-code
continue-on-error: true
run: echo "::set-output name=exit-code::$(yarn web:test:ui)"
- name: Check test results
if: steps.store-exit-code.outputs.exit-code != 0
run: echo "Test suite failed with exit code ${{ steps.store-exit-code.outputs.exit-code }}"
问题:为什么不像本地机器那样生成报告覆盖率?
两者都经过测试,但我仍然只看到一长串错误,而没有正确的报告。 旧堆栈溢出
我尝试在 json 包的 jest 行中实现 --coverage 标志,如 stackoverflow 问题中提到的(如何使用 React-create-app 获取代码覆盖率报告?) 和其他市场行为只是为了获取报告。我还链接了我推荐过的其他博客。
确定不是生成的吗?
run: |
cd ./packages
yarn web:test:ui > test-results.txt # Capture test output
在这里
cd ./packages
-> 当前目录是 ${{ github.workspace }}/packages
-> 你的文件被创建为 ${{ github.workspace }}/packages/test-results.txt
但是在上传部分你这样做:
uses: actions/upload-artifact@v3
with:
name: test-results.txt
path: test-results.txt
预计文件位于
${{ github.workspace }}/test-results.txt
。将其更改为:
uses: actions/upload-artifact@v3
with:
name: test-results.txt
path: ./packages/test-results.txt
它应该可以工作。