Karma没有运行具有“导入”功能的测试。 karma-webpack中的语句 [英] Karma not running tests that have "import" statements in karma-webpack

查看:461
本文介绍了Karma没有运行具有“导入”功能的测试。 karma-webpack中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些测试文件包含我想针对我的应用运行的测试。

I have some test files with tests I'd like to run against my app.

我正在尝试使用 karma karma-webpack karma-babel-preprocessor karma-chrome-launcher jasmine 在我的测试中。我的应用程序取决于很多东西,包括主干 marionette 等。我的应用程序是使用 webpack ,我试图使用 webpack 将我的文件捆绑在一起进行测试。 (我最初想看看我是否可以跳过这一步,即只需 import 一个要测试的文件,但似乎这是不可能的。)

I am attempting to use karma, karma-webpack, karma-babel-preprocessor, karma-chrome-launcher, and jasmine in my testing. My app depends on many things, including backbone, marionette, etc. My app is built using webpack, and I am attempting to use webpack to bundle my files together for testing. (I initially wanted to see if I could skip this step, i.e. simply import a file to be tested, but it seems this is impossible.)

我的测试脚本看起来像

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 语句时,karma将按预期运行并传递/失败。在顶部放置 import 语句将导致karma忽略该文件。没有错误被抛出。

When my files don't have import statements at the top, karma will run and pass/fail as expected. Putting an import statement at the top will cause karma to ignore the file. No errors are thrown.

如何让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);
    });
});

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

the terminal output is: (notice one less test is run)

测试/模型时/devicegroup.spec.js 确实有一个 import 语句:

import backbone from 'backbone';

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

终端输出为:

< a href =https://i.stack.imgur.com/txFz9.png =nofollow noreferrer>

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

I see no errors in the browser Karma opens.

编辑:

我已经通过将我的源文件添加到文件<进行了实验/ code>和预处理器我的 karma.conf.js 文件中的属性,按照此回购示例。除了大量增加的测试时间之外,行为没有变化。

I have experimented by adding my source files to the files and preprocessors attributes in my karma.conf.js file, as per this repo example. There was no change in behavior other than a massively increased testing time.

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:
为了实验(基于这个人的挣扎),我已经在每种可能的组合中尝试了上述karma.conf.js测试文件在文件预处理器中,只有源文件,测试文件在一个而不是另一个,源文件合二为一但没有,没有,两者都没有。没有好的结果,虽然偶尔会出现新的错误。

For the sake of experimentation (and based off this person's struggles), I have tried the above karma.conf.js in every possible combination - only test files in files and preprocessors, only source files, test files in one but not the other, source files in one but not the other, none, both. No good results, though occasionally new errors.

推荐答案

迟到了,但我遇到了同样的问题,并且正在搜索几个小时,为什么我的导入阻止了测试套件被执行。 karma-webpack-4.0.0-rc.2通过提供错误消息带来了启示!!

Little late, but I ran into the same problem, and was searching for hours, why my imports prevent the test suite from being executed. karma-webpack-4.0.0-rc.2 brought the enlightenment by providing error messages!!

我的情况是几个模块找不到, angular-mock jquery angular 和更多。

I my case a couple of modules where not found, angular-mock, jquery, angular and more.

如何修复

将模块放入您的业力中的files数组中.config like:

Put there modules into the files array in your karma.config like:

files = [
  "node_modules/jquery/dist/jquery.js",
  "node_modules/angular/angular.js",
  "node_modules/angular-mocks/angular-mocks.js",
  { pattern: "test/**/*.ts", watched: false }

我希望,这对某人有帮助。

I hope, this helps someone.

编辑

我当前版本的测试相关软件包:

My current versions of the testing related packages:

"@types/jasmine": "^2.8.8",
"jasmine": "^3.2.0",
"jasmine-core": "^3.2.1",
"jasmine-reporters": "2.3.2",
"jasmine-ts": "^0.2.1",
"karma": "3.0.0",
"karma-chrome-launcher": "2.2.0",
"karma-jasmine": "1.1.2",
"karma-junit-reporter": "1.2.0",
"karma-phantomjs-launcher": "1.0.4",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "^4.0.0-rc.2",
"typescript": "3.0.3",
"webpack": "4.17.2",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "3.1.8"

这篇关于Karma没有运行具有“导入”功能的测试。 karma-webpack中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