Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
jest introduction
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Ryota Matsunaga
December 07, 2020
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
jest introduction
Ryota Matsunaga
December 07, 2020
More Decks by Ryota Matsunaga
See All by Ryota Matsunaga
jest introduction
mats0000
0
290
Other Decks in Programming
See All in Programming
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
670
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
6
1.7k
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
450
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
2.8k
Security issues being discussed on Web Platforms
petamoriken
0
850
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
120
巨大モノリシックアプリ モダン化大作戦
ktcryomm
0
960
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
160
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
150
アクセシビリティから考える情報設計
high_g_engineer
0
370
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
130
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
240
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
How to build a perfect <img>
jonoalderson
1
6k
Odyssey Design
rkendrick25
PRO
2
800
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
270
Everyday Curiosity
cassininazir
0
320
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
The Limits of Empathy - UXLibs8
cassininazir
1
660
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
250
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
520
Become a Pro
speakerdeck
PRO
31
6.2k
Transcript
Jest 松永遼太
話すこと Jest について 話なさないこと youtubeチャンネル
Jest とは
JavaScript のテスティングフレームワーク TypeScript, Node, React, Angular, Vue, Babel などに対応 テストがシンプルにできる
インストールする際は
https://jestjs.io/docs/ja/getting-started
"name": ”reverseInt_test", "version": "1.0.0", "main": "app.js", "scripts": { "test": "jest"
}, "author": "", "license": "ISC", "devDependencies": { "jest": "^24.9.0" }, "dependencies": {}, "description": "" Package.json
npm run test / yarn testでテストを実⾏できる
PASS ./reverseInt.test.js ✓ reverseIntのユニットテスト (6ms) Test Suites: 1 passed, 1
total Tests: 1 passed, 1 total Snapshots: 0 total Time: 1.816s, estimated 2s Ran all test suites. FAIL ./reverseInt.test.js ✕ reverseIntのユニットテスト (3ms) どこのテストが失敗したか教えてくれ る時もある
Matchers
Jest では、matcher を使⽤して、様々な⽅法で値のテストをする ことができます。 この時に使われる関数は test expect toBeなど
var reverseInt = function(x) { if (x === 0) return
0; const sign = (x > 0) ? 1 : -1; if (x < 0){ x = x * -1; } let reversedInt = 0; while(x > 0){ reversedInt = (reversedInt * 10) + (x % 10); x = Math.floor(x/10); } return reversedInt * sign; }; こういう関数があったとします
test('reverseIntのユニットテスト', () => { expect(reverseInt(0)).toBe(0); expect(reverseInt(3)).toBe(3); expect(reverseInt(51)).toBe(15); expect(reverseInt(300)).toBe(3); expect(reverseInt(123456789)).toBe(987654321); expect(reverseInt(-311)).toBe(-113);
//他にも expect(reverseInt(null)).toBeNull(); expect(reverseInt(true)).toBeTruthy(); expect(reverseInt(5)).toBeDefined(); expect(reverseInt(false)).toBeFalsy(); }); Jestでmatcherを使ったテストはこんな感じ
expect は “expectation” オブジェクトを返します。 toBeはObject.isを使って値を⽐べている expect(2 + 2).toBe(4)
じゃーオブジェクト同⼠を⽐べるには? const user = {name: “Ryota”,}; user[‘age'] = 20; expect(user).toEqual({name:
Ryota, age: 20});
⾮同期
⾮同期動のコードがある場合は Jestはテスト対象のコードがいつ 完了したかを別のテストに進む前に知る必要があります。 そのためにdone( )などの関数があります。 *Done を使うのはcallback を使って⾮同期をしている場合 https://jestjs.io/docs/ja/asynchronous
Async/ await を使おう
⾮同期のコードをテストする場合はjavascript でも使う async/await を使ったほうがシンプル test(‘the data is peanut butter’, async
() => { const data = await fetchData(); expect(data).toBe('peanut butter’); });
テストの構成
テストを実⾏する前にいくつかのセットアップ作業をしたり、テ ストが終了した後にいくつかの仕上げ作業をしたい場合がありま す
例えば、テストの前にデータベースを初期化し、テスト後はデータベースのデータを削除 しい
beforeEach(() => { initializeDatabase(); }); afterEach(() => { clearDatabase(); });
});
スコープ
describe('matching cities to foods', () => { beforeEach(() => {
return initializeFoodDatabase(); }); test('Vienna <3 sausage', () => { expect(isValidCityFoodPair('Vienna', 'Wiener Schnitzel')).toBe(true); }); test('San Juan <3 plantains', () => { expect(isValidCityFoodPair(‘San Juan’, 'Mofongo')).toBe(true); }); });
describe ブロックの中にあるときは、 before などが ある場合 、 describe はdescribe ブロックの中のテストにだけに適⽤されます
モック関数
モック関数を使えば、複雑なコードも分解してテストできる
複雑な関数を分解してテスト const mockCallback = jest.fn(x => 42 + x); forEach([0,
1], mockCallback); expect(mockCallback.mock.calls.length).toBe(2); expect(mockCallback.mock.calls[0][0]).toBe(0); expect(mockCallback.mock.calls[1][0]).toBe(1); expect(mockCallback.mock.results[0].value).toBe(42);
コードにテスト⽤の値を⼊れるのにも利⽤できます const myMock = jest.fn(); myMock.mockReturnValueOnce(10).mockReturnValueOnce('x').mockReturnValue(true); console.log(myMock(), myMock(), myMock(), myMock());
// 10, 'x', true, true
Jest 応⽤編 Coming up next ……
https://jestjs.io/ja/