Nextのテストをswcで高速化

swcをnext/jest経由で利用してテストを高速化しました。


TR;DR

  • jestはswcで高速化出来ます。
  • @swc/jestを直接使うのではなく、next/jest経由で利用すると楽です。

※ 実際のコードはこちらのPRでご確認頂けます。

作業は以下を参考に進めます。

# turborepoの場合 apps/web等
yarn add -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
// jest.config.js
const nextJest = require('next/jest')

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: './'
})

// Add any custom config to be passed to Jest
const customJestConfig = {
  // Add more setup options before each test is run
  // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jest-environment-jsdom'
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig)

上記の設定をすることで以下のようなことを自動でしてくれます。

Under the hood, next/jest is automatically configuring Jest for you, including:

  • Setting up transform using SWC
  • Auto mocking stylesheets (.css, .module.css, and their scss variants) and image imports
  • Loading .env (and all variants) into process.env
  • Ignoring node_modules from test resolving and transforms
  • Ignoring .next from test resolving
  • Loading next.config.js for flags that enable SWC transforms