Jasmine规范跑步单元测试结果闪现然后消失

时间:2016-11-18 06:27:55

标签: unit-testing jasmine

我有这个文件来运行我的单元测试:

单元tests.html:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <title>Vepo Unit Tests</title>
  <link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
  <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>
</head>
<body>
  <!-- #1. add the system.js and angular libraries -->
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>

  <script src="systemjs.config.js"></script>


  <script>
    // #2. Import the spec files explicitly
    Promise.all([
      System.import('js/app/landing-page/landing-page.component.spec.js'),
      System.import('js/app/pipes/my-uppercase.pipe.spec.js')
    ])

      // #3. wait for all imports to load ...
      //     then re-execute `window.onload` which
      //     triggers the Jasmine test-runner start
      //     or explain what went wrong.
      .then(window.onload)
      .catch(console.error.bind(console));
  </script>

</body>

</html>

当我使用npm start启动服务器时,然后在Web浏览器中浏览到localhost:3000 / unit-tests.html,它会闪烁两个单元测试的绿色字样(如测试通过)然后结果就消失了。

一旦它消失,它并没有说没有发现单元测试。它就是这样:

enter image description here

没有控制台错误。

编辑:让它在重新加载前等待9秒:

  <script>
    Promise.all([
      System.import('js/app/landing-page/landing-page.component.spec'),
      System.import('js/app/pipes/my-uppercase.pipe.spec')
    ]).then(setTimeout(window.onload, 9000)).catch(console.error.bind(console));
  </script>

使测试结果显示9秒然后消失

然而,答案不是删除承诺而只是立即导入测试脚本,因为如果它们是比几行代码更大的测试,则找不到脚本。

问题是当窗口重新加载时,它会使一切消失。但没有重新加载,它没有找到大的单元测试。它找到了很小的。

如何阻止测试结果在重新加载窗口时消失?

2 个答案:

答案 0 :(得分:0)

可能我们有一些竞争条件可以通过异步 boot.js文件加载来解决

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <title>Vepo Unit Tests</title>
  <link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
  <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/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>
  <script src="systemjs.config.js"></script>
</head>
<body>
  <script>
    System.import('node_modules/jasmine-core/lib/jasmine-core/boot.js')
      .then(function() {
        return Promise.all([
          System.import('./spec/first.spec.js'),
          System.import('./spec/second.spec.js')
        ])
      })
      .then(function() {window.onload()})
      .catch(console.error.bind(console));
  </script>

</body>
</html>

看起来很有希望:

enter image description here

答案 1 :(得分:0)

我不知道为什么这样有效......这不是一个真正的答案,它只是一种解决方法。

当我在终端中运行“npm test”时,会打开unit-tests.html并且它们正常运行,结果会保留在屏幕上。

我正在进行最基本的设置,我认为它的工作方式不太基本但可能更好。