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

182
Views
toBeCalled returns false even though the mock function is being called

I'm testing a koa server. The `verifyRequest' is a middleware which calls http request internally, so I want to mock the function and test that the function is called properly.

The first time I run the mock function, toBeCalled returns true as expected. However, after executing mockClear, toBeCalled returns false, even though the mock function is executed (please see the logs below).

Can anyone explain the curious behavior? And please let me know how I can run mock clear correctly.

// server.test.ts

import {verifyRequest} from '../middlewares/verifyRequest';
import {mocked} from 'jest-mock';
import {agent} from './lib/agent';

jest.mock('../middlewares/verifyRequest', () => {
  return {
    verifyRequest: jest.fn().mockImplementation(
      (): Middleware<DefaultState, Context, any> => {
        return async function (_ctx, next) {
          console.log('mocked verifyRequest is called!');
          await next();
        };
      }
    ),
  };
});

afterEach(() => {
  mocked(verifyRequest).mockClear();
});

it('test-one', async () => {
  await agent.get('/'); // verifyRequest will be called as a middleware
  console.log('test-one', mocked(verifyRequest).mock);
  expect(verifyRequest).toBeCalled();
});
it('test-two', async () => {
  await agent.get('/');
  console.log('test-two', mocked(verifyRequest).mock);
  expect(verifyRequest).toBeCalled();
});
// ../middlewares/verifyRequest
import compose from 'koa-compose';
import {Context, DefaultState, Middleware} from 'koa';

export function verifyRequest(): Middleware<DefaultState, Context, any> {
  return compose([
    ...myMiddlwares
  ]);
}

    console.log
      mocked verifyRequest is called!

    console.log
      test-one {
        calls: [ [ [Object] ], [ [Object] ], [ [Object] ], [ [Object] ] ],
        instances: [ undefined, undefined, undefined, undefined ],
        invocationCallOrder: [ 5, 6, 9, 10 ],
        results: [
          { type: 'return', value: [AsyncFunction (anonymous)] },
          { type: 'return', value: [AsyncFunction (anonymous)] },
          { type: 'return', value: [AsyncFunction (anonymous)] },
          { type: 'return', value: [AsyncFunction (anonymous)] }
        ]
      }

    console.log
      mocked verifyRequest is called!

    console.log
      test-two { calls: [], instances: [], invocationCallOrder: [], results: [] }
versions
"jest": "^27.4.7",
"jest-mock": "^27.4.6",
"ts-jest": "^27.1.3",
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!