[英]Github Actions Jest
我的 Jest 測試通過了,但是 Github 操作工作流沒有完成。
這是我的 main.yaml。
name: Jest Unit Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x]
mongodb-version: ['4.2', '4.4', '5.0']
steps:
- uses: actions/checkout@v3
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.7.0
with:
mongodb-version: ${{ matrix.mongodb-version }}
- run: npm ci
- run: npm run workflow
env:
CI: true
我需要修復我的 yaml 文件嗎?
結果證明 Github 問題與異步進程有關。 即使測試通過了,Jest 仍然會發生異步操作,因此 Github 操作默認為未完成。
通過添加--forceExit
來強制測試結束和--detectOpenHandles
以查看仍在發生的異步操作來解決此問題。
將這些添加到我的package.json
腳本中。
"scripts": {
"local-test": "jest --watchAll",
"workflow": "jest --forceExit --detectOpenHandles",
"start": "node server.js",
"dev": "nodemon server.js"
},
For those new to Github actions, the yaml file in my original post calls npm run workflow
at the end, which is the script in my package.json
.
有關 Jest 選項的更多信息,請參閱https://jestjs.io/docs/cli 。
還請確保您的笑話在管道運行期間沒有警告:
例如: Jest: "global" coverage threshold for branches (10%) not met: 8.33%
如確保閾值始終通過:
您可以在jest.config.js
文件中設置如下內容:
coverageThreshold: {
global: {
branches: 1,
functions: 1,
lines: 1,
statements: 1,
},
},
從命令中刪除“--watchAll”,並為 GitHub 工作流添加其他
"scripts": {
"test": "jest --watchAll",
"ghworkflowtest": "jest"
}
為本地測試運行“npm test”,為 GitHub 操作運行 in.yml 文件調用“npm run ghworkflowtest”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.