如何设置 Karma + Jasmine 使用 ES6 模块

Fir*_*DoL 5 javascript jasmine karma-runner systemjs babeljs

我对此有点困惑。我有一个由 middleman、karma、jasmine、babeljs 组成的复杂堆栈来构建静态网站。

考虑到这是一个实验,我想使用带有模块的 ES6。不过,中间人方面一切都很好,我在设置 karma + jasmine 进行测试时遇到了困难。

主要问题在于 babel 中:如果您将其设置为使用,modules: "ignore"则必须手动使用规范中的所有System.import模块,这是我不想要的。我想使用 ES6 语法,但如果我设置,babeljs 会将我的所有测试包装到 中,如下所示:modules: "system"System.register

System.register(["mymodule"], function (_export) {
  "use strict";

  var Mymodule;
  return {
    setters: [function (_mymodule) {
      Mymodule = _mymodule["default"];
    }],
    execute: function () {

      console.log("I'm here!!!");
      console.log(Mymodule);

      describe("Mymodule", function () {

        it("has version", function () {
          expect(Mymodule.VERSION).toEqual("1.0.0");
        });


      });
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

因此测试不会自动执行。然后,我创建了以下脚本来解决这个问题(在包含所有规格后包含该脚本):

basePath = "/base/spec/"

modules = []

for own fileName, fileHash of window.__karma__.files
  if fileName.indexOf(basePath) is 0
    isRunner   = fileName.indexOf("spec_runner") >= 0
    isRunner ||= fileName.indexOf("spec_helper") >= 0
    unless isRunner
      moduleName = fileName.replace(basePath, "")
      moduleName = moduleName.replace(".js", "")
      modules.push(path: fileName, name: moduleName)

mappedModules = {}
baseSpecsPath = "http://localhost:9876"

for module in modules
  mappedModules[module.name] = baseSpecsPath + module.path

System.config
  baseURL: "http://localhost:4567/javascripts/"
  map:     mappedModules

for module in modules
  System.import(module.name)
Run Code Online (Sandbox Code Playgroud)

这段代码很简单:它为 SystemJS 准备地图配置,我可以正确地从我的应用程序加载模块(位于http://localhost:4567)并包装在 System.register 中的测试(位于http://localhost:9876) 。

但是,我的测试没有运行,也没有报告错误。更糟糕的是,我正确地记录了消息“我在这里!!!” 并且 Mymodule 已正确登录控制台。我什至尝试记录描述的值,它正确地是一个Suite对象。那么,到底为什么我的测试没有运行?(该it块永远不会运行)

我有什么解决办法?我可以稍微更改设置以使其正常工作,但我想保留以下内容:中间人、ES6 模块、无动态模块加载(我的所有模块最终都暴露在单个文件中或需要有一堆<script>标签),茉莉花

Fir*_*DoL 2

我终于解决了这个问题。我将此文件作为最后一个文件

basePath = "/base/spec/"

modules = []

for own fileName, fileHash of window.__karma__.files
  if fileName.indexOf(basePath) is 0
    isRunner   = fileName.indexOf("spec_runner") >= 0
    isRunner ||= fileName.indexOf("spec_helper") >= 0
    unless isRunner
      moduleName = fileName.replace(basePath, "")
      moduleName = moduleName.replace(".js", "")
      modules.push(path: fileName, name: moduleName)

mappedModules = {}
baseSpecsPath = "http://localhost:9876"
specModules   = []

for module in modules
  mappedModules[module.name] = baseSpecsPath + module.path
  specModules.push(module.name)

System.config
  baseURL: "http://localhost:4567/javascripts/"
  map:     mappedModules

moduleImports = specModules.map (moduleName) ->
  System.import(moduleName)

Promise.all(moduleImports).then ->
  window.__karma__.start     = window.__lastKarmaStart__
  window.__lastKarmaStart__  = null
  delete window.__lastKarmaStart__
  window.__karma__.start()
Run Code Online (Sandbox Code Playgroud)

它执行以下操作:

  • 暂时禁用业力启动功能,将其替换为空的
  • 获取存储在 中的每个测试System.register,运行System.import所有测试(使用 等待它们Promise.all
  • 所有导入完成后,它会附加回来__karma__start并执行它,运行 Jasmine