Jasmine 2.0 真的不能与 require.js 一起工作吗? [英] Does Jasmine 2.0 really not work with require.js?

查看:21
本文介绍了Jasmine 2.0 真的不能与 require.js 一起工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置我的 SpecRunner.html/.js、RequireConfig.js、我的路径和我的垫片,就像我在 Jasmine + RequireJs 的早期候选版本中一样,但现在我的测试方法显示 Jasmine 未定义.他们最近更改为加载 Jasmine 的不同方法,据我所知,它与 RequireJs 不兼容.

I'm setting up my SpecRunner.html/.js, RequireConfig.js, my paths and my shims just like I have with earlier release candidates of Jasmine + RequireJs, but now my test methods show Jasmine undefined. They've recently changed to a different method of loading Jasmine that I understand is incompatible with RequireJs.

我的理解正确吗?如果是这样,我们还能再次使用 Jasmine + RequireJs 吗?

Is my understanding correct? If so, will we ever be able to use Jasmine + RequireJs again?

推荐答案

新的 boot.js 做了一堆初始化并将其附加到在 require.js 加载 Jasmine 时已经调用的 window.onload().您可以手动调用 window.onload() 来初始化 HTML Reporter 并执行环境.

The new boot.js does a bunch of the initialization and attaches it to window.onload() which has already been called by the time require.js loads Jasmine. You can manually call window.onload() to initialize the HTML Reporter and execute the environment.

SpecRunner.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

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();
    });
  });
});

这篇关于Jasmine 2.0 真的不能与 require.js 一起工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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