Promise does not return string instead resolves to an empty string
This is my login.js file where I log in the user and generate a token. I've added comments to make the code more readable.
// get submit button and add event listener to it
const submitbtn = document.getElementById("submit");
console.log(submitbtn.value);
if(submitbtn){
submitbtn.addEventListener('click', loginFunction)
}
//call back function
function loginFunction(e){
e.preventDefault();
// the data to post
const data = {
email: document.getElementById("email").value,
password: document.getElementById("password").value,
};
// post the data to db via fetch
fetch("https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login",{
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
method:"POST",
mode: "cors",
body: JSON.stringify(data)
}).then(function(response){return response.json()})
.then(function(response){
localStorage.setItem('token', response.token);
if (response.Message === "User logged in successfully!"){
// redirect to index page
console.log(response.Message);
document.getElementById("notify").innerHTML =`<div class="isa_success">
<i class="fa fa-check"></i>
${response.Message}
</div>`;
console.log(document.getElementById('notify').innerHTML);
window.location.assign('./index.html')
}
else{
let notify = document.getElementById("notify");
notify.innerHTML =`<div class="isa_info">
<i class="fa fa-info-circle"></i>
${response.Message}
</div>`
}
})
}
So, I wrote a test for this file, called login.test.js. Here's the code
describe('login',() => {
let fetchMock;
let assignMock;
beforeEach(() => {
document.body.innerHTML +=`
<div id="notify"></div>
<form id="login">
<input type="email" id="email" value="test@gmail.com">
<input type="password" id="password" value ="testgmail1234">
<input type="submit" id="submit">
</form>`;
fetchMock = jest.spyOn(global, 'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({Message:"User logged in successfully!"})
}));
assignMock = jest.spyOn(window.location , "assign");
assignMock.mockImplementation(() =>{});
require('../UI/js/login');
});
afterEach(() => {
fetchMock.mockRestore();
assignMock.mockRestore();
jest.resetModules()
});
it('fetch data and change the content of #notify', async () =>{
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
expect(fetchArgs[1]).toEqual({
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
body: JSON.stringify({
email: 'test@gmail.com',
password: "testgmail1234"
})
});
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe(
`<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>`);
expect(assignMock).toHaveBeenCalledTimes(1);
expect(assignMock.mock.calls[0][0]).toBe("./index.html");
});
it('Login with wrong email/password', async () =>{
fetchMock = jest.spyOn(global,'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({message:"User not found!"})
}));
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe( '<div class="isa_info">n ' +
' <i class="fa fa-info-circle"></i>n ' +
'User not found!n' +
'</div>');
});
});
When I run this code, it fails with this error:
Error: expect(received).toBe(expected) // Object.is equality
Expected: "<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>"
Received: "" <Click to see difference>
46 | console.log(Promise.resolve().then());
47 | console.log(Promise.resolve().then());
> 48 | expect(document.getElementById('notify').innerHTML).toBe(
| ^
49 | `<div class="isa_success">
50 | <i class="fa fa-check"></i>
51 | User logged in successfully!
I've been trying to figure out what could be the issue but unsuccessfully. Could anyone help? I'm using jest as my test runner.
javascript unit-testing mocking jestjs
add a comment |
This is my login.js file where I log in the user and generate a token. I've added comments to make the code more readable.
// get submit button and add event listener to it
const submitbtn = document.getElementById("submit");
console.log(submitbtn.value);
if(submitbtn){
submitbtn.addEventListener('click', loginFunction)
}
//call back function
function loginFunction(e){
e.preventDefault();
// the data to post
const data = {
email: document.getElementById("email").value,
password: document.getElementById("password").value,
};
// post the data to db via fetch
fetch("https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login",{
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
method:"POST",
mode: "cors",
body: JSON.stringify(data)
}).then(function(response){return response.json()})
.then(function(response){
localStorage.setItem('token', response.token);
if (response.Message === "User logged in successfully!"){
// redirect to index page
console.log(response.Message);
document.getElementById("notify").innerHTML =`<div class="isa_success">
<i class="fa fa-check"></i>
${response.Message}
</div>`;
console.log(document.getElementById('notify').innerHTML);
window.location.assign('./index.html')
}
else{
let notify = document.getElementById("notify");
notify.innerHTML =`<div class="isa_info">
<i class="fa fa-info-circle"></i>
${response.Message}
</div>`
}
})
}
So, I wrote a test for this file, called login.test.js. Here's the code
describe('login',() => {
let fetchMock;
let assignMock;
beforeEach(() => {
document.body.innerHTML +=`
<div id="notify"></div>
<form id="login">
<input type="email" id="email" value="test@gmail.com">
<input type="password" id="password" value ="testgmail1234">
<input type="submit" id="submit">
</form>`;
fetchMock = jest.spyOn(global, 'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({Message:"User logged in successfully!"})
}));
assignMock = jest.spyOn(window.location , "assign");
assignMock.mockImplementation(() =>{});
require('../UI/js/login');
});
afterEach(() => {
fetchMock.mockRestore();
assignMock.mockRestore();
jest.resetModules()
});
it('fetch data and change the content of #notify', async () =>{
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
expect(fetchArgs[1]).toEqual({
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
body: JSON.stringify({
email: 'test@gmail.com',
password: "testgmail1234"
})
});
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe(
`<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>`);
expect(assignMock).toHaveBeenCalledTimes(1);
expect(assignMock.mock.calls[0][0]).toBe("./index.html");
});
it('Login with wrong email/password', async () =>{
fetchMock = jest.spyOn(global,'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({message:"User not found!"})
}));
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe( '<div class="isa_info">n ' +
' <i class="fa fa-info-circle"></i>n ' +
'User not found!n' +
'</div>');
});
});
When I run this code, it fails with this error:
Error: expect(received).toBe(expected) // Object.is equality
Expected: "<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>"
Received: "" <Click to see difference>
46 | console.log(Promise.resolve().then());
47 | console.log(Promise.resolve().then());
> 48 | expect(document.getElementById('notify').innerHTML).toBe(
| ^
49 | `<div class="isa_success">
50 | <i class="fa fa-check"></i>
51 | User logged in successfully!
I've been trying to figure out what could be the issue but unsuccessfully. Could anyone help? I'm using jest as my test runner.
javascript unit-testing mocking jestjs
1
await Promise.resolve().then();
is pretty much useless. Tryjest.runAllTimers();
instead.
– Felix Kling
Nov 13 '18 at 18:45
replacingawait Promise.resolve().then();
with eitherjest.runAllTimers
orjest.useFakeTimers
fails with the same error
– Dr Hush
Nov 14 '18 at 0:16
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
I'm pretty sure yourexpect
call executes before all thefetch
stuff was executed/resolved. Try addingconsole.log
statements in various places to confirm or exclude that case.
– Felix Kling
Nov 14 '18 at 5:16
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04
add a comment |
This is my login.js file where I log in the user and generate a token. I've added comments to make the code more readable.
// get submit button and add event listener to it
const submitbtn = document.getElementById("submit");
console.log(submitbtn.value);
if(submitbtn){
submitbtn.addEventListener('click', loginFunction)
}
//call back function
function loginFunction(e){
e.preventDefault();
// the data to post
const data = {
email: document.getElementById("email").value,
password: document.getElementById("password").value,
};
// post the data to db via fetch
fetch("https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login",{
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
method:"POST",
mode: "cors",
body: JSON.stringify(data)
}).then(function(response){return response.json()})
.then(function(response){
localStorage.setItem('token', response.token);
if (response.Message === "User logged in successfully!"){
// redirect to index page
console.log(response.Message);
document.getElementById("notify").innerHTML =`<div class="isa_success">
<i class="fa fa-check"></i>
${response.Message}
</div>`;
console.log(document.getElementById('notify').innerHTML);
window.location.assign('./index.html')
}
else{
let notify = document.getElementById("notify");
notify.innerHTML =`<div class="isa_info">
<i class="fa fa-info-circle"></i>
${response.Message}
</div>`
}
})
}
So, I wrote a test for this file, called login.test.js. Here's the code
describe('login',() => {
let fetchMock;
let assignMock;
beforeEach(() => {
document.body.innerHTML +=`
<div id="notify"></div>
<form id="login">
<input type="email" id="email" value="test@gmail.com">
<input type="password" id="password" value ="testgmail1234">
<input type="submit" id="submit">
</form>`;
fetchMock = jest.spyOn(global, 'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({Message:"User logged in successfully!"})
}));
assignMock = jest.spyOn(window.location , "assign");
assignMock.mockImplementation(() =>{});
require('../UI/js/login');
});
afterEach(() => {
fetchMock.mockRestore();
assignMock.mockRestore();
jest.resetModules()
});
it('fetch data and change the content of #notify', async () =>{
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
expect(fetchArgs[1]).toEqual({
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
body: JSON.stringify({
email: 'test@gmail.com',
password: "testgmail1234"
})
});
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe(
`<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>`);
expect(assignMock).toHaveBeenCalledTimes(1);
expect(assignMock.mock.calls[0][0]).toBe("./index.html");
});
it('Login with wrong email/password', async () =>{
fetchMock = jest.spyOn(global,'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({message:"User not found!"})
}));
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe( '<div class="isa_info">n ' +
' <i class="fa fa-info-circle"></i>n ' +
'User not found!n' +
'</div>');
});
});
When I run this code, it fails with this error:
Error: expect(received).toBe(expected) // Object.is equality
Expected: "<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>"
Received: "" <Click to see difference>
46 | console.log(Promise.resolve().then());
47 | console.log(Promise.resolve().then());
> 48 | expect(document.getElementById('notify').innerHTML).toBe(
| ^
49 | `<div class="isa_success">
50 | <i class="fa fa-check"></i>
51 | User logged in successfully!
I've been trying to figure out what could be the issue but unsuccessfully. Could anyone help? I'm using jest as my test runner.
javascript unit-testing mocking jestjs
This is my login.js file where I log in the user and generate a token. I've added comments to make the code more readable.
// get submit button and add event listener to it
const submitbtn = document.getElementById("submit");
console.log(submitbtn.value);
if(submitbtn){
submitbtn.addEventListener('click', loginFunction)
}
//call back function
function loginFunction(e){
e.preventDefault();
// the data to post
const data = {
email: document.getElementById("email").value,
password: document.getElementById("password").value,
};
// post the data to db via fetch
fetch("https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login",{
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
method:"POST",
mode: "cors",
body: JSON.stringify(data)
}).then(function(response){return response.json()})
.then(function(response){
localStorage.setItem('token', response.token);
if (response.Message === "User logged in successfully!"){
// redirect to index page
console.log(response.Message);
document.getElementById("notify").innerHTML =`<div class="isa_success">
<i class="fa fa-check"></i>
${response.Message}
</div>`;
console.log(document.getElementById('notify').innerHTML);
window.location.assign('./index.html')
}
else{
let notify = document.getElementById("notify");
notify.innerHTML =`<div class="isa_info">
<i class="fa fa-info-circle"></i>
${response.Message}
</div>`
}
})
}
So, I wrote a test for this file, called login.test.js. Here's the code
describe('login',() => {
let fetchMock;
let assignMock;
beforeEach(() => {
document.body.innerHTML +=`
<div id="notify"></div>
<form id="login">
<input type="email" id="email" value="test@gmail.com">
<input type="password" id="password" value ="testgmail1234">
<input type="submit" id="submit">
</form>`;
fetchMock = jest.spyOn(global, 'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({Message:"User logged in successfully!"})
}));
assignMock = jest.spyOn(window.location , "assign");
assignMock.mockImplementation(() =>{});
require('../UI/js/login');
});
afterEach(() => {
fetchMock.mockRestore();
assignMock.mockRestore();
jest.resetModules()
});
it('fetch data and change the content of #notify', async () =>{
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
expect(fetchArgs[1]).toEqual({
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
'Access-Control-Allow-Origin':'*',
'Access-Control-Request-Method': '*'
},
body: JSON.stringify({
email: 'test@gmail.com',
password: "testgmail1234"
})
});
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe(
`<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>`);
expect(assignMock).toHaveBeenCalledTimes(1);
expect(assignMock.mock.calls[0][0]).toBe("./index.html");
});
it('Login with wrong email/password', async () =>{
fetchMock = jest.spyOn(global,'fetch');
fetchMock.mockImplementation(() =>Promise.resolve ({
json: () => Promise.resolve({message:"User not found!"})
}));
document.getElementById('submit').click();
expect(fetchMock).toHaveBeenCalledTimes(1);
const fetchArgs = fetchMock.mock.calls[0];
expect(fetchArgs[0]).toBe('https://store-manager-api-app-v2.herokuapp.com/api/v2/auth/login');
await Promise.resolve().then();
expect(document.getElementById('notify').innerHTML).toBe( '<div class="isa_info">n ' +
' <i class="fa fa-info-circle"></i>n ' +
'User not found!n' +
'</div>');
});
});
When I run this code, it fails with this error:
Error: expect(received).toBe(expected) // Object.is equality
Expected: "<div class="isa_success">
<i class="fa fa-check"></i>
User logged in successfully!
</div>"
Received: "" <Click to see difference>
46 | console.log(Promise.resolve().then());
47 | console.log(Promise.resolve().then());
> 48 | expect(document.getElementById('notify').innerHTML).toBe(
| ^
49 | `<div class="isa_success">
50 | <i class="fa fa-check"></i>
51 | User logged in successfully!
I've been trying to figure out what could be the issue but unsuccessfully. Could anyone help? I'm using jest as my test runner.
javascript unit-testing mocking jestjs
javascript unit-testing mocking jestjs
edited Nov 14 '18 at 1:41
Dr Hush
asked Nov 13 '18 at 18:24
Dr HushDr Hush
11
11
1
await Promise.resolve().then();
is pretty much useless. Tryjest.runAllTimers();
instead.
– Felix Kling
Nov 13 '18 at 18:45
replacingawait Promise.resolve().then();
with eitherjest.runAllTimers
orjest.useFakeTimers
fails with the same error
– Dr Hush
Nov 14 '18 at 0:16
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
I'm pretty sure yourexpect
call executes before all thefetch
stuff was executed/resolved. Try addingconsole.log
statements in various places to confirm or exclude that case.
– Felix Kling
Nov 14 '18 at 5:16
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04
add a comment |
1
await Promise.resolve().then();
is pretty much useless. Tryjest.runAllTimers();
instead.
– Felix Kling
Nov 13 '18 at 18:45
replacingawait Promise.resolve().then();
with eitherjest.runAllTimers
orjest.useFakeTimers
fails with the same error
– Dr Hush
Nov 14 '18 at 0:16
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
I'm pretty sure yourexpect
call executes before all thefetch
stuff was executed/resolved. Try addingconsole.log
statements in various places to confirm or exclude that case.
– Felix Kling
Nov 14 '18 at 5:16
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04
1
1
await Promise.resolve().then();
is pretty much useless. Try jest.runAllTimers();
instead.– Felix Kling
Nov 13 '18 at 18:45
await Promise.resolve().then();
is pretty much useless. Try jest.runAllTimers();
instead.– Felix Kling
Nov 13 '18 at 18:45
replacing
await Promise.resolve().then();
with either jest.runAllTimers
or jest.useFakeTimers
fails with the same error– Dr Hush
Nov 14 '18 at 0:16
replacing
await Promise.resolve().then();
with either jest.runAllTimers
or jest.useFakeTimers
fails with the same error– Dr Hush
Nov 14 '18 at 0:16
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
I'm pretty sure your
expect
call executes before all the fetch
stuff was executed/resolved. Try adding console.log
statements in various places to confirm or exclude that case.– Felix Kling
Nov 14 '18 at 5:16
I'm pretty sure your
expect
call executes before all the fetch
stuff was executed/resolved. Try adding console.log
statements in various places to confirm or exclude that case.– Felix Kling
Nov 14 '18 at 5:16
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04
add a comment |
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
});
}
});
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%2f53287324%2fpromise-does-not-return-string-instead-resolves-to-an-empty-string%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
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%2f53287324%2fpromise-does-not-return-string-instead-resolves-to-an-empty-string%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
await Promise.resolve().then();
is pretty much useless. Tryjest.runAllTimers();
instead.– Felix Kling
Nov 13 '18 at 18:45
replacing
await Promise.resolve().then();
with eitherjest.runAllTimers
orjest.useFakeTimers
fails with the same error– Dr Hush
Nov 14 '18 at 0:16
@FelixKling, I cannot figure out where the issue is, coz I see my code is working alright and the test seems okay
– Dr Hush
Nov 14 '18 at 4:59
I'm pretty sure your
expect
call executes before all thefetch
stuff was executed/resolved. Try addingconsole.log
statements in various places to confirm or exclude that case.– Felix Kling
Nov 14 '18 at 5:16
The promise actually resolves to undefined, not to an empty string. Forgive me, I'm not even sure whether I'm doing the right thing
– Dr Hush
Nov 27 '18 at 19:04