Jest unit test fails?

Hi friends in my test I mocked the express res properly but res.status assertion fails, I can’t figure out a clear explanation.
do you have any suggestions, thanks for the help :grinning:
Note : the if branch in the getAllusers is not a reason I already commented it, still the same result. :+1:
the file being tested:

const bcrypt = require('bcryptjs')
const { User } = require('../models')
const catchAsync = require('../utils/catchAsync')
const AppError = require('../utils/appError')

exports.getAllUsers = catchAsync(async (req, res, next) => {
  const { start = 0, limit = 7 } = req.query
  const usersCount = await User.count({})
  res.set('Access-Control-Expose-Headers', 'X-Total-Count')
  res.set('X-Total-Count', `${usersCount}`)
  const users = await User.findAll({
    attributes: ['id', 'username', 'email', 'role', 'avatar'],
    limit: limit * 1,
    offset: start * 1
  })
  if (!users.length) {
    return next(new AppError('no data found', 204))
  }
  res.status(200).json({
    status: 'success',
    data: users
  })
})

the test file:

const userController = require('./userController')
const AppError = require('../utils/appError')
const { User } = require('../models')

describe('UserController', () => {
  afterEach(() => {
    jest.clearAllMocks()
    // Reset to original implementation before each test
  })
  test('Expect to respond with 200 code and users data', async () => {
    const users = [{ username: 'han' }, { username: 'med' }]
    User.findAll = jest
      .fn()
      .mockImplementation(() => Promise.resolve([...users]))
    User.count = jest.fn().mockImplementation(() => Promise.resolve(2))
    const req = { query: {} }
    const res = {
      status: jest.fn(() => res),
      json: jest.fn(() => res),
      set: jest.fn()
    }
    const next = jest.fn()
    await userController.getAllUsers(req, res, next)
    expect(res.set).toHaveBeenCalledTimes(2)
    expect(res.set.mock.calls[0][0]).toBe('Access-Control-Expose-Headers')
    expect(res.set.mock.calls[0][1]).toBe('X-Total-Count')
    expect(res.set.mock.calls[1][0]).toBe('X-Total-Count')
    expect(res.set.mock.calls[1][1]).toBe('2')
    expect(res.status).toHaveBeenCalledTimes(1)
    expect(res.status).toHaveBeenCalledWith(200)
    expect(res.json).toHaveBeenCalledTimes(1)
    expect(res.json).toHaveBeenCalledWith({
      status: 'success',
      data: users
    })
  })
})

test result:

 FAIL  controllers/userController.test.js (7.622 s)
  UserController
    × Expect to respond with 200 code and users data (47 ms)

  ● UserController › Expect to respond with 200 code and users data

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0

      27 |     expect(res.set.mock.calls[1][0]).toBe('X-Total-Count')
      28 |     expect(res.set.mock.calls[1][1]).toBe('2')
    > 29 |     expect(res.status).toHaveBeenCalledTimes(1)
         |                        ^
      30 |     expect(res.status).toHaveBeenCalledWith(200)
      31 |     expect(res.json).toHaveBeenCalledTimes(1)
      32 |     expect(res.json).toHaveBeenCalledWith({

      at Object.<anonymous> (controllers/userController.test.js:29:24)


Interactive Test Progress
 › 1 test remaining


Watch Usage
 › Press s to skip the current test.
 › Press q to quit Interactive Mode.
 › Press Enter to return to watch mode.
 pattern ›

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.