我有一些测试文件,我想对我的应用程序运行测试。
我试图在我的测试中使用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-webpack运行我的测试,这些测试具有import语句/什么是将模块导入我的测试的业力安全方法?
当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打开的浏览器没有错误。
编辑:
我根据files
将我的源文件添加到preprocessors
文件中的karma.conf.js
和this repo example属性进行了实验。除了大量增加的测试时间之外,行为没有变化。
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'],
},
EDIT2:为了实验(and based off this person's struggles),我在每种可能的组合中都尝试了上面的karma.conf.js - 只测试files
和preprocessors
中的文件,只有源文件,测试文件在一个但不是另一个,源文件在一个但不是另一个,没有,两个。没有好的结果,虽然偶尔会出现新的错误。