Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
Jest, mocking return from await, I see results in console, but expect still fails

I have a pretty simple example of a mock working, updating the value correctly, but my test still fails.

fetchStuff.js:

const fetchStuff = async (entity, service) => {
    entity = await service.get();
    console.log('entity is an expected result!', entity); // this consoles out the expected value correctly.
}

fetchStuff.spec.js:

it('should...', () => {
    const expected = 'this will show up in console';
    const service.get = jest.fn().mockImplementation(() => expected);
    let entity;

    fetchStuff(entity, service);

    expect(entity).toEqual(expected) // entity = 'undefined'
});

Every time, I see that console.log in the function is printing out the expected result, but some reason the expect(entity) is always undefined.

I've tried stuff like with flush-promise, I've tried removing the await. I've even seen the done(), technique, where we pass done in to it, ie it('should...', done => {...})

Not sure why I cannot get the correct expected, even tho the console is showing the expected result.

PS. I understand this is not respecting functional paradigm or pure functions. Please ignore that.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are using an asynchronous function, but testing right away, before the promise is resolved. You need an async/await in your test, if you are using asynchronous calls, like your await in the main function.

it('should...', async () => {
    const expected = 'this will show up in console';
    const service.get = jest.fn().mockImplementation(() => expected);
    let entity;

    await fetchStuff(entity, service);

    expect(entity).toEqual(expected) // entity = 'undefined'
});

However, as per the comment on your question, the code may not be returning what you expect, so this change for async/await will not solve the problem on its own.

const fetchStuff = async (entity, service) => {
    entity = await service.get();
    console.log('entity is an expected result!', entity); 
    return entity;
}


it('should...', async () => {
    const expected = 'this will show up in console';
    const FakeService = jest.fn().mockResolvedValue(expected),

    const entity = await FakeService();
    expect(entity).toBe(expected) ;
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!