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
Testing Vue components with Jest and vue-test-u...
Search
Ryo Hashimoto
April 25, 2018
Technology
0
280
Testing Vue components with Jest and vue-test-utils
How to test Vue components effectively with Jest and vue-test-utils.
Ryo Hashimoto
April 25, 2018
Tweet
Share
More Decks by Ryo Hashimoto
See All by Ryo Hashimoto
What's new in Ruby on Rails 5.2
ryohashimoto
4
2.8k
Other Decks in Technology
See All in Technology
マーケットプレイス版Oracle WebCenter Content For OCI
oracle4engineer
PRO
2
170
株式会社M2X エンジニアチーム紹介資料
m2xsoftware
0
410
歴史と背景から改めて振り返るVPC
shotashiratori
2
220
スーパーマリオRPGのリメイク版の変更点からみるUX
nishiharatsubasa
1
270
Our Journey from in-House CD System to Open Source
ffjlabo
0
110
Dify - LINE Bot連携 考え方と実用テクニック
uezo
5
1.1k
目標設定と習慣化で今よりも一歩生産性を上げる
sansantech
PRO
7
2.4k
LLM を現場で評価する
asei
4
700
Discover Your Tailored Platform Strategy with Real-World Practice
hhiroshell
1
140
技術力あげたい
hisaichi5518
2
2.9k
Javaにおける関数型プログラミンへの取り組み
skrb
7
280
Evolving DevOps Teams and Flexible Organizational Culture
kakehashi
1
180
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
27
7.4k
4 Signs Your Business is Dying
shpigford
179
21k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Fantastic passwords and where to find them - at NoRuKo
philnash
47
2.8k
Thoughts on Productivity
jonyablonski
66
4.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
359
18k
Principles of Awesome APIs and How to Build Them.
keavy
125
16k
Fashionably flexible responsive web design (full day workshop)
malarkey
401
65k
StorybookのUI Testing Handbookを読んだ
zakiyama
25
5k
Code Review Best Practice
trishagee
62
16k
Git: the NoSQL Database
bkeepers
PRO
425
64k
Transcript
Testing Vue components with Jest and vue-test-utils @ryohashimoto
ࠓճ͓͢͠Δ͜ͱ • 2िؒ΄ͲલʹɺVue.jsΛϑϩϯτΤϯυͰ ͬͨRuby on RailsͷϓϩδΣΫτͰɺJest ͱvue-test-utilsΛಋೖɻ • ؆୯ʹVueίϯϙʔωϯτͷ୯ମςετ͕ॻ͚ ΔΑ͏ʹͳͬͯɺΧόϨοδऔΕΔΑ͏ʹ
ͳͬͨɻ
ͳͥɺVueίϯϙʔωϯτͷ ςετΛߦ͏͔ʁ
Why? • ֤ίϯϙʔωϯτ͕ࢦఆ͞Εͨঢ়ଶͰਖ਼͘͠ ಈ͘͜ͱΛอূ͢ΔͨΊʢϢχοτςετʣ • खಈςετͷ߹ͷखؒʢϒϥβͷૢ࡞ʣ Λল͖ɺCIͰ࣮ߦՄೳʹ͢ΔͨΊ • কདྷతͳVue.jsͷΞοϓάϨʔυʹඋ͑ΔͨΊ
Vue.jsΞϓϦέʔγϣϯͷ ߏཁૉ
Vueίϯϙʔωϯτ • HTMLςϯϓϨʔτ+Vue.jsίʔυ • ΧϓηϧԽ͞Εͨ࠶ར༻Մೳͳίʔυ • ୯ҰϑΝΠϧίϯϙʔωϯτ(.vue֦ுࢠ)Λ ༻Ͱ͖Δ • Έ߹ΘͤΔ͜ͱͰΞϓϦέʔγϣϯΛߏ
Vueίϯϙʔωϯτͷྫ <template> <p>{{ greeting }} World!</p> </template> <script> module.exports =
{ data () { return { greeting: 'Hello' } } } </script> JavaScript (ES6) HTML
Vuex ετΞ • ΞϓϦέʔγϣϯͷঢ়ଶʢεςʔτʣΛҰݩ อ࣋͢Δ • ঢ়ଶͷมԽϛϡʔςʔγϣϯͷίϛοτʹ Αͬͯߦ͏ • ίϯϙʔωϯτຖʹঢ়ଶΛ࣋ͨͳͯ͘ࡁΉ
(ύϥϝʔλͷड͚͕͠ෆཁʹͳΔ)
Vuex ετΞͷྫ const store = new Vuex.Store({ state: { count:
0 }, mutations: { increment (state) { state.count++ } } }) εςʔτ ύϥϝʔλ ϛϡʔςʔγϣϯ ૢ࡞
Vueίϯϙʔωϯτͷςετ
લఏɿ୯ମςετͷ3-A • Arrange (४උ) ॳظԽ͠ɺσʔλΛ४උ͢Δ • Act (࣮ߦ) ςετରͱͳΔॲཧΛ࣮ߦ͢Δ •
Assert (ݕূ) ༧ଌ௨Γʹಈ࡞͢Δ͜ͱΛݕূ͢Δ
Vueίϯϙʔωϯτͷ3A (1) Vue component Vuex state HTML • Vueίϯϙʔωϯτʹ VuexεςʔτΛೖྗͱ
ͯ͠༩͑Δ͜ͱͰɺHTMLΛग़ྗ͢Δ • Arrange: VuexεςʔτɺAct: Vueίϯϙʔω ϯτɺAssert: HTML
Vueίϯϙʔωϯτͷ3A (2) Vue component Vuex state HTML Event • ΠϕϯτΛ༩͑ͨ߹ʢΫϦοΫͳͲʣ
• Arrange: Vuexεςʔτ, Act: Πϕϯτ / Vueί ϯϙʔωϯτ, Assert: HTML / Vuexεςʔτ
Vueίϯϙʔωϯτͷςετཁ݅ • Vue ίϯϙʔωϯτ୯ମͰςετ͕Ͱ͖Δ • VuexετΞɺΠϕϯτͳͲΛਖ਼͘͠ѻ͑Δ • πʔϧͷಋೖ͕؆୯ͰɺΧόϨοδऔΕΔ • Jest
ͱ vue-test-utilsͳΒશ͕ͯՄೳʂ
ςετͷπʔϧ (Jest / vue-test-utils)
Jest • JavaScriptͷςετϑϨʔϜϫʔΫ ʢFacebook, ReactYarnͳͲͰར༻) • ΦʔϧΠϯϫϯͰɺগͳ͍ઃఆͰར༻Ͱ͖Δ • ؆୯ʹΧόϨοδΛऔಘͰ͖Δ •
Vue.jsͰͪΖΜར༻͕Մೳ
Jestͷίʔυྫ // sum.js function sum(a, b) { return a +
b } module.exports = sum // sum.test.js import sum from './sum' test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3) });
vue-test-utils • Vue.js͚ͷެࣜ୯ମςετϥΠϒϥϦ • VueίϯϙʔωϯτΛִͯ͠Ϛϯτͯ͠ಈ ࡞ͤ͞ɺςετΛߦ͏͜ͱ͕Ͱ͖Δ • JestΛͬͨςετͷެࣜυΩϡϝϯτ͋Γ (https://vue-test-utils.vuejs.org/ja/guides/testing-SFCs-with-jest.html)
wrapper (vue-test-utils) • ίϯϙʔωϯτͱԾ DOM Λςετ͢Δϝ ιουΛؚΉΦϒδΣΫτ • mount /
shallowϝιουͰίϯϙʔωϯτΛ ࢦఆͯ͠ɺॳظԽ͢Δ • wrapper.findͰDOMΤϨϝϯτΛࢦఆͯ͠ɺ triggerͰΠϕϯτΛൃՐͤ͞Δ͜ͱ͕Մೳ
vue-test-utilsΛ༻͍ͨྫ test('close the app', () => { // Arrange const
wrapper = shallow(TriggerButton) // Act wrapper.find('.close').trigger('click') // Assert expect(store.state.closed).toBe(true) })
Jestͱvue-test-utilsͷ ϓϩδΣΫτͷಋೖ
ύοέʔδͷΠϯετʔϧ • yarn add @vue/test-utils jest vue- jest babel-jest jest-serializer-
vue --dev • jest.config.jsʹઃఆϑΝΠϧΛՃ • Rails 5.1ͷϓϩδΣΫτͰWebpackerΛ༻ ͍ͯ͠ΔͷͰɺ༰қʹಋೖͰ͖ͨɻ
jest.config.js module.exports = { "moduleFileExtensions": [ "js", "json", "vue" ],
"moduleNameMapper": { "^@/(.*)$": "<rootDir>/app/javascript/$1" }, "transform": { ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest", "^.+\\.js$": "<rootDir>/node_modules/babel-jest" }, "snapshotSerializers": [ "<rootDir>/node_modules/jest-serializer-vue" ], "testRegex": "/app/javascript/test/.*\\.test\\.js$" }
·ͱΊ • Jestͱvue-test-utilsΛಋೖ͢Εɺ؆୯ʹ Vue.jsίϯϙʔωϯτͷ୯ମςετΛॻ͘͜ͱ ͕Ͱ͖Δɻ • ϑϩϯτΤϯυͷςετίʔυͲΜͲΜॻ ͍͍͖ͯ·͠ΐ͏ɻ