vue.jsHow do I set up unit testing for a Vue.js application?
Unit testing for a Vue.js application can be set up in several steps.
- Install the necessary packages with the following command:
npm install --save-dev @vue/cli-plugin-unit-jest
- Create the jest configuration file with the following command:
vue-cli-service test:e2e
- Create a test file in the
tests
directory. For example:
// tests/example.spec.js
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallowMount(HelloWorld, {
propsData: { msg }
})
expect(wrapper.text()).toMatch(msg)
})
})
- Run the tests with the following command:
npm run test:unit
- To view the test results, open the generated
jest_html_reporters.html
file in thecoverage
directory.
For more information on unit testing Vue.js applications, see the following links:
More of Vue.js
- How do I download a zip file using Vue.js?
- How can I implement pinch zoom functionality in a Vue.js project?
- How do I change the z-index of a modal in Vue.js?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How do I make an XHR request with Vue.js?
- How do I obtain a Vue.js certification?
- How do I use Yup with Vue.js?
- How to use a YAML editor with Vue.js?
- How do I set a z-index in Vue.js?
- How do I determine which version of Vue.js I am using?
See more codes...