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
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
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
200
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
100
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
310
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
530
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
120
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
790
Are APIs Still Relevant in the AI Era?
soyuka
0
130
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
820
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
iOSDC2026登壇資料.pdf
riofujimon
0
150
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.6k
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
620
Featured
See All Featured
Building the Perfect Custom Keyboard
takai
2
870
Documentation Writing (for coders)
carmenintech
77
5.5k
Designing for Performance
lara
611
70k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.9k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.1k
Being A Developer After 40
akosma
91
590k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
830
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
980
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
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/