草庐IT

javascript - Karma 未运行在 karma-webpack 中具有 "import"语句的测试

coder 2024-12-29 原文

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

我正在尝试使用 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 语句时,karma 将按预期运行并通过/失败。将 import 语句放在顶部将导致 karma 忽略该文件。不会抛出任何错误。

如何让 karma/karma-webpack 运行我的具有导入语句的测试/将模块导入我的测试的 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 文件中的 filespreprocessors 属性.除了测试时间大幅增加外,行为没有任何变化。

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 - 仅测试 filespreprocessors 中的文件,仅源文件,测试文件在一个而不是另一个,源文件在一个而不是另一个,没有,两个。没有好的结果,虽然偶尔会出现新的错误。

最佳答案

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

我的情况是有几个模块没有找到,angular-mockjqueryangular 等等。

如何修复

将模块放入您的 karma.config 中的文件数组中,例如:

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

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

编辑

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

"@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"

关于javascript - Karma 未运行在 karma-webpack 中具有 "import"语句的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50709191/

有关javascript - Karma 未运行在 karma-webpack 中具有 "import"语句的测试的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  5. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  10. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

随机推荐