Mocha timing out when test in Promise callback fails












0















If I have the following module:



module.exports = kontinue => {
Promise.resolve({error:null})
.then(o => {
console.log('promise resolved');
// say something goes wrong here
if(true)
kontinue({error:'promise resolved but something else went wrong'});
else kontinue(o);
})
.catch(error => {
console.log('caught error');
kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
});
};



And the following test:



const assert = require('assert').strict;
const target = require('./the_above_code.js');

it('should not timeout', (done) => {
target((sut) => {
console.log('continuation called');
assert.ok(false); // the test for sut.error === what I expected was failing
done();
});
});



It outputs:



promise resolved
continuation called
caught error

...

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.



I realise this is because the .catch() is returning a new promise which is not resolving, but that's not what I really want during testing.



How do I test the object a promise resolves to, fail the test if necessary, have Mocha report that failure?



Perhaps there is somewhere else other than in the continuation (which never returns in the code that uses this module) that I can put the tests?



I'm sure monads can reduce the amount of boilerplate code here, but using them surely would violate Kernighan's maxim.










share|improve this question

























  • What if you type return on Promise.resolve()?

    – squeekyDave
    Nov 14 '18 at 13:12











  • @squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

    – Nicholas Shanks
    Nov 14 '18 at 13:34











  • I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

    – squeekyDave
    Nov 14 '18 at 13:39













  • @squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

    – Nicholas Shanks
    Nov 14 '18 at 14:45
















0















If I have the following module:



module.exports = kontinue => {
Promise.resolve({error:null})
.then(o => {
console.log('promise resolved');
// say something goes wrong here
if(true)
kontinue({error:'promise resolved but something else went wrong'});
else kontinue(o);
})
.catch(error => {
console.log('caught error');
kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
});
};



And the following test:



const assert = require('assert').strict;
const target = require('./the_above_code.js');

it('should not timeout', (done) => {
target((sut) => {
console.log('continuation called');
assert.ok(false); // the test for sut.error === what I expected was failing
done();
});
});



It outputs:



promise resolved
continuation called
caught error

...

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.



I realise this is because the .catch() is returning a new promise which is not resolving, but that's not what I really want during testing.



How do I test the object a promise resolves to, fail the test if necessary, have Mocha report that failure?



Perhaps there is somewhere else other than in the continuation (which never returns in the code that uses this module) that I can put the tests?



I'm sure monads can reduce the amount of boilerplate code here, but using them surely would violate Kernighan's maxim.










share|improve this question

























  • What if you type return on Promise.resolve()?

    – squeekyDave
    Nov 14 '18 at 13:12











  • @squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

    – Nicholas Shanks
    Nov 14 '18 at 13:34











  • I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

    – squeekyDave
    Nov 14 '18 at 13:39













  • @squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

    – Nicholas Shanks
    Nov 14 '18 at 14:45














0












0








0








If I have the following module:



module.exports = kontinue => {
Promise.resolve({error:null})
.then(o => {
console.log('promise resolved');
// say something goes wrong here
if(true)
kontinue({error:'promise resolved but something else went wrong'});
else kontinue(o);
})
.catch(error => {
console.log('caught error');
kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
});
};



And the following test:



const assert = require('assert').strict;
const target = require('./the_above_code.js');

it('should not timeout', (done) => {
target((sut) => {
console.log('continuation called');
assert.ok(false); // the test for sut.error === what I expected was failing
done();
});
});



It outputs:



promise resolved
continuation called
caught error

...

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.



I realise this is because the .catch() is returning a new promise which is not resolving, but that's not what I really want during testing.



How do I test the object a promise resolves to, fail the test if necessary, have Mocha report that failure?



Perhaps there is somewhere else other than in the continuation (which never returns in the code that uses this module) that I can put the tests?



I'm sure monads can reduce the amount of boilerplate code here, but using them surely would violate Kernighan's maxim.










share|improve this question
















If I have the following module:



module.exports = kontinue => {
Promise.resolve({error:null})
.then(o => {
console.log('promise resolved');
// say something goes wrong here
if(true)
kontinue({error:'promise resolved but something else went wrong'});
else kontinue(o);
})
.catch(error => {
console.log('caught error');
kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
});
};



And the following test:



const assert = require('assert').strict;
const target = require('./the_above_code.js');

it('should not timeout', (done) => {
target((sut) => {
console.log('continuation called');
assert.ok(false); // the test for sut.error === what I expected was failing
done();
});
});



It outputs:



promise resolved
continuation called
caught error

...

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.



I realise this is because the .catch() is returning a new promise which is not resolving, but that's not what I really want during testing.



How do I test the object a promise resolves to, fail the test if necessary, have Mocha report that failure?



Perhaps there is somewhere else other than in the continuation (which never returns in the code that uses this module) that I can put the tests?



I'm sure monads can reduce the amount of boilerplate code here, but using them surely would violate Kernighan's maxim.







mocha es6-promise






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 13:30







Nicholas Shanks

















asked Nov 14 '18 at 13:06









Nicholas ShanksNicholas Shanks

6,85414167




6,85414167













  • What if you type return on Promise.resolve()?

    – squeekyDave
    Nov 14 '18 at 13:12











  • @squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

    – Nicholas Shanks
    Nov 14 '18 at 13:34











  • I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

    – squeekyDave
    Nov 14 '18 at 13:39













  • @squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

    – Nicholas Shanks
    Nov 14 '18 at 14:45



















  • What if you type return on Promise.resolve()?

    – squeekyDave
    Nov 14 '18 at 13:12











  • @squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

    – Nicholas Shanks
    Nov 14 '18 at 13:34











  • I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

    – squeekyDave
    Nov 14 '18 at 13:39













  • @squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

    – Nicholas Shanks
    Nov 14 '18 at 14:45

















What if you type return on Promise.resolve()?

– squeekyDave
Nov 14 '18 at 13:12





What if you type return on Promise.resolve()?

– squeekyDave
Nov 14 '18 at 13:12













@squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

– Nicholas Shanks
Nov 14 '18 at 13:34





@squeekyDave I removed the braces so that the arrow function returned the result of Promise.resolve().then().catch() and it still timed out. I also tried removing the done parameter from the callback supplied to it() but that meant assert.ok(false) no longer caused a test failure.

– Nicholas Shanks
Nov 14 '18 at 13:34













I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

– squeekyDave
Nov 14 '18 at 13:39







I don't know how useful this will be but I faced a similar issue not long ago and the solution of my problem was to use a beforeEach hook where I typed this.timeout=10000. It was something like beforeEach(function(){ this.timeout=1000; // then i loaded my variable that returned a prmise }) And after that I was able to test. Hope this helps.

– squeekyDave
Nov 14 '18 at 13:39















@squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

– Nicholas Shanks
Nov 14 '18 at 14:45





@squeekyDave thanks, but my problem is not that the operation takes longer than the time-out duration (which increasing the timeout would solve), but rather that the test assertion throws an exception within the .then() and I don't know where else I can put it.

– Nicholas Shanks
Nov 14 '18 at 14:45












0






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',
autoActivateHeartbeat: false,
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%2f53300946%2fmocha-timing-out-when-test-in-promise-callback-fails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300946%2fmocha-timing-out-when-test-in-promise-callback-fails%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