Promise chains aren't being executed all the way through
I'm trying to do an integration test on a function that manages users. The calls it makes are asynchronous but Jest seems to be cutting out after I add the test user and check the details.
it("runs lifecycle", async done => {
// expect.assertions(1);
function check(a, b, c) {
return new Promise((resolve, reject) => {
console.log(a, b, c);
if (a != b) reject(c);
console.error(a, b, c);
resolve();
});
}
await new Promise((resolve, reject) => {
user.add(testUser).then(() => {
user.get(testUser.id).then(out => {
check(out, testUser, "adding").then(() => {
user.update(testUser1).then(() => {
user
.get(testUser1.id)
.then(out => check(out, testUser1, "update"))
.then(() => {
user.delete(testUser.id).then(() => {
user
.get(testUser1.id)
.then(out => check(out, null, "delete"))
.then(() => {
resolve();
done();
});
});
});
});
});
});
});
}).then(() => {
expect(true).toBeTruthy;
done();
});
done();
testUser
and testUser1
are objects with some of their properties changed. I have tested this outside of Jest and the code runs fine.
console.log user.js:68
Successfully created new user: test
console.log user.test.js:26
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
console.error user.test.js:28
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
FAIL ./user.test.js (7.269s)
User APIs
✕ runs lifecycle (2779ms)
● User APIs › runs lifecycle
Error
adding
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.916s
Ran all test suites.
console.log user.js:114
Successfully deleted test
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
node.js jestjs
|
show 2 more comments
I'm trying to do an integration test on a function that manages users. The calls it makes are asynchronous but Jest seems to be cutting out after I add the test user and check the details.
it("runs lifecycle", async done => {
// expect.assertions(1);
function check(a, b, c) {
return new Promise((resolve, reject) => {
console.log(a, b, c);
if (a != b) reject(c);
console.error(a, b, c);
resolve();
});
}
await new Promise((resolve, reject) => {
user.add(testUser).then(() => {
user.get(testUser.id).then(out => {
check(out, testUser, "adding").then(() => {
user.update(testUser1).then(() => {
user
.get(testUser1.id)
.then(out => check(out, testUser1, "update"))
.then(() => {
user.delete(testUser.id).then(() => {
user
.get(testUser1.id)
.then(out => check(out, null, "delete"))
.then(() => {
resolve();
done();
});
});
});
});
});
});
});
}).then(() => {
expect(true).toBeTruthy;
done();
});
done();
testUser
and testUser1
are objects with some of their properties changed. I have tested this outside of Jest and the code runs fine.
console.log user.js:68
Successfully created new user: test
console.log user.test.js:26
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
console.error user.test.js:28
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
FAIL ./user.test.js (7.269s)
User APIs
✕ runs lifecycle (2779ms)
● User APIs › runs lifecycle
Error
adding
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.916s
Ran all test suites.
console.log user.js:114
Successfully deleted test
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
node.js jestjs
1
Avoid the.then
-as-callback anti-pattern - return thePromise
so the next.then
can handle it instead, for much flatter code. (or, for this much nesting, considerawait
instead)
– CertainPerformance
Nov 28 '18 at 5:11
First off, dofn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a.then()
so that it is inserted into the promise chain.fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.
– jfriend00
Nov 28 '18 at 5:24
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48
|
show 2 more comments
I'm trying to do an integration test on a function that manages users. The calls it makes are asynchronous but Jest seems to be cutting out after I add the test user and check the details.
it("runs lifecycle", async done => {
// expect.assertions(1);
function check(a, b, c) {
return new Promise((resolve, reject) => {
console.log(a, b, c);
if (a != b) reject(c);
console.error(a, b, c);
resolve();
});
}
await new Promise((resolve, reject) => {
user.add(testUser).then(() => {
user.get(testUser.id).then(out => {
check(out, testUser, "adding").then(() => {
user.update(testUser1).then(() => {
user
.get(testUser1.id)
.then(out => check(out, testUser1, "update"))
.then(() => {
user.delete(testUser.id).then(() => {
user
.get(testUser1.id)
.then(out => check(out, null, "delete"))
.then(() => {
resolve();
done();
});
});
});
});
});
});
});
}).then(() => {
expect(true).toBeTruthy;
done();
});
done();
testUser
and testUser1
are objects with some of their properties changed. I have tested this outside of Jest and the code runs fine.
console.log user.js:68
Successfully created new user: test
console.log user.test.js:26
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
console.error user.test.js:28
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
FAIL ./user.test.js (7.269s)
User APIs
✕ runs lifecycle (2779ms)
● User APIs › runs lifecycle
Error
adding
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.916s
Ran all test suites.
console.log user.js:114
Successfully deleted test
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
node.js jestjs
I'm trying to do an integration test on a function that manages users. The calls it makes are asynchronous but Jest seems to be cutting out after I add the test user and check the details.
it("runs lifecycle", async done => {
// expect.assertions(1);
function check(a, b, c) {
return new Promise((resolve, reject) => {
console.log(a, b, c);
if (a != b) reject(c);
console.error(a, b, c);
resolve();
});
}
await new Promise((resolve, reject) => {
user.add(testUser).then(() => {
user.get(testUser.id).then(out => {
check(out, testUser, "adding").then(() => {
user.update(testUser1).then(() => {
user
.get(testUser1.id)
.then(out => check(out, testUser1, "update"))
.then(() => {
user.delete(testUser.id).then(() => {
user
.get(testUser1.id)
.then(out => check(out, null, "delete"))
.then(() => {
resolve();
done();
});
});
});
});
});
});
});
}).then(() => {
expect(true).toBeTruthy;
done();
});
done();
testUser
and testUser1
are objects with some of their properties changed. I have tested this outside of Jest and the code runs fine.
console.log user.js:68
Successfully created new user: test
console.log user.test.js:26
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
console.error user.test.js:28
{ id: 'test',
email: 'you@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo@me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
FAIL ./user.test.js (7.269s)
User APIs
✕ runs lifecycle (2779ms)
● User APIs › runs lifecycle
Error
adding
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.916s
Ran all test suites.
console.log user.js:114
Successfully deleted test
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
node.js jestjs
node.js jestjs
edited Nov 28 '18 at 13:04
skyboyer
4,07311230
4,07311230
asked Nov 28 '18 at 5:07
Wilhelm ErasmusWilhelm Erasmus
1139
1139
1
Avoid the.then
-as-callback anti-pattern - return thePromise
so the next.then
can handle it instead, for much flatter code. (or, for this much nesting, considerawait
instead)
– CertainPerformance
Nov 28 '18 at 5:11
First off, dofn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a.then()
so that it is inserted into the promise chain.fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.
– jfriend00
Nov 28 '18 at 5:24
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48
|
show 2 more comments
1
Avoid the.then
-as-callback anti-pattern - return thePromise
so the next.then
can handle it instead, for much flatter code. (or, for this much nesting, considerawait
instead)
– CertainPerformance
Nov 28 '18 at 5:11
First off, dofn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a.then()
so that it is inserted into the promise chain.fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.
– jfriend00
Nov 28 '18 at 5:24
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48
1
1
Avoid the
.then
-as-callback anti-pattern - return the Promise
so the next .then
can handle it instead, for much flatter code. (or, for this much nesting, consider await
instead)– CertainPerformance
Nov 28 '18 at 5:11
Avoid the
.then
-as-callback anti-pattern - return the Promise
so the next .then
can handle it instead, for much flatter code. (or, for this much nesting, consider await
instead)– CertainPerformance
Nov 28 '18 at 5:11
First off, do
fn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a .then()
so that it is inserted into the promise chain. fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.– jfriend00
Nov 28 '18 at 5:24
First off, do
fn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a .then()
so that it is inserted into the promise chain. fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.– jfriend00
Nov 28 '18 at 5:24
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48
|
show 2 more comments
1 Answer
1
active
oldest
votes
I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512507%2fpromise-chains-arent-being-executed-all-the-way-through%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);
add a comment |
I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);
add a comment |
I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);
I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);
answered Nov 29 '18 at 10:32
Wilhelm ErasmusWilhelm Erasmus
1139
1139
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512507%2fpromise-chains-arent-being-executed-all-the-way-through%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Avoid the
.then
-as-callback anti-pattern - return thePromise
so the next.then
can handle it instead, for much flatter code. (or, for this much nesting, considerawait
instead)– CertainPerformance
Nov 28 '18 at 5:11
First off, do
fn().then().then().then()
instead of deep nesting. Second, make sure you return any promise from with a.then()
so that it is inserted into the promise chain.fn().then(val => { return someOtherfn()}).then(...).then(...)
. If you don't return it, then it's just an independent promise chain that has no connection at all to the parent except when it started and it's completion is not part of the parent chain.– jfriend00
Nov 28 '18 at 5:24
@jfriend00 it seems to stop executing right after the first invocation of check() the update() has a promise implemented. I've tried both nested and unnested promises but neither work.
– Wilhelm Erasmus
Nov 28 '18 at 5:33
What does "stop executing" mean?
– jfriend00
Nov 28 '18 at 5:38
@jfriend00 testing halts and any errors after this point don't get accounted for.
– Wilhelm Erasmus
Nov 28 '18 at 5:48