2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jest×Next.js×TypeScriptでテストを書く準備

Last updated at Posted at 2023-05-07

(テストコードを書き始めたので、今後忘れないように、自分用にメモします)

テンプレートを作成

npx create-next-app@latest --ts next-sample

Jestをインストールする

(Jest以外にも、必要なライブラリをインストールする)

npm install -D jest @testing-library/react @testing-library/jest-dom jest-environment-jsdom

Jest-DOMを使えるようにする

ルートディレクトリに、jest.setup.tsを作成し、以下を書く
(このファイルを作るのは必須ではないが、その場合、jest.config.tsのsetupFilesAfterEnv: ['/jest.setup.js'],を消す。)

import '@testing-library/jest-dom/extend-expect'

設定を書く

ルートディレクトリに、jest.config.tsを作成し、以下を書く

const nextJest = require('next/jest')

const createJestConfig = nextJest({
  dir: './',
})

const customJestConfig = {
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
  moduleDirectories: ['node_modules', '<rootDir>/'],
  testEnvironment: 'jest-environment-jsdom',
}

module.exports = createJestConfig(customJestConfig)

テスト

あとはテストコードを書くのみです。

以上

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?