Y
Y
Yuri Valdis2020-03-06 19:04:11
Unit testing
Yuri Valdis, 2020-03-06 19:04:11

What's wrong with setting up vue + mocha?

The problem is setting up the test environment (Vue + Mocha + chai).
Code related to tests:

in package.json

"test": "nyc mocha-webpack --webpack-config build/webpack.test.conf.js --require test/setup.js test/**/*.spec.js"

"nyc": {
    "include": [
      "src/**/*.(js|vue)"
    ],
    "instrument": false,
    "sourceMap": false
  }

in webpack.test
output: {
    devtoolModuleFilenameTemplate: '[absolute-resource-path]',
    devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
  },
  devtool: 'inline-cheap-module-source-map',
  externals: [
    nodeExternals()
  ]

in babel
{
  "presets": [
    ["env", {
      "modules": false
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"]
}


in test/setup.js
require('jsdom-global')()
global.expect = require('expect')


the test itself
import { shallowMount } from '@vue/test-utils'
import { expect } from "chai"
import Counter from '../src/components/Counter.vue'

describe('Counter.vue', () => {
  it('увеличивает счётчик по нажатию кнопки', () => {
    const wrapper = shallowMount(Counter)
    wrapper.find('button').trigger('click')
    expect(wrapper.find('div').text()).toMatch('1')
  })
})


Error on startup
node_modules\jsdom\lib\jsdom\living\helpers\dates-and-times.js:235
  } catch {
          ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question