vuejs unit testing, default installation says no test specified

Viewed 1891

On Windows 7. Installed a fresh vue project using the VueJS UI utility. Set unit testing / Jest enabled. Added babel.

running "npm test" at the command line returns 'Error: no test specified'.

I have the typical "scripts": { "vue-cli-service test:unit" } set up in my package.json file.

jest.config.js has the expected matching pattern:

{  testMatch: [
    '**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
  ] }

So, it's not finding even the example file located in tests/unit/example.spec.js.

Curiously, I CAN get it to find tests I run directly from the node_modules directory, using vue-cli-service:

node_modules\.bin\vue-cli-service test:unit

from there I get a babel transpile problem (it fails to recognize the ES6 import command in the example.spec.js file) .. which is another nightmare, perhaps not for this post.

Tried updating npm (using 6.9.2). Vue cli version is 3.8.2. Tried deleting the node_modules directory. Any help is most appreciated.

After running npm test, the error looks like this:

> testing_research@0.1.0 test C:\Users\allan.luchenitser\sandbox\testing_research
> echo 'Error: no test specified'

'Error: no test specified'

thanks again,

1 Answers

Your package.json's "scripts" should contain a named script. Example:

"scripts": {
  // other scripts...
  "test:unit": "vue-cli-service test:unit"
}

In addition, you have to configure jest to parse any .js or .ts test files in your project, adding this configuration to your package.json:

"jest": {
  "moduleFileExtensions": [
    "js",
    "ts",
    "json",
    "vue"
  ],
  "transform": {
    ".*\\.(vue)$": "vue-jest"
  }
}

Now running npm run test:unit should find and run any tests you have in your project.

Related