【问题标题】:Jasmine spec runner unit test results flash then disappearJasmine spec runner 单元测试结果闪烁然后消失
【发布时间】:2016-11-19 02:26:42
【问题描述】:

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

单元测试.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 时,它会闪烁两个单元测试的绿色字样(就像通过的测试一样),然后结果就消失了。

它并没有说没有找到单元测试,一旦它消失了。它只有这个:

没有控制台错误。

编辑:让它在重新加载前等待 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 秒然后消失

但答案不是删除承诺,直接导入测试脚本,因为如果测试比几行代码更大,则找不到脚本。

问题在于,当窗口重新加载时,它会使所有内容都消失。但如果没有重新加载,它就找不到大的单元测试。它会找到微小的。

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

【问题讨论】:

    标签: unit-testing jasmine


    【解决方案1】:

    可能我们遇到了一些可以通过异步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>
    

    看起来很有希望:

    【讨论】:

    • 不幸的是,它似乎无法正常工作。我的控制台什么也没说。不是 XHR 获取
    • 您是在本地网络服务器上运行它吗?尝试解决 XHR 请求的问题,因为这是基本需要的功能
    • 我在控制台中没有任何错误,所以它仍然可能是 XHR 请求问题吗?这再次阻止了我,因为我下面的解决方案已停止工作。承诺不起作用似乎确实是一个问题。
    • 似乎现在实际上正在工作,尽管它正在显示其中一个测试文件结果 3 次。很难回答这个问题,因为它只是配置设置的大量试验和错误,我不确定是什么让它起作用
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2021-09-02
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多