1 of 49

Frontend testing practice & principle in Mamikos

🔬

x

2 of 49

Tri Hargianto

Connect

trihargianto.com

Tri Hargianto

Tri Hargianto

Tech Lead, Front-end at Mamikos / Co-Organizer at JogjaJS

x

3 of 49

Yang akan dibahas

01

02

03

04

05

06

07

Apa itu testing dan kenapa bikin automation test

Untuk siapa kita membuat UI

Sejarah frontend testing di Mamikos dan masalahnya

Pendekatan yang dipakai sekarang

Library yang dipakai

Ngomongin Code Coverage

Yang kami pelajari setelah menerapkan automation test

x

4 of 49

Ekspektasi

Gak bahas testing di luar frontend testing

Gak bahas End-to-end testing

Contoh kode pakai Vue & JavaScript

Merupakan praktik testing di Mamikos

x

5 of 49

Ngomongin Testing

01

Apa itu testing dan kenapa bikin automation test

x

6 of 49

Automation Testing

Manual Testing

VS

x

7 of 49

Automation Testing?

Form.vue

Form.test.js

x

8 of 49

Automation Testing?

Ngapain repot-repot, bikin kode tes? langsung manual beres kan?

Fitur yang lagi dibikin jadi lebih lama dong selesainya

x

9 of 49

Kenapa repot bikin automation test?

  • Dapet rasa yakin komponen dan fitur yang kita bikin bekerja sesuai yang kita mau

Benar

Salah

  • Merupakan poin KPI mu

  • Ikutan hype

  • Di suruh senior supaya Pull Request di approve

dll

x

10 of 49

Gimana caranya bisa dapet

rasa yakin?

x

11 of 49

Bahas pengguna

02

Untuk siapa kita membuat UI

x

12 of 49

Studi Kasus: Komponen Counter

https://github.com/trihargianto/testing-principle-demo

x

13 of 49

Untuk Siapa Kita Membuat Komponen UI ?

Yang berinteraksi dengan komponen mu melalui browser

End-User

Yang memakai

komponen mu

Developer

x

14 of 49

  • Melihat sebuah teks, 2 tombol plus dan minus, 1 tombol berlabel “Kirim”.

  • Ketika ia menekan tombol merah (-), maka angka 2 akan dikurangi satu

  • ditambah satu lagi ketika menekan tombol biru (+)

  • Jika tombol “kirim” di tekan, maka ia akan men-submit form nya

Yang berinteraksi dengan komponen mu melalui browser

End-User

x

15 of 49

  • Me-render komponen ini di komponen <form-user /> (misal)

  • Dari situ ia akan mengirimkan props label dan event click untuk mendapatkan nilainya.

Yang memakai

komponen mu

Developer

<form-user>

<counter

label=”judulnya”

@click=”handle()”

/>

</form-user>

x

16 of 49

Mari sepakat bahwa kita membuat UI hanya untuk melayani 2 users dan tidak selain itu

x

17 of 49

Sejarah & Masalah

03

Sejarah frontend testing di Mamikos dan masalahnya

x

18 of 49

Studi Kasus: Komponen Counter

https://github.com/trihargianto/testing-principle-demo

x

19 of 49

Requirements

  • Menampilkan sebuah angka dengan nilai default 0.

  • Satu tombol berwarna biru untuk menaikkan satu angka ke atas.

  • Satu tombol berwarna merah untuk menurunkan satu angka ke bawah.

  • Satu tombol submit yang akan men-trigger event click

  • Satu baris teks yang berfungsi sebagai label form

x

20 of 49

Demo

x

21 of 49

Kesalahan #1 : Implementation Detail Testing

Demo

x

22 of 49

Kenapa Perlu Menghindari Implementation Detail Testing

  1. Automation test fail ketika kita me-refactor kode. False Negative.
  2. Automation test pass ketika kita merusak aplikasi. False Positive.

False Positive: Kondisi ketika automation test pass tapi aplikasi sebenarnya tidak berjalan normal

