从业力切换到Jest时单元测试停止工作

时间:2018-11-09 07:09:18

标签: angular karma-jasmine jestjs

我正在做一个离子/角度项目,并尝试从Karma切换到Jest进行测试。 切换之前,以下测试运行良好:

import {TestBed, inject, async} from '@angular/core/testing';
import { TreasureTableService } from './treasure-table.service';
import {PapaParseModule} from 'ngx-papaparse';
import {HttpClientModule} from '@angular/common/http';

describe('TreasureTableService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule, PapaParseModule]
    });
  });

  beforeEach(inject([TreasureTableService], s => {
    treasureTableService = s;
  }));

  it('should get treasure type list', async() => {
    let result: string[] = await treasureTableService.getTreasureTypeList();
    expect(result).toContain("A");
    expect(result.length).toBe(33);
  });
});

切换到Jest之后,出现以下错误:

    console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Error: connect ECONNREFUSED 127.0.0.1:80
  at Object.dispatchError (C:\Users\fhu\WebstormProjects\AcksAssistant\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:65:19)
        at Request.client.on.err (C:\Users\fhu\WebstormProjects\AcksAssistant\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:676:20)
        at emitOne (events.js:121:20)
        at Request.emit (events.js:211:7)
        at Request.onRequestError (C:\Users\fhu\WebstormProjects\AcksAssistant\node_modules\request\request.js:881:8)
        at emitOne (events.js:116:13)
        at ClientRequest.emit (events.js:211:7)
        at Socket.socketErrorListener (_http_client.js:387:9)

有人知道可能是什么问题吗?

编辑: 在我停止使用HttpClientModule并切换到HttpClientTestingModule之后,连接错误消失了,但是现在我的异步调用遇到了问题:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
95 | function createdPatchedSpec(OriginalSpec, registry) {
96 |   function PatchedSpec(attrs) {
>  97 |     OriginalSpec.apply(this, arguments);

0 个答案:

没有答案