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
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
1.1k
アプリエンジニアのためのGraphQL入門.pdf
spycwolf
0
140
A Tour of Anti-patterns for Functional Programming
guvalif
0
1.7k
プロダクト活用度で見えた真実 ホリゾンタルSaaSでの顧客解像度の高め方
tadaken3
0
280
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.9k
SDN の Hype Cycle を一通り経験してみて思うこと / Going through the Hype Cycle of SDN
mshindo
3
290
Yahoo! JAPANトップページにおけるマイクロフロントエンド - 大規模組織におけるFE開発を加速させるには
lycorptech_jp
PRO
0
970
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
310
Flutterによる 効率的なAndroid・iOS・Webアプリケーション開発の事例
recruitengineers
PRO
0
180
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
210
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
200
心が動くエンジニアリング ── 私が夢中になる理由
16bitidol
0
130
Featured
See All Featured
Writing Fast Ruby
sferik
627
61k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
How GitHub (no longer) Works
holman
310
140k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
4 Signs Your Business is Dying
shpigford
180
21k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Raft: Consensus for Rubyists
vanstee
136
6.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Facilitating Awesome Meetings
lara
50
6.1k
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ίϯϙʔωϯτͷ୯ମςετΛॻ͘͜ͱ ͕Ͱ͖Δɻ • ϑϩϯτΤϯυͷςετίʔυͲΜͲΜॻ ͍͍͖ͯ·͠ΐ͏ɻ