31

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.

JimmyShoe
  • 2,009
  • 4
  • 21
  • 40
  • i have tried to install jest-runner extension for vscode but when i try to run a test it always says "test suite failed to run" – JimmyShoe Feb 27 '19 at 15:26
  • Does this answer your question? [How do I run a single test using Jest?](https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest) – stanimirsp Aug 13 '21 at 10:13

3 Answers3

42

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.

Hardik Raval
  • 3,406
  • 1
  • 26
  • 28
7

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

Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
3

For that you can use the only method. It can be chained on to the test method directly.

myunittests.spec.js

describe('My Unit Tests', () => {
    test('My excluded test', () => {
        ...
    })

    test.only('my single test', () => {
        ...
    })
})

After that you can run the tests by running npm run test:unit -t myunittests.

There is also a skip method that can be chained.

myunittests.spec.js

describe('My Unit Tests', () => {
    test('My excluded test', () => {
        ...
    })

    test.skip('my single test', () => {
        ...
    })
})

By running npm run test:unit -t myunittests again you will see all 'other' tests are running.

Tarabass
  • 3,132
  • 2
  • 17
  • 35