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.

2
  • 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" Commented Feb 27, 2019 at 15:26
  • Does this answer your question? How do I run a single test using Jest? Commented Aug 13, 2021 at 10:13

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This does not work for me. It simply says 0 passing and runs no tests.
I had to do npm run test:unit -t tests/unit/Foo.spec.ts.
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

2 Comments

This doesn't answer the question. This runs all the tests within that specific spec file, and not "a single test"
@coler-j, you could try it.only(...) alongside with the testPathPattern option.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.