Skip to content

Instantly share code, notes, and snippets.

@ArturT
Last active December 1, 2022 07:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArturT/72ed2a0cca035a25c1158348ad21aaa1 to your computer and use it in GitHub Desktop.
Save ArturT/72ed2a0cca035a25c1158348ad21aaa1 to your computer and use it in GitHub Desktop.
GitHub Actions config for NodeJS and Jest tests. Running parallel jobs for Jest test suite with https://knapsackpro.com/?utm_source=github&utm_medium=gist&utm_campaign=jest-on-github-actions&utm_content=sign_up

GitHub Actions config for running Jest tests with parallel jobs

@knapsack-pro/jest supports environment variables provided by GitHub Actions to run your tests. You have to define a few things in .github/workflows/main.yaml config file.

  • You need to set KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST environment variable in GitHub repository Settings -> Secrets. See creating and using secrets in GitHub Actions.
  • You should create as many parallel jobs as you need with matrix.ci_node_total and matrix.ci_node_index properties. If your test suite is slow you should use more parallel jobs.

Below you can find config for GitHub Actions.

Video demo Jest and GitHub Actions

https://youtu.be/hKbclhHBGBU

# .github/workflows/main.yaml
name: Main
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [8.x]
# Set N number of parallel jobs you want to run tests on.
# Use higher number if you have slow tests to split them on more parallel jobs.
# Remember to update ci_node_index below to 0..N-1
ci_node_total: [2]
# set N-1 indexes for parallel jobs
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
ci_node_index: [0, 1]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install and build
run: |
npm install
npm run build --if-present
- name: Run tests with Knapsack Pro
env:
KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_JEST }}
KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }}
run: |
$(npm bin)/knapsack-pro-jest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment