Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
とうとうJestを導入してみた話
Search
ricky
July 03, 2019
Programming
0
11k
とうとうJestを導入してみた話
React×TypeScript環境にユニットテストツール「Jest」を導入してみた話です
ricky
July 03, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
12
7.2k
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
330
When Dependencies Fail: Building Antifragile Applications in a Fragile World
selcukusta
0
110
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
310
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
650
Webサーバーサイド言語としてのRustについて
kouyuume
1
5k
Go言語はstack overflowの夢を見るか?
logica0419
0
630
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
3
980
CSC305 Lecture 12
javiergs
PRO
0
240
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
200
理論と実務のギャップを超える
eycjur
0
180
O Que É e Como Funciona o PHP-FPM?
marcelgsantos
0
210
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.7k
Stop Working from a Prison Cell
hatefulcrawdad
272
21k
Building a Scalable Design System with Sketch
lauravandoore
463
33k
Speed Design
sergeychernyshev
32
1.2k
Thoughts on Productivity
jonyablonski
71
4.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.2k
The Language of Interfaces
destraynor
162
25k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
130k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
Designing for humans not robots
tammielis
254
26k
Transcript
とうとうJestを導入してみた話 React LT会 〜最近学び始めた方から業務でバリバリ使っている方まで〜 1
WHO AM I 2 小澤 力也(おざわ りきや)@rikipedia_uw • 株式会社ウィルゲート •
エンジニア・広報 • 技術:JavaScript(TS)・React(8ヶ月) • React LT会にずっと参加したかった
想定聴者 • React中級者 • React環境にテストを導入したいひと 3
Jestを導入したアプリケーション • 新規開発中のSPA ◦ React+Redux+TypeScript 4
Jestを導入したアプリケーション 5
Jestとは? 6 Jest(ジェスト) • Facebook謹製JavaScriptのユニットテ ストツール • オールインワンなテストフレームワーク ◦ テストランナー
◦ アサーション ◦ テストモック・テストダブル ◦ スナップショットテスト
Jestを導入した背景 • 開発当初は体制や納期の問題でテストに手を出せなかった ◦ シナリオテストやふるまいテストはやってた 7
Jestを導入した背景 • 開発当初は体制や納期の問題でテストに手を出せなかった ◦ シナリオテストやふるまいテストはやってた →落ち着いてきたのでテスト導入に至った 8
テストを導入する目的 • バグの混入を防ぐ・検知する • テスタブルなコードを意識して書ける • 一貫性のあるテスト観点になる • 品質を担保しつつ、テスターは別のテストを実施することができ る
• テストカバレッジにより定量的に品質を測定できる 9
テストを導入する目的 • バグの混入を防ぐ・検知する • テスタブルなコードを意識して書ける • 一貫性のあるテスト観点になる • 品質を担保しつつ、テスターは別のテストを実施することができ る
• テストカバレッジにより定量的に品質を測定できる 10 ↑メリットを活かすためのテストコードを書く
テスト対象 • Function Component ◦ 純粋関数 11
テスト対象 • Function Component ◦ 純粋関数 12 Container Component(Redux接続部)やRedux Storeの
テストは次の機会
テスト手法 • スナップショットテスト ◦ UIが予期せず変更されていないか確かめるテスト手法 13
React×Jestにおけるスナップショットテスト • コンポーネントの出力結果をファイルに保存 • 1回目ならファイル作成 • 2回目以降は前回との差分を見てOKなら通過 14
スナップショットテストの書き方 import React from 'react'; import Link from '../Link.react'; import
renderer from 'react-test-renderer'; it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON(); expect(tree).toMatchSnapshot(); }); 15 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing import React from 'react' import Link from '../Link.react' import renderer from 'react-test-renderer' it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON() expect(tree).toMatchSnapshot() }) component.test.tsx
スナップショットテストの書き方 import React from 'react'; import Link from '../Link.react'; import
renderer from 'react-test-renderer'; it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON(); expect(tree).toMatchSnapshot(); }); 16 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing 必要なライブラリなどを import import React from 'react' import Link from '../Link.react' import renderer from 'react-test-renderer' it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON() expect(tree).toMatchSnapshot() }) component.test.tsx
スナップショットテストの書き方 import React from 'react'; import Link from '../Link.react'; import
renderer from 'react-test-renderer'; it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON(); expect(tree).toMatchSnapshot(); }); 17 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing Reactコンポーネントを生成し、 Json形式に変換 import React from 'react' import Link from '../Link.react' import renderer from 'react-test-renderer' it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON() expect(tree).toMatchSnapshot() }) component.test.tsx
スナップショットテストの書き方 import React from 'react' import Link from '../Link.react' import
renderer from 'react-test-renderer' it('renders correctly', () => { const tree = renderer .create(<Link page="http://www.facebook.com">Facebook</Link>) .toJSON() expect(tree).toMatchSnapshot() }) 18 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing 1回目:スナップショットファイル生成 2回目以降:前回のファイルと比較して OKなら通過 component.test.tsx
スナップショットテストファイル exports[`renders correctly 1`] = ` <a className="normal" href="http://www.facebook.com" onMouseEnter={[Function]}
onMouseLeave={[Function]} > Facebook </a> ` 19 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing __snapshots__/component.test.tsx.snap
スナップショットテストファイル exports[`renders correctly 1`] = ` <a className="normal" href="http://www.facebook.com" onMouseEnter={[Function]}
onMouseLeave={[Function]} > Facebook </a> ` 20 Jest公式ページより:https://jestjs.io/docs/ja/snapshot-testing __snapshots__/component.test.tsx.snap 手動での書き換えは怒られる
スナップショットテストを導入してみて • テスタブルなコードを意識するとロジックとUIが自然に分離す る • ロジックの実装に集中できるようになった • 実装が変わったときに差分が出るからバグの検知が楽になっ た 21
スナップショットテストで気をつけたいこと • 期待する出力が明確になってから行う ◦ 間違ったテストになってしまうため • React Hooksも一部対応しているが完璧ではない 22
以上 23
懇親会で話したいこと • webpack周りの設定どうしてる? • Class Componentのこれからについて 24
Follow Me! 25 https://twitter.com/rikipedia_uw