【发布时间】:2018-06-05 21:27:34
【问题描述】:
我有一些测试文件,其中包含我想针对我的应用运行的测试。
我正在尝试在我的测试中使用karma、karma-webpack、karma-babel-preprocessor、karma-chrome-launcher 和jasmine。我的应用程序依赖于很多东西,包括backbone、marionette 等。我的应用程序是使用webpack 构建的,我正在尝试使用webpack 将我的文件捆绑在一起进行测试。 (我最初想看看是否可以跳过这一步,即简单地 import 一个要测试的文件,但似乎这是不可能的。)
我的测试脚本看起来像
package.json(脚本部分)
"test": "./node_modules/karma/bin/karma start",
其余文件:
karma.conf.js
var webpackConfig = require('./config/webpack/webpack.test.conf.js');
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
{ pattern: 'test/**/*.spec.js', watched: true },
{ pattern: 'test/*.spec.js', watched: true }
],
exclude: [
],
preprocessors: {
'test/**/*.spec.js': ['webpack'],
'test/*.spec.js': ['webpack']
},
webpack: webpackConfig,
webpackMiddleware: {
stats: 'errors-only'
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
test/test.spec.js这个文件可见
describe("A suite", function () {
it("contains spec with an expectation", function () {
expect(true).toBe(true);
});
});
describe("Another suite", function () {
it("contains another spec with an expectation", function () {
expect(true).toBe(false);
});
});
test/models/devicegroup.spec.js没有看到这个文件
import backbone from 'backbone';
describe("backbone", function () {
it("containsasdfasdfasdfasdfspec with an expectation", function ()
{
expect(true).toBe(false);
});
});
我的文件夹结构是:
- karma.conf.js
- test/
- - test.spec.js
- - models/
- - - devicegroup.spec.js
- public/
- - js/
- - - app.js
当我的文件顶部没有 import 语句时,业力将按预期运行并通过/失败。在顶部放置 import 语句将导致 karma 忽略该文件。没有错误被抛出。
如何让 karma / karma-webpack 运行包含 import 语句的测试 / 将模块导入测试的 karma 安全方式是什么?
当 test/models/devicegroup.spec.js 没有import 语句时:
// import backbone from 'backbone';
describe("backbone", function () {
it("contains with an expectation", function () {
expect(true).toBe(false);
});
});
终端输出是:(注意少了一个测试)
当 test/models/devicegroup.spec.js 确实有 import 声明时:
import backbone from 'backbone';
describe("backbone", function () {
it("contains with an expectation", function () {
expect(true).toBe(false);
});
});
终端输出为:
我在 Karma 打开的浏览器中没有看到任何错误。
编辑:
根据this repo example,我已经尝试将我的源文件添加到我的karma.conf.js 文件中的files 和preprocessors 属性。除了大量增加测试时间外,行为没有任何变化。
karma.conf.js
files: [
{ pattern: 'public/js/**/*.js', watched: true},
{ pattern: 'test/**/*.spec.js', watched: true },
// each file acts as entry point for the webpack configuration
],
preprocessors: {
// add webpack as preprocessor
'public/js/**/*.js': ['webpack'],
'test/**/*.spec.js': ['webpack'],
},
编辑2:
为了实验 (and based off this person's struggles),我已经在所有可能的组合中尝试了上述 karma.conf.js - 仅测试 files 和 preprocessors 中的文件,仅源文件,一个中的测试文件,而不是另一个中的测试文件, 源文件在一个但不是另一个,没有,两者都有。没有好的结果,虽然偶尔会出现新的错误。
【问题讨论】:
-
您是否注意到框架:['jasmine'],{模式:'test/**/*.spec.js',观看:true },{模式:'test/models/*. spec.js', seen: true } ] 缺少一个方括号(或者有一个额外的,不确定)?
-
这可能是一个复制/粘贴工件,我在我的代码编辑器中没有看到它......虽然让我仔细看一下,但请稍等
-
感谢您指出,这是一个复制/粘贴错误,上面的代码现在完全反映了我在本地拥有的内容
-
你有没有找到解决这个问题的方法,因为我也遇到了同样的事情。
-
不,我改用 Jest。
标签: javascript webpack karma-jasmine karma-runner karma-webpack