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

293
Views
Spy returns false, but I can see it has been called

I have a function:

export class Processor {
  public static async process(info) {
    return Promise.resolve('item processed');
  }
}
import { Processor } from "./processor";

export const execute = (event, context) => {
  const records = event.Records;

  records.map(async (record) => {
    return await processRecord(record.body.info);
  });

  async function processRecord(info) {
    try {
      console.log('before process');
      await Processor.process(info);
      console.log('after process');
    } catch (e) {
      throw new Error('Processing error');
    }
  }
};

Then I have a test:

import { execute } from "./execute";
import { Processor } from "./processor";
jest.mock("./Processor");
const mockProcessor = jest.mocked(Processor, false);

describe("Execute", () => {
  it("should process info", () => {
    const processSpy = jest.spyOn(mockProcessor, "process").mockResolvedValueOnce("test-return");
    const result = await execute({Records: [{body: {info: "test-info"}}]}, {});
    expect(processSpy).toHaveBeenCalled();
  
  });
});

The test fails on the process spy, although 'before process' and 'after process' logs are displayed. Where did I make a mistake ?

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

0

You don't need to use jest.mock('./Processor'), use jest.spyOn(Processor, 'process') is enough.

import { execute } from './execute';
import { Processor } from './processor';

describe('Execute', () => {
  it('should process info', async () => {
    const processSpy = jest.spyOn(Processor, 'process').mockResolvedValueOnce('test-return');
    await execute({ Records: [{ body: { info: 'test-info' } }] }, {});
    expect(processSpy).toHaveBeenCalled();
  });
});
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!