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

2

22

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.

Balkh answered 30/4, 2019 at 5:4 Comment(0)
G
34

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.
Gaillardia answered 30/4, 2019 at 5:13 Comment(3)
Getting...Error: Cannot find module '@babel/traverse' when tried to run npm run test:unit – Brandenbrandenburg
@Brandenbrandenburg I cannot reproduce the issue with Vue CLI 5.0.4. Please explain how that comment is related to the answer. – Gaillardia
After updating the Node, don't see that issue. – Brandenbrandenburg
T
3

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",
    ...
},
Tufa answered 22/2, 2020 at 11:34 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.