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
独自ツール開発でスタジオ撮影をDX!「VLS(Virtual LED Studio)」 / dx-studio-vls
cyberagentdevelopers
PRO
1
180
使えそうで使われないCloudHSM
maikamibayashi
0
170
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
来年もre:Invent2024 に行きたいあなたへ - “集中”と“つながり”で楽しむ -
ny7760
0
470
30万人が利用するチャットをFirebase Realtime DatabaseからActionCableへ移行する方法
ryosk7
5
350
なんで、私がAWS Heroに!? 〜社外の広い世界に一歩踏み出そう〜
minorun365
PRO
6
1.1k
CAMERA-Suite: 広告文生成のための評価スイート / ai-camera-suite
cyberagentdevelopers
PRO
3
270
[JAWS-UG金沢支部×コンテナ支部合同企画]コンテナとは何か
furuton
3
250
ユーザーの購買行動モデリングとその分析 / dsc-purchase-analysis
cyberagentdevelopers
PRO
2
100
【若手エンジニア応援LT会】AWSで繋がり、共に成長! ~コミュニティ活動と新人教育への挑戦~
kazushi_ohata
0
180
Automated Promptingを目指すその前に / Before we can aim for Automated Prompting
rkaga
0
110
Commitment vs Harrisonism - Keynote for Scrum Niseko 2024
miholovesq
6
1.1k
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
Making Projects Easy
brettharned
115
5.9k
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
Designing for Performance
lara
604
68k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
167
49k
Bash Introduction
62gerente
608
210k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
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ίϯϙʔωϯτͷ୯ମςετΛॻ͘͜ͱ ͕Ͱ͖Δɻ • ϑϩϯτΤϯυͷςετίʔυͲΜͲΜॻ ͍͍͖ͯ·͠ΐ͏ɻ