Home > Software design >  Mocking a function within an express request for a Jest Unit test
Mocking a function within an express request for a Jest Unit test

Time:09-15

I have a simple express application and i want to fully be able to mock it, here's what i've got so far

userRoute.js

const express = require("express")
const router = express.Router()
const db = require('./db')

router.get("/users/", async (req, res) => {
  res.json({
    status: 200,
    data: await db.getUsers(),
  })
})

module.exports = router

My issue is i am trying to mock the db.getUsers function but not sure how

here is my test code:

const router = require("./userRoute.js")

const app = new express()
app.use("/", router)

describe("Users Route", () => {
  test("getUsers Happy Path", async () => {
    jest.spyOn(db, "getUsers").mockImplementation(() => {
      return {
         id:12345
         name: "Jason"
         age: "34"
       }
    })

    const res = await app.get("/users/")
    console.log(res)
  })
})

this doesn't work for me, if i run the function regularly in a standard function it works but since its an api endpoint in a route it doesn't work for whatever reason, any help would be fantastic

CodePudding user response:

Maybe you want to try to mock the db before require the useRouter.js

Also, you need to run the server (and close it after all tests) and make a real request to your server.

const express = require('express');
const axios = require('axios');
const PORT = 5565;

const userMock = {
  id: 1,
  name: 'John Doe',
  email: '[email protected]',
}
const dbSpy = jest.spyOn(require('./db.js'), 'getUsers').mockImplementation(() => userMock);

const router = require('./index.js');

const app = express();

app.use('/', router);

const server = app.listen(PORT, () => 'Example app listening on port 3000!');

describe('Users Route', () => {
  afterAll(() => server.close());
  test('getUsers Happy Path', async () => {
    const response = await axios.get(`http://localhost:${PORT}/users/`);      

    expect(dbSpy).toHaveBeenCalledTimes(1);
    expect(response.status).toBe(200);
    expect(response.data.data).toEqual(userMock);
  });  
})
  • Related