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

86
Views
Trying to mock a data return in a promise causes jest to timeout

So I have a function which is a promise and returns and array (either empty of full depending how the data comes back) I've mocked the data return how I've got it working previously, I've used the debugger to check the values and it is getting the mocked values, but it is just timing out before it resolves?

  it('works correctly when audience assigned', () => {
    fetchEssentialComplianceData(
      { includeAudienceAssigned: true },
      { id: 'userId' }
    );
    getAssignedLearning.mock.calls[0][1]([], {
      getUserAssignments: {
        results: [
          {
            content: {
              contentType: 'contentType',
              isOfficial: true,
              title: 'content title',
            },
            contentId: 'content id',
            dueDate: 'due date',
          },
        ],
      },
    });
    expect(getAssignedLearning).toHaveBeenCalledTimes(1);
    expect(getAssignedLearning).toHaveBeenCalledWith(
      {
        input: {
          userId: 'userId',
          pageNumber: 0,
          returnCount: 12,
          sort: [
            {
              type: 'dueDate',
              order: 'desc',
            },
            {
              type: 'dateCreated',
              order: 'desc',
            },
          ],
        },
      },
      expect.any(Function)
    );
    //TODO always times out before resolving?
    return expect(
      fetchEssentialComplianceData(
        { includeAudienceAssigned: true },
        { id: 'userId' }
      )
    ).resolves.toBe([]);
  });

I've tried different ways of writing it, making it its own function, tried

I wrote it like this which passed, but was a false pass.

    const result = fetchEssentialComplianceData(
      { includeAudienceAssigned: true },
      { id: 'userId' }
    )
    expect(result).resolves.toBe(['123'])

I also have the same function tested but with no data return (the request is never sent) and this works correctly, I've tried it with different values to make sure and it is right.

it('works correctly when not audience assigned', () => {
    fetchEssentialComplianceData(
      { includeAudienceAssigned: false },
      { id: 'userId' }
    );
    expect(getAssignedLearning).toHaveBeenCalledTimes(0);
    return expect(
      fetchEssentialComplianceData({ includeAudienceAssigned: false })
    ).resolves.toStrictEqual([]);
  });

I'm jsut really stumped now, I've clicked on all the links I could find with google and couldn't find anything that would work

about 4 years ago · Juan Pablo Isaza
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!