False Negative: Kondisi ketika automation test fail tapi aplikasi sebenarnya berjalan normal

x

23 of 49

Kesalahan #2 : Pakai shallowMount()

x

24 of 49

Apa itu shallowMount() ?

App.vue

Counter.vue

TextDisplay.vue

App.test.js

Counter.test.js

TextDisplay.test.js

“Creates a Wrapper that contains the mounted and rendered Vue component, but with stubbed child components”

x

25 of 49

Demo

https://github.com/trihargianto/testing-principle-demo/tree/2-shallowmount

x

26 of 49

Kenapa pakai shallowMount() ?

  1. Tiap komponen bisa punya file test nya sendiri dan ter-isolate dari tes lain

  1. Performance

x

27 of 49

shallowMount() in action

x

28 of 49

Pendekatan

04

Pendekatan yang dipakai sekarang

x

29 of 49

— Kent C Dodds

#1 Menerapkan Prinsip

“The more your tests resemble the way your software is used, the more confidence they can give you.”

x

30 of 49

#2 Blackbox testing

x

31 of 49

#2 Blackbox testing

x

32 of 49

#3 Teknik Memilih Selector

Selector

Catatan

wrapper.find('button')

Terburuk. Terlalu generik, tidak memiliki konteks.

wrapper.find('.btn');

Buruk. Bergantung ke styling. Sangat mungkin style berubah.

wrapper.find('#btn');

Sedikit lebih baik. Tapi masih bisa bergantung ke style atau bahkan JS Event Listeners. Sangat mungkin berubah.

wrapper.find('[data-testid="btn"]')

Baik. Terisolasi dari semua perubahan.

Memilih elemen berdasarkan apa yang dilihat user.

Sangat baik.

x

33 of 49

Demo

https://github.com/trihargianto/testing-principle-demo/tree/3-blackbox

x

34 of 49

Library & Tools

05

Library & tools yang dipakai sekarang

x

35 of 49

Sebuah prinsip akan lebih maksimal untuk diterapkan ketika kita mempunyai tools yang tepat.

x

36 of 49

Tools

Jest

Mercury is the smallest planet

Before

After

Vue-Test-Utils

Mercury is the smallest planet

Jest

jestjs.io

Vue Testing Library

testing-library.com/vue

Mock Service Worker

mswjs.io

Jest DOM

github.com/testing-library/jest-dom

x

37 of 49

Demo

Jest

jestjs.io

Vue Testing Library

testing-library.com/vue

Jest DOM

github.com/testing-library/jest-dom

x

38 of 49

Code Coverage

06

Apa itu Code Coverage dan perannya di Mamikos

x

39 of 49

Code Coverage

— ChatGPT

“Code coverage is a metric used in software testing to measure the extent to which your code has been tested. It quantifies the percentage of code that has been executed during a test suite or testing process. Code coverage helps developers and quality assurance teams assess the thoroughness of their testing efforts, identify areas of the code that remain untested, and gauge the overall quality of their software.”

x

40 of 49

Demo Code Coverage

jest --coverage

x

41 of 49

Daily Monitoring

x

42 of 49

Refleksi

07

Yang kami pelajari setelah menerapkan automation test

x

43 of 49

#1 Development Speed

x

44 of 49

#2 Reproducibility

“Humans should do things that humans are good at, and performing repetitive tasks is not one of them.”

x

45 of 49

#3 Bisa Diprediksi

x

46 of 49

Resources

📝

Tempat buat belajar frontend testing

x

47 of 49

Belajar Frontend Testing

Course

Book

Blog

Testing Libraries

https://testingjavascript.com

https://www.manning.com/books/testing-javascript-applications (Lucas da Costa)

https://kentcdodds.com/blog?q=testing

https://test-utils.vuejs.org/guide/

https://testing-library.com/vue

x

48 of 49

— Kent C Dodds

“Always remember the reason that you’re testing is about confidence. If something your test is doing isn’t bringing you more confidence, then consider whether you can stop doing it!”

x

49 of 49

Terimakasih

x