【问题标题】:Jest async test times out if "expect" called within async function. Works sometimes. "Async callback was not invoked within timeout specified"如果在异步函数中调用“期望”,则 Jest 异步测试超时。有时工作。 “在指定的超时时间内未调用异步回调”
【发布时间】:2017-07-10 14:08:15
【问题描述】:

我正在使用 Jest 和 JS,并尝试围绕 X-ray JS 库(一个网络抓取工具包)编写测试。以下是测试。这是使用 Jest 18.x 和截至 2017 年 2 月 20 日最新的 X 射线 js。

const htmlResponse = require('../__mocks__/html_response'); // just contains {listingsPage: '<html>....</html>';}

describe('scraper', () => {
    it("should get David Nichols' latest study from valid HTML", (done) => {
        var listingsHtml = htmlResponse.listingsPage;
        const Xray = require('x-ray');
        const x = Xray();
        expect(x).not.toEqual(null);
        var parseHtml = x('#Repo tbody tr', { link: 'td:nth-child(1) a@href' })
        parseHtml(listingsHtml, (err, result) => {
            console.log(Object.keys(result));
            expect(result.link).toEqual('le test'); // commenting this out causes test to pass.
            done();
        });
});

如果我在done() 上方的回调中删除expect().toEqual 调用,则测试运行:

 PASS  src/__tests__/scraper-test.js

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.315s, estimated 6s
Ran all test suites related to changed files.

按原样使用该行,它会超时。 result 是一个简单的对象 {link: 'string'} 测试没有进行网络调用。我尝试将超时值更新为 30 秒,但没有成功。

 FAIL  src/__tests__/scraper-test.js (5.787s)

  ● scraper › should get David Nichols' latest study from valid HTML

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

      at Timeout.callback [as _onTimeout] (node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/browser/Window.js:480:19)
      at ontimeout (timers.js:365:14)
      at tryOnTimeout (timers.js:237:5)
      at Timer.listOnTimeout (timers.js:207:5)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        6.641s
Ran all test suites related to changed files.

【问题讨论】:

    标签: javascript jasmine jestjs jasmine2.0 x-ray


    【解决方案1】:

    问题是您无法预测需要多长时间才能获得结果。您可以做的是创建在回调中解析的 Promise 并从您的测试中返回此 Promise

        const htmlResponse = require('../__mocks__/html_response'); // just contains {listingsPage: '<html>....</html>';}
    describe('scraper', () = > {
        it("should get David Nichols' latest study from valid HTML", (done) = > {
          const Xray = require('x-ray');
          const x = Xray();
          expect(x)
            .not.toEqual(null);
          var parseHtml = x('#Repo tbody tr', {
            link: 'td:nth-child(1) a@href'
          })
          return  new Promise((resolve) = > {
            var listingsHtml = htmlResponse.listingsPage;
            parseHtml(listingsHtml, (err, result) = > {
              resolve(result);
            });
          })
          .then((result = > {
            expect(result.link)
              .toEqual('le test'); // commenting this out causes test to pass.
          }))
        });
    

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2014-05-01
      • 2019-11-04
      • 1970-01-01
      相关资源
      最近更新 更多