Javascript Jest异步测试在“如果”时超时;期望;在异步函数中调用。有时工作&引用;在指定的超时时间内未调用异步回调;

Javascript Jest异步测试在“如果”时超时;期望;在异步函数中调用。有时工作&引用;在指定的超时时间内未调用异步回调;,javascript,jasmine,jestjs,jasmine2.0,x-ray,Javascript,Jasmine,Jestjs,Jasmine2.0,X Ray,我将Jest与JS结合使用,并试图围绕X-ray JS库(一个web抓取工具包)编写一个测试。以下是测试。这是使用Jest 18.x和截至2017年2月20日的最新x射线js const htmlResponse = require('../__mocks__/html_response'); // just contains {listingsPage: '<html>....</html>';} describe('scraper', () => {

我将Jest与JS结合使用,并试图围绕X-ray JS库(一个web抓取工具包)编写一个测试。以下是测试。这是使用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();
        });
});
如果这条线保持不变,它就会超时<代码>结果是一个简单的对象{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.

问题是你无法预测得到结果需要多长时间。您可以做的是创建在回调中解析的承诺,并从测试中返回该承诺

    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.
      }))
    });
const htmlResponse=require('../\uuu mocks\uuu/html\u response');//仅包含{listingsPage:'..';}
描述('scraper',()=>{
它(“应该从有效的HTML获取David Nichols的最新研究”,(完成)=>{
常数x射线=需要('x射线');
常数x=x射线();
期望值(x)
.not.toEqual(空);
var parseHtml=x(“#Repo tbody tr”{
链接:'td:n个孩子(1)a@href'
})
返回新承诺((解决)=>{
var listingsHtml=htmlResponse.listingsPage;
parseHtml(listingsHtml,(错误,结果)=>{
决心(结果);
});
})
。然后((结果=>{
expect(result.link)
.toEqual('le test');//注释掉此项会导致测试通过。
}))
});
    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.
      }))
    });