【问题标题】:Intern Promise Timeouts实习生承诺超时
【发布时间】:2014-04-03 08:50:20
【问题描述】:

我正在与 Intern 一起编写一些功能测试,并遇到了以下文本部分...

"如果在测试超时时间内没有兑现承诺,测试也会失败(默认为 30 秒;设置 this.timeout 以更改值)。"

在……

https://github.com/theintern/intern/wiki/Writing-Tests-with-Intern#asynchronous-testing

如何设置功能测试的承诺超时?我试过直接在 promise 上调用 timeout() ,但这不是一个有效的方法。

我已经设置了各种 WD 超时(页面加载超时、隐式等待等),但我遇到了 promise 超时问题。

【问题讨论】:

    标签: intern


    【解决方案1】:

    通过建议的 API 在我的测试中设置超时不起作用。 它远非理想,但我最终直接修改了 Test.js,并在我想要的超时时间内进行了硬编码。 在查看源代码时,我确实注意到有一条关于超时代码的注释,上面写着类似 // TODO 超时尚未正常工作

    【讨论】:

      【解决方案2】:

      在最新版本上似乎可以正常工作:

      define([
          'intern!object',
          'intern/chai!assert',
          'require',
          'intern/dojo/node!leadfoot/helpers/pollUntil'
      ], function (registerSuite, assert, require, pollUntil) {
          registerSuite(function(){
              return {
                  name: 'index',
      
                  setup: function() {         
                  },
      
                  'Test timeout': function () {           
                      this.timeout = 90000;
                      return this.remote.sleep(45000);
                  }
              }
          });
      });
      

      【讨论】:

        【解决方案3】:

        您还可以将defaultTimeout: 90000 添加到您的配置文件(默认教程代码库中的tests/intern.js)以全局设置超时。这对我很有效。

        【讨论】:

          【解决方案4】:

          通过将超时作为第一个参数传递给this.async 或设置this.timeout(它是属性,而不是方法)来设置测试超时。

          【讨论】:

          • 我试过这个,但它根本不适合我。设置该值似乎完全没有效果。它不是很好,但我最终求助于直接修改 test.js 类并在那里设置超时值。这对我有用,但显然不是最好的方法
          【解决方案5】:

          对于那些在使用 InternJS 4 并利用 async/await 进行功能测试的人来说:timeoutexecuteAsync 对我不起作用,但下面的模式可以。基本上我只是执行了一些逻辑并以比setTimeout 更长的时间间隔使用sleep 方法。请记住,在execute 内部运行的javascript 是块作用域的,因此您需要缓存以后要在window 对象上引用的任何内容。希望这可以节省其他人一些时间和挫败感...

          test(`timer test`, async ({remote}) => {
              await remote.execute(`
                  // run setup logic, keep in mind this is scoped and if you want to reference anything it should be window scoped
                  window.val = "foo";
                  window.setTimeout(function(){
                      window.val = "bar";
                  }, 50);
                  return true;
              `);
              await remote.sleep(51); // remote will chill for 51 ms
              let data = await remote.execute(`
                  // now you can call the reference and the timeout has run
                  return window.val;
              `);
              assert.strictEqual(
                data,
                'bar',
                `remote.sleep(51) should wait until the setTimeout has run, converting window.val from "foo" to "bar"`
              );
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-12-29
            • 2018-09-04
            • 2020-07-19
            • 1970-01-01
            • 2019-07-07
            • 2018-03-18
            • 2015-06-21
            • 2018-04-10
            相关资源
            最近更新 更多