Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest test succeed with error printed to console

I'm trying to use jest with an Angular project created by @nrwl/nx. I have followed this article to convert my app from using karma to jest.

The issue I'm getting is that even though my tests are passing, for some reason the following error shows in the console:

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

I have only one test, which is:

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();
  }));
});

For some reason if I take the following line out, the console error does not show:

const fixture = TestBed.createComponent(AppComponent);

I've been looking at the error for hours now, but can't seem to figure out what's causing it. I'm not doing any http request in my test or component, so no idea why it's saying ECONNREFUSED.

Anyone had this error before?

Thanks

like image 305
Kakalokia Avatar asked Oct 16 '22 12:10

Kakalokia


1 Answers

This message is common when you try to make a request to a localhost service which is not available.

Lets consider I am running a script which consumes my node.js service which is being executed under localhost:80. If my node instance is not running I would receive a message like yours.

What I believe is happening is that you have setup the port to check on 80, which is usually more common the default one to be 8080.

like image 193
Cleriston Avatar answered Nov 02 '22 06:11

Cleriston