Vue Cli unit test with jest how to run single test
Asked Answered
E

3

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.

Eugeneeugenia answered 27/2, 2019 at 15:15 Comment(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"Eugeneeugenia
Does this answer your question? How do I run a single test using Jest?Algy
D
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.

Diez answered 6/10, 2019 at 7:30 Comment(2)
This does not work for me. It simply says 0 passing and runs no tests.Chillon
I had to do npm run test:unit -t tests/unit/Foo.spec.ts.Chillon
O
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

Obscurant answered 14/3, 2019 at 20:48 Comment(2)
This doesn't answer the question. This runs all the tests within that specific spec file, and not "a single test"Shaeshaef
@coler-j, you could try it.only(...) alongside with the testPathPattern option.Chism
C
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.

Culvert answered 15/5, 2020 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.