【问题标题】:Wait for a module to be loaded to execute the tests it contains等待一个模块被加载以执行它包含的测试
【发布时间】:2016-06-07 02:37:14
【问题描述】:

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

<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 方法包含一个测试套件:

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

我放置了一些跟踪,并检查了 main 函数和 describe 函数中定义的回调是否被调用。但测试本身并未在 describe 回调中执行。

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

感谢您的帮助!

【问题讨论】:

标签: jasmine systemjs


【解决方案1】:

告诉 Jasmine 使用 .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 测试

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

【讨论】:

  • 不客气。感谢蒂埃里的快速反馈。 :-)
猜你喜欢
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 2019-04-25
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多