Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vue :: Better Testing 2024

up1
October 01, 2024

Vue :: Better Testing 2024

Vue :: Better Testing 2024

up1

October 01, 2024
Tweet

More Decks by up1

Other Decks in Programming

Transcript

  1. © 2017 - 2024 Siam Chamnankit Company Limited. All rights

    reserved. Better Testing With Vue application
  2. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 6 Write better code ! Refactor with confidence ! Deploy on Friday !
  3. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 13 Testing Effort ?
  4. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 14 Ice-Cream Cone https://alisterscott.github.io/TestingPyramids.html
  5. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 15 Testing Pyramid https://alisterscott.github.io/TestingPyramids.html
  6. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 16 Testing Pyramid https://alisterscott.github.io/TestingPyramids.html
  7. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 17 Cupcake https://www.thoughtworks.com/insights/blog/introducing-software-testing-cupcake-anti-pattern
  8. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 18 Testing Trophy https://www.testingjavascript.com/
  9. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 19 Testing Rocket https://kentcdodds.com/blog/write-tests https://madewithvuejs.com/blog/how-to-learn-vue-js-testing-the-ultimate-guide
  10. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 22 Fast Isolated Repeatable Self-validate Understand
  11. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 23 Test-First vs Test-Last Vs Test-Later
  12. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 24 JavaScript Testing With Vue and Nuxt app https://vuejs.org/guide/scaling-up/testing
  13. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 25 Testable project structure ? Start with
  14. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 26 Project structure Split by Feature or Domain Nuxt app User domain Product domain Catalog domain extends pages components config
  15. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 27 Project structure Split by Feature or Domain Nuxt app export default defineNuxtConfig({ devtools: { enabled: true }, modules: ['@nuxtjs/tailwindcss'], extends: ['./domains/users', './domains/products'], })
  16. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 28 Shared state Nuxt app User domain Product domain Catalog domain extends Shared State use
  17. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 29 https://github.com/goldbergyoni/javascript-testing-best-practices
  18. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 30 Frontend Backend Full stack JavaScript Developer !!
  19. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 32 Frontend Backend HTTPs REST gRPC tRPC … DB External System End-to-End testing
  20. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 33 How to test ? 1. Setup external system 2. Setup database 3. Deploy Backend 4. Deploy Frontend 5. Testing Process
  21. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 34 End-to-End Testing Vue External System CRUD Component A Component B
  22. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 36 Need more stable ???
  23. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 37 Need more speed and feedback ???
  24. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 38 Isolated Testing Vue External System CRUD Component A Component B Mock
  25. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 39 Component Testing Vue External System CRUD Component A Component B Mock https://vuejs.org/guide/scaling-up/testing#component-testing
  26. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 40 More speed and feedback But Lower confidence
  27. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 41 Need to balance
  28. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 42 Demo time Testing for Vue app
  29. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 43 Vue app End-to-End Testing Mock Mock Service Worker HTTP
  30. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 44 Start with write test
  31. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 45 Write test with Playwright import { test, expect } from '@playwright/test'; test('add item to list', async ({ page }) => { await page.goto('http://localhost:5173'); await page.fill('#title', '2x4 DIPA Melvin'); await page.click('#add-button'); const item = page.locator('.item:nth-child(1)'); await expect(item).toBeVisible(); }); Passed
  32. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 46 Bad test !! import { test, expect } from '@playwright/test'; test('add item to list', async ({ page }) => { await page.goto('http://localhost:5173'); await page.fill('#title', '2x4 DIPA Melvin'); await page.click('#add-button'); const item = page.locator('.item:nth-child(1)'); await expect(item).toBeVisible(); }); Coupling from test framework and User interface
  33. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 47 Decoupling from test framework
  34. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 48 Vue app Coupling from test framework
  35. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 49 Vue app Decoupling from test framework Custom Driver Testing framework library
  36. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 50 Better test import { test } from '../../drivers/virtual/driver'; test("add item to list", async ({ driver }) => { await driver.goTo("/"); await driver.findByTestId("title").type("2x4 DIPA Melvin"); await driver.findByTestId("add-button").click(); await driver .findByText("2x4 DIPA Melvin", { withinTestId: "active items" }) .shouldBeVisible(); }); Decoupling from test framework and User interface
  37. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 51 3 levels of UI test automation https://gojko.net/2010/04/13/how-to-implement-ui-testing-without-shooting-yourself-in-the-foot-2/
  38. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 52 Test case as specification Human understanding
  39. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 53 Better Better test test("add item to list", async ({ driver }) => { const shoppingList = createShoppingList({ driver }); await shoppingList.open(); const newBeer = "2x4 DIPA Melvin”; await shoppingList.expectItemNotOnList(newBeer); await shoppingList.addItem(newBeer); await shoppingList.expectItemOnList(newBeer); }); Test case as specification Human readable
  40. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 54 Domain Specific Language (DSL) const createShoppingList = ({ driver }: { driver: Driver }) => ({ open: async () => { await driver.goTo('/'); }, addItem: async (title: string) => { await driver.findByTestId("title").type(title); await driver.findByTestId("add-button").click(); }, expectItemOnList: async (item: string) => { await driver .findByText(item, { withinTestId: "active items" }) .shouldBeVisible(); }, expectItemNotOnList: async (item: string) => { await driver.queryByText(item).shouldNotExist(); }, });
  41. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 55 Component Testing Vue External System CRUD Component A Component B Mock https://vuejs.org/guide/scaling-up/testing#component-testing
  42. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 56 Vue app Component Testing Mock Mock Service Worker Testing framework library https://testing-library.com/docs/vue-testing-library/intro Vue Test Utils https://github.com/vuejs/test-utils
  43. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 57 ShoppingList component import { expect, test, render, screen, fireEvent } from '../__test__/utils'; import ShoppingList from './ShoppingList.vue'; test(`Should emit an "remove" event when we click an item`, async () => { let { emitted } = render(ShoppingList, { props: { items: [ { id: '1', title: `2x4 DIPA Melvin 9.9%`, state: `active` }, { id: '2', title: `Muay Thai Kob Khun 6.2%`, state: `active` }, ], }, }); const removeItem = screen.getByRole(`button`, { name: `2x4 DIPA Melvin 9.9%`, }); await fireEvent.click(removeItem); const removeEvents = emitted().remove as unknown[][]; expect(removeEvents[0][0]).toBe('1'); // id of removed item });
  44. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 58 Need more testing ?
  45. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 60 Snapshot testing Visual testing Contract testing Property- based testing Unit testing … more…
  46. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 61 Continuous Testing === More Confidence
  47. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 62 Back to Basic Why to test What to test When/How to test
  48. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 63 Learn from your mistake
  49. Better Testing © 2017 - 2024 Siam Chamnankit Company Limited.

    All rights reserved. 64 Bug-Driven Development