Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test vue components #85

Open
Raruto opened this issue Jul 26, 2022 · 0 comments
Open

Unit test vue components #85

Raruto opened this issue Jul 26, 2022 · 0 comments
Labels
help wanted Extra attention is needed tests Edit or add tests
Milestone

Comments

@Raruto
Copy link
Collaborator

Raruto commented Jul 26, 2022

Related to: #83

Reference:

Vue’s single-file components make it straight forward to write unit tests for components in isolation. This lets you develop new features with confidence you are not breaking existing ones, and helps other developers understand what your component does.

Component unit tests have lots of benefits:

  • Provide documentation on how the component should behave
  • Save time over testing manually
  • Reduce bugs in new features
  • Improve design
  • Facilitate refactoring

Unit testing is an important part of any serious application. At first, when the vision of an application is not clear, unit testing might slow down development, but once a vision is established and real users will be interacting with the application, unit tests (and other types of automated tests) are absolutely essential to ensure the codebase is maintainable and scalable.

Test example:

<template>
  <div>
    <div class="message">
      {{ message }}
    </div>
    Enter your username: <input v-model="username">
    <div
      v-if="error"
      class="error"
    >
      Please enter a username with at least seven letters.
    </div>
  </div>
</template>

<script>
export default {
  name: 'Foo',

  data () {
    return {
      message: 'Welcome to the Vue.js cookbook',
      username: ''
    }
  },

  computed: {
    error () {
      return this.username.trim().length < 7
    }
  }
}
</script>

The things that we should test are:

  • is the message rendered?
  • if error is true, <div class="error"> should be present
  • if error is false, <div class="error"> should not be present
import { shallowMount } from '@vue/test-utils'
import Foo from './Foo'

const factory = (values = {}) => {
  return shallowMount(Foo, {
    data () {
      return {
        ...values
      }
    }
  })
}

describe('Foo', () => {
  it('renders a welcome message', () => {
    const wrapper = factory()

    expect(wrapper.find('.message').text()).toEqual("Welcome to the Vue.js cookbook")
  })

  it('renders an error when username is less than 7 characters', () => {
    const wrapper = factory({ username: ''  })

    expect(wrapper.find('.error').exists()).toBeTruthy()
  })

  it('renders an error when username is whitespace', () => {
    const wrapper = factory({ username: ' '.repeat(7) })

    expect(wrapper.find('.error').exists()).toBeTruthy()
  })

  it('does not render an error when username is 7 characters or more', () => {
    const wrapper = factory({ username: 'Lachlan'  })

    expect(wrapper.find('.error').exists()).toBeFalsy()
  })
})
@Raruto Raruto added help wanted Extra attention is needed feature New feature or request labels Jul 28, 2022
@Raruto Raruto added this to the 3.7 milestone Jul 29, 2022
@Raruto Raruto added needs discussion This issue or pull requires further discussion by the dev team tests Edit or add tests help wanted Extra attention is needed and removed feature New feature or request help wanted Extra attention is needed needs discussion This issue or pull requires further discussion by the dev team labels Aug 5, 2022
@Raruto Raruto modified the milestones: v3.7, v3.9 Jan 11, 2023
@Raruto Raruto modified the milestones: v3.9, v3.10 Oct 19, 2023
@Raruto Raruto modified the milestones: v3.10, v4.0 Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed tests Edit or add tests
Projects
None yet
Development

No branches or pull requests

1 participant