Karma没有在karma-webpack中运行具有“import”语句的测试

问题描述 投票:5回答:1

我有一些测试文件,我想对我的应用程序运行测试。

我试图在我的测试中使用karmakarma-webpackkarma-babel-preprocessorkarma-chrome-launcherjasmine。我的应用程序取决于很多东西,包括backbonemarionette等。我的应用程序是使用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);
    });
});

终端输出是:(注意少运行一次测试)

Screenshot of terminal output karma successfully running test of file without import statement

当test / models / devicegroup.spec.js确实有import语句时:

import backbone from 'backbone';

describe("backbone", function () {
    it("contains with an expectation", function () {
        expect(true).toBe(false);
    });
});

终端输出是:

Screenshot of terminal output of karma when not able to see files with import statements

我看到Karma打开的浏览器没有错误。

编辑:

我根据files将我的源文件添加到preprocessors文件中的karma.conf.jsthis 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 - 只测试filespreprocessors中的文件,只有源文件,测试文件在一个但不是另一个,源文件在一个但不是另一个,没有,两个。没有好的结果,虽然偶尔会出现新的错误。

javascript webpack karma-jasmine karma-runner karma-webpack
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.