Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Vue Test Utils with Jest to already existing Vue-CLI 3 project?

Tags:

I want to test an already existing Vue-CLI 3 project. I haven't initialized the testing preset when I was creating the project. I have searched, but haven't found any suitable results. I read the documentation also, but it said to add testing when creating the project.

like image 881
Lucifer Avatar asked Apr 30 '19 05:04

Lucifer


People also ask

Is Vue and Vue CLI the same?

The CLI ( @vue/cli ) is a globally installed npm package and provides the vue command in your terminal. It provides the ability to quickly scaffold a new project via vue create . You can also manage your projects using a graphical user interface via vue ui .

How do I add jest testing to a Vue app?

Let’s create a Vue app called alligator-test. Choose the default preset at the prompt (hit the enter key). After that, run the following command to add our testing dependencies ( @vue/cli-plugin-unit-jest and @vue/test-utils ): Next, modify your project’s package.json file to have an entry in scripts which says "test": "jest".

How to unit test a vuejs project?

For unit testing Vue projects, VueJS created a utility project for testing Vue apps. So, the utilities will create the configuration for running tests. We can test any Vue project in Karma, Jest or Mocha + webpack.

How to add jest and Mocha tests with Vue-CLI?

Vue CLI has built-in support of adding Jest and Mocha tests while creating with vue-cli. If you select either of them, it will add configurations required for testing while creating a new Vue project.

How to use VUE test utils to test Vue components?

In the test file, Vue Test Utils should be imported first, and we also need to import the vue component we want to test. The example code is as below. When import Vue Test Utils, mount or shallowMount should be imported. They both could be used to wrap vue components and render them when testing. These two method work similarly.


2 Answers

From your project root directory, enter the following command to add @vue/test-utils and jest:

vue add unit-jest 

The command output should look similar to this:

$ vue add unit-jest  πŸ“¦  Installing @vue/cli-plugin-unit-jest...  + @vue/[email protected] added 282 packages from 167 contributors, removed 2 packages and audited 42205 packages in 9.693s found 63 low severity vulnerabilities   run `npm audit fix` to fix them, or `npm audit` for details βœ”  Successfully installed plugin: @vue/cli-plugin-unit-jest   πŸš€  Invoking generator for @vue/cli-plugin-unit-jest... πŸ“¦  Installing additional dependencies...  added 12 packages from 11 contributors, updated 1 package, moved 4 packages and audited 42427 packages in 7.895s found 64 low severity vulnerabilities   run `npm audit fix` to fix them, or `npm audit` for details βœ”  Successfully invoked generator for plugin: @vue/cli-plugin-unit-jest    The following files have been updated / added:       jest.config.js      tests/unit/.eslintrc.js      tests/unit/example.spec.js      package-lock.json      package.json     You should review these changes with git diff and commit them. 
like image 144
tony19 Avatar answered Sep 18 '22 03:09

tony19


I was successful with the following plugin:

@vue/cli-plugin-unit-jest ([https://www.npmjs.com/package/@vue/cli-plugin-unit-jest)

I installed it, from the root folder, using the command:

vue add unit-jest 

The command already adds an entry to my package.json file:

"scripts": {     ...     "test:unit": "vue-cli-service test:unit",     ... }, 
like image 25
Evandro Severgnini Avatar answered Sep 20 '22 03:09

Evandro Severgnini