Jasmine 2.0真的不能与require.js一起使用吗?

29

我正在设置我的SpecRunner.html/.js、RequireConfig.js、我的路径和我的shims,就像我之前使用Jasmine + RequireJs的早期发布候选版一样,但现在我的测试方法显示Jasmine未定义。他们最近改变了加载Jasmine的不同方法,我理解这种方式与RequireJs不兼容。

我的理解是正确的吗?如果是,那我们将来还能使用Jasmine + RequireJs吗?

2个回答

70
新的boot.js文件对初始化进行了一些操作,并将其附加到已由require.js加载Jasmine时已经调用的window.onload()上。您可以手动调用window.onload()来初始化HTML报告生成器并执行环境。
SpecRunner.html
<!doctype html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Jasmine Spec Runner v2.0.0</title>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
    <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">

    <!-- specRunner.js runs all of the tests -->
    <script data-main="specRunner" src="../bower_components/requirejs/require.js"></script>
  </head>
  <body>
  </body>
</html>

specRunner.js

(function() {
  'use strict';

  // Configure RequireJS to shim Jasmine
  require.config({
    baseUrl: '..',
    paths: {
      'jasmine': 'tests/lib/jasmine-2.0.0/jasmine',
      'jasmine-html': 'tests/lib/jasmine-2.0.0/jasmine-html',
      'boot': 'tests/lib/jasmine-2.0.0/boot'
    },
    shim: {
      'jasmine': {
        exports: 'window.jasmineRequire'
      },
      'jasmine-html': {
        deps: ['jasmine'],
        exports: 'window.jasmineRequire'
      },
      'boot': {
        deps: ['jasmine', 'jasmine-html'],
        exports: 'window.jasmineRequire'
      }
    }
  });

  // Define all of your specs here. These are RequireJS modules.
  var specs = [
    'tests/spec/routerSpec'
  ];

  // Load Jasmine - This will still create all of the normal Jasmine browser globals unless `boot.js` is re-written to use the
  // AMD or UMD specs. `boot.js` will do a bunch of configuration and attach it's initializers to `window.onload()`. Because
  // we are using RequireJS `window.onload()` has already been triggered so we have to manually call it again. This will
  // initialize the HTML Reporter and execute the environment.
  require(['boot'], function () {

    // Load the specs
    require(specs, function () {

      // Initialize the HTML Reporter and execute the environment (setup by `boot.js`)
      window.onload();
    });
  });
})();

示例规格

define(['router'], function(router) {
  'use strict';

  describe('router', function() {
    it('should have routes defined', function() {
      router.config({});
      expect(router.routes).toBeTruthy();
    });
  });
});

谢谢您的回答。我花了一个下午来尝试让它工作。 - keepitreal
我刚刚偶然发现这个,它运行得非常好!我甚至通过修改 boot.js 文件并让它在我选择的 div 中加载,将其附加到了一个视图上。 - Joshua
1
老兄,非常感谢!我已经折腾了很久,但这个让我立刻开始了。 - medoingthings
1
这个答案帮了很多忙,但调用window.onload()也会导致其他代码重新运行。最终使用了一个自定义的boot.js文件,将window.onload()移除并放置在一个函数中。请参考https://github.com/erikringsmuth/jasmine2-amd-specrunner/。 - RockResolve
非常感谢您!! - Mariama Mariama

13
这里有一个替代方案,对于一些情况可能更简单-使用Jasmine的异步支持在执行测试之前加载你的AMD模块,像这样:
MySpec.js中:
describe('A suite', function() {
  var myModule;

  // Use require.js to fetch the module
  it("should load the AMD module", function(done) {
    require(['myModule'], function (loadedModule) {
      myModule = loadedModule;
      done();
    });
  });

  //run tests that use the myModule object
  it("can access the AMD module", function() {
    expect(myModule.speak()).toBe("hello");
  });
});

为了使其起作用,您需要在SpecRunner.html中包含require.js,可能还需要configure require(与平时一样,例如通过设置baseUrl),如下所示:
SpecRunner.html中:
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Jasmine Spec Runner v2.0.0</title>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
    <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">

    <script src="lib/require.min.js"></script>
    <script> require.config({ baseUrl: "src" }); </script>

    <script src="lib/jasmine-2.0.0/jasmine.js"></script>
    <script src="lib/jasmine-2.0.0/jasmine-html.js"></script>
    <script src="lib/jasmine-2.0.0/boot.js"></script>

    <script src="spec/MySpec.js"></script>

  </head>
  <body>
  </body>
</html>

本例中,AMD模块的实现可能如下所示:

src/myModule.js 文件中:

define([], function () {
  return {
    speak: function () {
      return "hello";
    }
  };
});

这是一个实现完整示例的工作Plunk,请查看working Plunk
祝您愉快!

你的 Plunker 真是太棒了!谢谢。 - JsAndDotNet
1
或者,您也可以在beforeEach()函数中使用done(),这样它将在每个spec运行之前运行。然后,您可以单独运行每个spec,而无需运行加载模块spec。 - Radiance Wei Qi Ong

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接