等待加载模块以执行它包含的测试 [英] Wait for a module to be loaded to execute the tests it contains

查看:114
本文介绍了等待加载模块以执行它包含的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单元测试包含在使用SystemJS从HTML页面加载的模块中。

My unit tests are contained into a module that loads from an HTML page using SystemJS.

<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>

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

<script>
  System.config({
    defaultJSExtensions: true,
    map: {
      "angular2": 'http://localhost:8000/angular2',
      "rxjs": 'node_modules/rxjs'
    },
    packages: {
      angular2: {
        defaultExtension: 'js',
      }
    }
  });
  System.import('angular2/test/http/backends/xhr_backend_spec')
    .then((src) => {
      src.main();
    });
</script>

我的页面没有显示任何测试,而我的 main 方法包含一个测试套件:

My page doesn't display any tests whereas my main method contains a test suite:

export function main() {
  console.log('in main');
  describe('XHRBackend', () => {
    (...)
  });
}

我放了一些痕迹,我检查了 main 函数和describe函数中定义的回调被调用。但是测试本身并没有在 describe 回调中执行。

I put some traces and I check that the main function and the callback defined in the describe function are called. But the tests themselves aren't executed within the describe callback.

我想我需要等待在运行Jasmine之前要加载的模块,但我不知道如何配置它。

I guess that I need to wait for the module to be loaded before running Jasmine but I don't know how to configure this.

感谢您的帮助!

推荐答案

告诉Jasmine使用 .then(window.onload)运行导入的测试

Tell Jasmine to run the imported tests with .then (window.onload)

<script>
  System.config({
  (...)
  System.import('angular2/test/http/backends/xhr_backend_spec')

      //  wait for all imports to load ...
      //  then re-execute `window.onload` which
      //  triggers the Jasmine test-runner start

     .then(window.onload)
     (...)
</script>

我读过如何在angular.io上运行Jasmine测试

I read how to run Jasmine Tests on angular.io

https://angular.io/docs/ts/latest/testing/first-app-tests.html

这篇关于等待加载模块以执行它包含的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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