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

End-to-End Testing Vue Apps with Cypress

End-to-End Testing Vue Apps with Cypress

Amir Rustamzadeh

November 12, 2019
Tweet

More Decks by Amir Rustamzadeh

Other Decks in Technology

Transcript

  1. @amirrustam + Let’s keep to the conversation going after the

    talk… See something, tweet something. I’ll follow up with all questions and points of interest after the talk.
  2. We need to change our perspective on where testing fits

    within the development lifecycle. @amirrustam
  3. E2E Integration Unit Visit The Testing Pyramid Easy, developers really

    like to write these. More Difficult, costly, slower… Often completely ignored @amirrustam
  4. We need to be able experiment as we’re figuring out

    the implementation. @amirrustam We’re often faced with new and unfamiliar libraries, patterns, components, etc.
  5. @amirrustam Test Apparatus Mechanical Vibration Test System Engine Controller Instrumentation

    Simulate vibration scenarios Attached accelerometers tracked response/performance of implementation
  6. Being able to consistently and reliably simulate real scenarios is

    key to quickly developing a viable implementation. @amirrustam
  7. Enter A tool to quickly and reliably test anything that

    runs in a browser. @amirrustam Free & Open Source (MIT License)
  8. $ npm install -D cypress ☝ All-in-one tool You get

    everything you need ❤ Familiar tools underneath Mocha, Chia, Sinon
  9. Deterministic Execution Order @amirrustam it('send email with contact form', ()

    => { cy.get('#name-input').type('Amir') cy.get('#email-input').type('[email protected]') cy.get('form').submit() cy.get('#success-message').should('be.visible') }) 1 2 3 4 Necessary for flake-free test runs
  10. @amirrustam cy.get('button') .click() .should('have.class', 'active') Cypress will wait up to

    4 seconds (default) for this assertion to pass. Automatic Waiting Cypress will wait for the button to be clickable. Takes the guess-work out of arbitrary timeouts & pauses.
  11. #1 Tip: Always test from the user’s perspective. Don’t waste

    time testing specific implementation. @amirrustam
  12. @amirrustam Layers to Test Network Logic & State Interface Interact

    with the UI Spy & Stub (or mock) network requests Provision internal state and utilize internal logic to in test code Check for Visual Regressions
  13. Spy or Stub Network Requests @amirrustam it('starts with zero items',

    () => { // start Cypress network server // spy on route `GET /todos` // THEN visit the page cy.server() cy.route('GET', '/todos').as('todos') cy.visit('/') cy .wait('@todos') // wait for `GET /todos` response // inspect the server's response .its('response.body') .should('have.length', 0) // then check the DOM cy.get('li.todo').should('have.length', 0) })
  14. Spy or Stub Network Requests @amirrustam it('starts with zero items

    (fixture)', () => { // start Cypress network server // stub route `GET /todos`, return data from fixture file // THEN visit the page cy.server() cy.route('GET', '/todos', ‘fixture:sample-todos').as('todos') cy.visit('/') cy .wait('@todos') // wait for `GET /todos` response // inspect the server's response .its('response.body') .should('have.length', 0) // then check the DOM cy.get('li.todo').should('have.length', 0) })
  15. Utilizing Internal Logic & State @amirrustam const app = new

    Vue({ ... }) if (window.Cypress) { window.__app__ = app }
  16. Utilizing Internal Logic & State @amirrustam const app = new

    Vue({ ... }) if (window.Cypress) { window.__app__ = app }
  17. Utilizing Internal Logic & State @amirrustam cy.window().then(win => { return

    win.app.$store.dispatch('login', { email: '[email protected]', password: '1234' }) }) Use your app code to help you write your test code. It can be more efficient, and can provide a higher level of confidence.
  18. Utilizing Internal Logic & State @amirrustam cy.window().then(win => { return

    win.app.$store.commit(‘SET_TODOS’, {…}) }) Provision the state of your app for testing
  19. Utilizing Internal Logic & State @amirrustam cy.window().then(win => { cy.wait('@todos')

    .its('response.body.todos') .should('have.length', win.app.$store.state.todos.length) })
  20. Isolated Component Tests @amirrustam import mount from 'cypress-vue-unit-test' import MyComponent

    from './app/components/MyComponent.vue' describe('My Component', () => { beforeEach(mount(MyComponent)) it('loads', () => { cy.get('button').should('be.visible') }) }) Experimental
  21. @amirrustam Visual Regression Testing OSS Options and Paid Services on.cypress.io/visual-testing

    https://applitools.com/tutorials/cypress.html https://docs.percy.io/docs/cypress
  22. @amirrustam Code Coverage Use it to help guide your tests,

    but it’s not a silver-bullet. Write the minimum set of E2E tests to maximize coverage. Consider unit-tests for unreachable part of your code. Or refactor.
  23. @amirrustam Code Coverage Use it to help guide your tests,

    but it’s not a silver-bullet. on.cypress.io/code-coverage
  24. * cypress open Great for everyday development workflow Cypress +

    Editor Side-by-Side TDD Nirvana cypress run Testing in CI/CD Pipeline Headless More efficient for running all your tests @amirrustam
  25. cypress run --parallel cypress run --parallel cypress run --parallel Your

    CI Machines 3x parallelism Ready for work signup.spec.js login.spec.js widget.spec.js @amirrustam
  26. Previous Spec Test Runs Spec Test Duration History Test Duration

    (seconds) The Future Automatic Load Balancing of Test Specs Duration Forecast @amirrustam
  27. @amirrustam Upcoming features from the Roadmap Full Network Layer Stubbing:

    You’ll be the master of the network. Great for handling GraphQL and fetch requests. Improved Error Reporting: From error to the point of the problem in your cord. Better communication of context around errors. Test Retries: Identify flake and save broken CI builds due to false negatives.
  28. @amirrustam Check out the Docs No really, do it. They

    are some of the best you’ll ever read. docs.cypress.io
  29. @amirrustam There is a plugin for that Plugins for accessibility

    testing, visual testing, helpful utilities, etc. docs.cypress.io
  30. @amirrustam We’re trying to change the status quo of testing.

    We want to testing to be enjoyable and productive. We’re only getting started.