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