Unit test stopped working when switching from Karma to Jest











up vote
0
down vote

favorite












I am working on an Ionic/Angular project and tried switching from Karma to Jest for my testing.
The following test was running fine before the switch:



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


After I switched to Jest I got the following error:



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


Anyone got an idea what the problem could be?



EDIT:
After I stopped using the HttpClientModule and switched to the HttpClientTestingModule the connection Error went away but now I have problem with the async call:



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









share|improve this question
























  • For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
    – Florian
    Nov 9 at 7:13










  • How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
    – Ludevik
    Nov 9 at 7:37










  • Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
    – Florian
    Nov 9 at 12:33










  • Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
    – Ludevik
    Nov 9 at 12:58






  • 1




    Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
    – Ludevik
    Nov 9 at 14:27















up vote
0
down vote

favorite












I am working on an Ionic/Angular project and tried switching from Karma to Jest for my testing.
The following test was running fine before the switch:



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


After I switched to Jest I got the following error:



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


Anyone got an idea what the problem could be?



EDIT:
After I stopped using the HttpClientModule and switched to the HttpClientTestingModule the connection Error went away but now I have problem with the async call:



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









share|improve this question
























  • For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
    – Florian
    Nov 9 at 7:13










  • How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
    – Ludevik
    Nov 9 at 7:37










  • Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
    – Florian
    Nov 9 at 12:33










  • Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
    – Ludevik
    Nov 9 at 12:58






  • 1




    Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
    – Ludevik
    Nov 9 at 14:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working on an Ionic/Angular project and tried switching from Karma to Jest for my testing.
The following test was running fine before the switch:



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


After I switched to Jest I got the following error:



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


Anyone got an idea what the problem could be?



EDIT:
After I stopped using the HttpClientModule and switched to the HttpClientTestingModule the connection Error went away but now I have problem with the async call:



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









share|improve this question















I am working on an Ionic/Angular project and tried switching from Karma to Jest for my testing.
The following test was running fine before the switch:



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


After I switched to Jest I got the following error:



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


Anyone got an idea what the problem could be?



EDIT:
After I stopped using the HttpClientModule and switched to the HttpClientTestingModule the connection Error went away but now I have problem with the async call:



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






angular karma-jasmine jestjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:25









Jonathan

5,07722153




5,07722153










asked Nov 9 at 7:09









Florian

134




134












  • For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
    – Florian
    Nov 9 at 7:13










  • How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
    – Ludevik
    Nov 9 at 7:37










  • Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
    – Florian
    Nov 9 at 12:33










  • Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
    – Ludevik
    Nov 9 at 12:58






  • 1




    Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
    – Ludevik
    Nov 9 at 14:27


















  • For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
    – Florian
    Nov 9 at 7:13










  • How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
    – Ludevik
    Nov 9 at 7:37










  • Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
    – Florian
    Nov 9 at 12:33










  • Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
    – Ludevik
    Nov 9 at 12:58






  • 1




    Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
    – Ludevik
    Nov 9 at 14:27
















For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
– Florian
Nov 9 at 7:13




For the note: There are Jest test that are running just fine so I think its not just a configuration problem.
– Florian
Nov 9 at 7:13












How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
– Ludevik
Nov 9 at 7:37




How could it work with karma? It seems that your test is doing http request to localhost. Did your localhost run when you ran your karma test?
– Ludevik
Nov 9 at 7:37












Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
– Florian
Nov 9 at 12:33




Yes, Karma did open a browser window. I thought that one of big advantages with Jest is that they can do it without an open browser by probably doing it in a headless chrome in the background.
– Florian
Nov 9 at 12:33












Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
– Ludevik
Nov 9 at 12:58




Actually jest doesn't run in any browser, it currently runs only in jsdom. My question was whether your dev server with your app was running when you ran your karma tests or not.
– Ludevik
Nov 9 at 12:58




1




1




Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
– Ludevik
Nov 9 at 14:27




Hm, strange. Could you inspect how your karma test managed to correctly call the http request and trace it down? Nevertheless, your test should not do any xhr requests to any server at all. You should use HttpClientTestingModule and mock all request in your tests.
– Ludevik
Nov 9 at 14:27

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221240%2funit-test-stopped-working-when-switching-from-karma-to-jest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221240%2funit-test-stopped-working-when-switching-from-karma-to-jest%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python