编辑:从another answer我决定尝试从karma-webpack 3.0.5升级到4.0.0-rc.2,我开始收到实际的错误。它开始抱怨没有定义angular,然后我意识到我是从tests.bundle.spec文件导入我的home.spec.js文件,而不是依赖于上下文来导入它(在调试时这样做,然后忘记了)。删除额外的导入后,我的测试成功运行!我会用答案更新这个问题一次,这样我就可以回答我自己的问题了。
我相当确定karma甚至没有加载我的测试包文件,尽管似乎是webpack创建了这个包。
我似乎看不到tests.bundle.spec.js或home.spec.js文件中的任何console.logs。当我使用singleRun=false时,我在刷新后在生成的Chrome窗口中检查控制台(测试应该重新运行),我在网络选项卡中看到tests.bundle.spec.js文件已加载,但在控制台中什么也看不到,html文件中也没有引用它。html页面中加载的唯一脚本是socket.io.js和karma.js。
编辑:从Chrome打开调试页面后,我确实看到我的tests.bundle.spec.js包正在加载,但其中包含的模块都没有运行过。我已经在测试脚本中设置了断点,甚至在tests.bundle.spec.js代码中(例如,在为require设置上下文时),但没有一个断点被触发。我一定是在什么地方遗漏了什么,因为Karma从来不会初始化这些模块中的任何一个。我甚至在__webpack_require__函数中设置了断点,但它们没有被触发。因此,我的任何模块都不会被需要/导入。
Webpack肯定会构建模块,我在控制台的yarn test命令(运行karma start)的输出中看到了这一点:
Entrypoint src/tests.bundle.spec = vendors~src/tests.bundle.spec.bundle.js src/tests.bundle.spec.js
[./ sync recursive home\.spec\.js$] . sync home\.spec\.js$ 192 bytes {src/tests.bundle.spec} [built]这是我的结构/配置
结构:
-src
--app
---home
----home.js
----home.spec.js
--tests.bundle.spec.js
karma.conf.js
webpack.test.jskarma.conf.js
var webpackConfig = require('./webpack.test.js');
module.exports = function (config) {
process.env.BABEL_ENV = 'karma';
config.set({
basePath: '',
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
{
pattern: './src/tests.bundle.spec.js',
watched: false
}
],
// plugins
plugins: [
'karma-webpack',
'karma-jasmine',
'karma-sourcemap-loader',
'karma-chrome-launcher'
],
preprocessors: {
'./src/tests.bundle.spec.js': ['webpack', 'sourcemap']
},
// Webpack config
webpack: webpackConfig,
webpackServer: {
noInfo: false
},
reporters: ['progress'],
// web server port
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: [
'Chrome'
],
singleRun: false,
concurrency: Infinity
})
}webpack.test.js
const webpack = require("webpack");
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
devtool: 'eval-source-map',
entry: {
app: path.resolve(__dirname, './src/index.js')
},
output: {
path: path.resolve(__dirname, './build_app/'),
filename: 'app-[name].js',
chunkFilename: 'app-vendors.[chunkhash].js'
},
module: {
rules: [
// JavaScript source files for the LeadingReach application
{
test: /\.js$/,
exclude: /(node_modules)(\.spec\.js$)/,
rules : [
{
loader: 'babel-loader'
},
{
loader: 'eslint-loader',
options: {
emitError: true,
emitWarning: true,
failOnError: true,
globals: [
'_',
'angular',
'lrenums',
'readJSON'
]
}
}
]
},
// JavaScript test files
{
test: /\.spec.js$/,
exclude: /(node_modules)/,
use : [
'babel-loader'
]
},
// Templates (non-compiled)
{
test: /\.tpl.html$/,
exclude: /\.tpl.html2js$/,
loader: ['file-loader?name=[path][name].[ext]?[hash]', 'extract-loader', 'html-loader']
},
// LESS files
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
// CSS files
{
test: /\.css$/,
loader: ['style-loader', 'css-loader']
},
// Static files
{
test: /\.(jpe?g|gif|png|ico)$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
outputPath: 'assets/images/'
}
}]
},
// Font files
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
outputPath: 'fonts/'
}
}]
}
]
},
optimization: {
namedChunks: true,
splitChunks: {
chunks: "all",
minSize: 0,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'all',
minSize: 0
}
}
}
},
plugins: [
// Clean build_app folder
new CleanWebpackPlugin(['build_app'], {
// Write logs to console.
verbose: true,
// perform clean just before files are emitted to the output dir
// Default: false
beforeEmit: true
}),
// Create our index.php file
new HtmlWebpackPlugin({
template: './src/index.php',
filename: 'index.php',
inject: 'head' // place scripts in head because we bootstrap the app at the end of the body
}),
// Expose _ (underscoreJS) to the global scope
new webpack.ProvidePlugin({
_: 'underscore'
})
]
};tests.bundle.spec.js
const context = require.context('./', true, /.+home\.spec\.js$/);
console.log('================WHEEEEEE==============');
console.log(context.keys());
/*
* For each file, call the context function that will require the file and load it up here.
*/
context.keys().forEach(function(key) {
context(key);
});home.spec.js
// Import dependencies
console.log('============HELLOOOOOOO123123123123==============');
require('angular');
require('angular-mocks');
import './home.js';
console.log('============HELLOOOOOOO==============');
describe('home section', function () {
console.log('============HELLOOOOOOO222222==============');
it('should run test', inject(function () {
expect(1).toEqual(1);
});
}当我的测试运行时,我会得到Executed 0 of 0 ERROR (0.001 secs / 0 secs)
发布于 2020-03-27 03:37:58
最后解决了我自己的问题,很抱歉从最初的帖子开始就延迟了更新答案。
根据另一个答案,我决定尝试从karma-webpack 3.0.5升级到4.0.0-rc.2,我开始收到实际的错误。它开始抱怨没有定义angular,然后我意识到我是从tests.bundle.spec文件导入我的home.spec.js文件,而不是依赖于上下文来导入它(在调试时这样做,然后忘记了)。删除额外的导入后,我的测试成功运行!
发布于 2019-04-08 23:00:56
对我来说,这是一个与optimization.splitChunks相关的问题。在我将其从karma-webpack-config中删除后,我的测试被找到了。
发布于 2020-01-30 16:30:48
你需要放入karma.conf.js
callback: function(){window.__karma__.start()}https://stackoverflow.com/questions/54331636
复制相似问题