Madogiwa Blog

主に技術系の学習メモに使っていきます。

JestのcacheをCIでも利用して高速化するメモ📝

Jestはデフォルトでcacheを利用するのですが、CIでも利用できるようにしとくと高速化できそうだったのでやり方をメモ📝

Whether to use the cache. Defaults to true. Disable the cache using --no-cache.

Jest CLI Options · Jest

jestでcacheを保存するパスを指定する

CIでcacheしやすいようにcacheの配置先をcacheDirectoryで明示的に指定します。

module.exports = {
  cacheDirectory: "node_modules/.cache/jest"
  // ...
}

The directory where Jest should store its cached dependency information.

Configuring Jest · Jest

これでCI上でもcacheを利用しやすくなりました!

CIでjestのcacheを利用する

以下jestのcacheを利用するような設定を入れたGitHub Actionのサンプルです。

name: frontend ci

on: [push]

jobs:
  ci:
    runs-on: ubuntu-latest
    env:
      TZ: Asia/Tokyo
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v3
        with:
          node-version: "16"
          cache: 'yarn'
      - name: install node deps
        run: yarn install --frozen-lockfile
      - name: cache jest
        uses: actions/cache@v3
        with:
          path: node_modules/.cache/jest
          key: jest-v1-${hashFiles('yarn.lock')}-${{ github.ref_name }}-${{ github.sha }}
          restore-keys: |
            jest-v1-
      - name: test js
        run: yarn test:coverage

簡単ですね!

参考

engineering.meetsmore.com