23
votes

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.

3
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

3 Answers

24
votes

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.

6
votes

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

2
votes

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.