使用 Jest 、 React Testing Library 、MSW 建立測試環境

主要套件

Jest - 建立測試環境 提供 asseration 等功能判斷結果
React Testing Library - 針對 React component 進行操作
Mock Service Worker - MSW 攔截 API 作 mock 資料

@jest/globals
@testing-library/jest-dom
@testing-library/react
@types/jest
jest
jest-environment-jsdom - jsdom 環境 可測試 UI 的部分
dotenv - 建立 env 環境參數
ts-jest
msw
undici - msw 在 jsdom 下使用到 
有雷!!!
請使用 https://github.com/mswjs/examples/blob/main/examples/with-jest-jsdom/package.json msw example 中的版本
現在直接install會是 6.多版會造成奇怪的錯誤

如果需要使用 jsdom 且電腦是 mac M 系列
需到此使用 homebrew 安裝相關套件
https://github.com/Automattic/node-canvas

設定

這邊會以 jsdom 的測試環境為主

jest.config.ts

jest.polyfills.ts 這是根據 msw 來的 https://mswjs.io/docs/migrations/1.x-to-2.x/

setup-tests.ts 建立 env 中的參數 建立 msw

node.ts 建立 server

handler.ts 設定 API response url 需使用完整的網址 https://..... 只用 relative url 會攔截不到

網路上的範例有使用 import { rest } from 'msw' 這是舊版的寫法 新版請使用 http

Last updated