Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Cli unit test with jest how to run single test

I have vue application using the vue cli 3. During the setup process i chose jest as the testing framework. To run my unit tests i have a script in the package.json:

test:unit": "vue-cli-service test:unit", 

and to run this i write in the vs code terminal:

npm run test:unit 

This runs all my tests that meet the specifications set up in the jest config section of the package.json file.

My question is how to run just a single test. Is there a specific command i need to run? or is there an vscode extension that will work with this setup.

like image 341
JimmyShoe Avatar asked Feb 27 '19 15:02

JimmyShoe


People also ask

How do I run unit tests at Vue?

Run the unit tests for a Vue project. Utilize the beforeEach() and afterEach() functions within a unit test suite. Write unit tests for testing the implementation details of a Vue component.

What does createLocalVue do?

createLocalVue returns a Vue class for you to add components, mixins and install plugins without polluting the global Vue class. The errorHandler option can be used to handle uncaught errors during component render function and watchers.


2 Answers

If you want to execute only single file then simply do:

npm run test:unit -t counter OR npm run test:unit counter 

Whereas counter.spec.js is the my test file.

like image 164
Hardik Raval Avatar answered Sep 27 '22 22:09

Hardik Raval


The Vue CLI service respects Jest's CLI options, so you can append them to commands in package.json, e.g

{   ...   "scripts": {     "serve": "vue-cli-service serve",     "build": "vue-cli-service build",     "test:unit": "vue-cli-service test:unit",     "test:only": "vue-cli-service test:unit --testPathPattern=user.store",   },   "dependencies": {  

testPathPattern takes a regex which applies to the spec file names, so in my tests if I specify the pattern

--testPathPattern=user.store   

I run a single spec file, but if I specify

--testPathPattern=store 

I run multiple matching spec files with store in the name.

Here is the Jest docs ref

like image 29
Richard Matsen Avatar answered Sep 27 '22 21:09

Richard Matsen