我正在尝试对用Electron-Vue制作的应用程序进行单元测试 https://github.com/SimulatedGREG/electron-vue
我的程序使用的vuex既可以在开发中也可以在生产中使用,但是我正在尝试使业力正常工作,而且好像出现了错误。
我使用命令yarn unit
运行。
我的package.json看起来像这样:
"unit": "karma start test/unit/karma.conf.js"
排除错误如下:
15 04 2019 11:07:38.406:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/
15 04 2019 11:07:38.408:INFO [launcher]: Launching browser visibleElectron with unlimited concurrency
15 04 2019 11:07:38.411:INFO [launcher]: Starting browser Electron
15 04 2019 11:07:39.531:INFO [Electron 3.1.8 (Node 10.2.0)]: Connected on socket ZQnCuOzcgaybwZ1nAAAA with id 56543028
Electron 3.1.8 (Node 10.2.0) ERROR
{
"message": "Uncaught ReferenceError: vuex is not defined\nat webpack:///external%20%22vuex%22:1 <- index.js:16247:18\n\nundefined",
"str": "Uncaught ReferenceError: vuex is not defined\nat webpack:///external%20%22vuex%22:1 <- index.js:16247:18\n\nundefined"
}
Electron 3.1.8 (Node 10.2.0): Executed 0 of 0 ERROR (0.682 secs / 0 secs)
error Command failed with exit code 1.
我在测试中有一个karma.conf.js文件,其中包含以下内容:
'use strict'
const path = require('path')
const merge = require('webpack-merge')
const webpack = require('webpack')
const baseConfig = require('../../.electron-vue/webpack.renderer.config')
const projectRoot = path.resolve(__dirname, '../../src/renderer')
// Set BABEL_ENV to use proper preset config
process.env.BABEL_ENV = 'test'
let webpackConfig = merge(baseConfig, {
devtool: '#inline-source-map',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"testing"'
})
],
mode: "none"
})
// don't treat dependencies as externals
delete webpackConfig.entry
// delete webpackConfig.externals // causes module not found import errors
delete webpackConfig.output.libraryTarget
// apply vue option to apply isparta-loader on js
webpackConfig.module.rules
.find(rule => rule.use.loader === 'vue-loader').use.options.loaders.js = 'babel-loader'
module.exports = config => {
config.set({
browsers: ['visibleElectron'],
client: {
useIframe: false
},
coverageReporter: {
dir: './coverage',
reporters: [
{ type: 'lcov', subdir: '.' },
{ type: 'text-summary' }
]
},
customLaunchers: {
'visibleElectron': {
base: 'Electron',
flags: ['--show']
}
},
frameworks: ['mocha', 'chai'],
files: ['./index.js'],
preprocessors: {
'./index.js': ['webpack', 'sourcemap']
},
reporters: ['spec', 'coverage'],
singleRun: true,
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
}
})
}