Unit-tests produced by vue-cli throw exceptions

926 Views Asked by At

After creating a project with vue-cli(v3.5.1), the unit test that was auto-created is failing when I run: 'npm run test:unit'.

After running 'vue create', I manually selected the following options: TS, Router, Vuex, Linter, Unit, E2E, class-style component syntax: yes, Babel: no, history mode: no, linter: TSLint, Lint on save, unit-test solution: Mocha, E2E test solution Cypress, config files: dedicated filesenter image description here.

The output of 'npm run test:unit' is below.

WEBPACK Compiled successfully in 7321ms
MOCHA Testing...
RUNTIME EXCEPTION Exception occurred while loading your tests
ReferenceError: performance is not defined at Module../node_modules/vue/dist/vue.runtime.esm.js

I attempted to modify the npm script used to run the test as below(wasn't really sure if the include was coming from Node, and I don't think that it should be), but I just get an error stating that perf_hooks wasn't found in \node_modules\mocha-webpack\lib.

"test:unit": "vue-cli-service test:unit --include perf_hooks"

Code example: https://github.com/derek-baker/super-duper-guacamole

2

There are 2 best solutions below

0
On BEST ANSWER

If, in addition to the options above, I opt to include Babel as a feature, and also use Babel alongside TypeScript for auto-detected polyfills, the unit-test runs as expected. Example of working config is below.

{
  "name": "vue-cli-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "test:e2e": "vue-cli-service test:e2e",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "vue": "^2.6.6",
    "vue-class-component": "^6.0.0",
    "vue-property-decorator": "^7.0.0",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@types/chai": "^4.1.0",
    "@types/mocha": "^5.2.4",
    "@vue/cli-plugin-babel": "^3.5.0",
    "@vue/cli-plugin-e2e-cypress": "^3.5.0",
    "@vue/cli-plugin-typescript": "^3.5.0",
    "@vue/cli-plugin-unit-mocha": "^3.5.0",
    "@vue/cli-service": "^3.5.0",
    "@vue/test-utils": "1.0.0-beta.29",
    "chai": "^4.1.2",
    "typescript": "^3.2.1",
    "vue-template-compiler": "^2.5.21"
  }
}
0
On

You have to falsify isServerBuild there's an open issue https://github.com/vuejs/vue-cli/issues/2270#issue-351812395

mocha webpack refrence https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-unit-mocha/index.js#L5

quick remedy:

// vue.config.js
module.exports = {
    chainWebpack: (config) => {
      if (process.env.NODE_ENV === 'test') {    
        config.merge({
            // target: 'node',
            devtool: 'inline-cheap-module-source-map'
        })
        config.module
        .rule('vue')
          .use('vue-loader')
          .tap(options => {
            options.isServerBuild = false
            return options
          })
        } 
    },  
        
    
  };