Jest 测试成功,错误打印到控制台

Kak*_*kia 5 jestjs angular nrwl

我正在尝试使用jest@nrwl/nx. 我已经按照这篇文章将我的应用程序从使用转换karmajest.

我遇到的问题是,即使我的测试通过了,由于某种原因,控制台中会显示以下错误:

console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Error: connect ECONNREFUSED 127.0.0.1:80
    at Object.dispatchError (\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:65:19)
    at Request.client.on.err (\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:676:20)
    at Request.emit (events.js:187:15)
    at Request.onRequestError (\node_modules\request\request.js:881:8)
    at ClientRequest.emit (events.js:182:13)
    at Socket.socketErrorListener (_http_client.js:391:9)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19) undefined
Run Code Online (Sandbox Code Playgroud)

我只有一项测试,即:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { RouterTestingModule } from '@angular/router/testing';

import { CommonUtilsModule } from '@lib/common-utils';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [RouterTestingModule, CommonUtilsModule],
      declarations: [AppComponent]
    }).compileComponents();
  }));
  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
});
Run Code Online (Sandbox Code Playgroud)

出于某种原因,如果我取出以下行,控制台错误不会显示:

const fixture = TestBed.createComponent(AppComponent);
Run Code Online (Sandbox Code Playgroud)

我已经看了这个错误好几个小时了,但似乎无法弄清楚是什么导致了它。我没有在我的测试或组件中做任何 http 请求,所以不知道为什么它说ECONNREFUSED.

以前有人遇到过这个错误吗?

谢谢

Cle*_*ton 3

当您尝试向不可用的本地主机服务发出请求时,此消息很常见。

假设我正在运行一个脚本,该脚本使用在 localhost:80 下执行的 node.js 服务。如果我的节点实例未运行,我会收到类似您的消息。

我相信正在发生的事情是您已设置端口来检查 80,这通常更常见,默认端口是 8080。