简体   繁体   中英

Github Actions Jest

My Jest tests are passing, but Github actions workflow isn't completing.

Here's my 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

在此处输入图像描述

Do I need to fix my yaml file?

So turns out Github issues has issues with async processes. Even though the tests passed, there were still async operations occurring from Jest, so Github actions defaults to not completing.

Fix this by adding --forceExit to force the test to end and --detectOpenHandles to see what async operations are still happening.

Added these to my package.json scripts.

"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 .

See https://jestjs.io/docs/cli for more info on the Jest options.

Please also ensure your jest has no warnings during pipeline running:

eg: Jest: "global" coverage threshold for branches (10%) not met: 8.33%

Such as ensure the threshold value is always passed:

you can setup something like below in jest.config.js file:

coverageThreshold: {
    global: {
      branches: 1,
      functions: 1,
      lines: 1,
      statements: 1,
    },
  },

Remove "--watchAll" from the command, and add other for GitHub Workflow

"scripts": {
   "test": "jest --watchAll",
   "ghworkflowtest": "jest"
}

Run "npm test" for local tests and in.yml file call "npm run ghworkflowtest" for GitHub actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM