I’m trying to write tests for the CreateVendor function using Jest and Supertest. I’m stuck on how to mock the dependencies (generateSalt, hashPassword) to properly test the function’s behavior. I’m getting this error: “Property generateSalt does not exist in the provided object.
This concise statement should effectively explain your difficulty and provide context for your mentor to assist you.
Controller function
export const CreateVendor = async (req: Request, res: Response, next: NextFunction) => {
const { address, email, foodType, name, ownerName, password, phone, pincode } = <CreateVendorInput>req.body
try {
const findOne = await findVendor('', email)
if (!findOne) {
const bcryptSalt = await generateSalt()
const bcryptPassword = await hashPassword(password, bcryptSalt)
const CreateVendor = await Vendor.create({
name: name,
email: email,
address: address,
foodType: foodType,
ownerName: ownerName,
password: bcryptPassword,
phone: phone,
pincode: pincode,
salt: bcryptSalt,
rating: 0,
serviceAvailable: false,
coverImage: [],
})
return res.status(201).json({ "success": CreateVendor })
} else {
throw new BadRequestError('Vendor Already Exists', 'Admin/CreateVendor')
}
} catch (err) {
next(err)
}
}
Test file
it('creates a new vendor successfully', async () => {
//@ts-ignore
const mockVendorData: CreateVendorInput = test_data.vendor
// const mockSalt = jest.fn().mockReturnValue('mockedSalt'); // Replace with a sample salt string
// const mockHashedPassword = jest.fn().mockResolvedValue('hashedPassword123');
//@ts-ignore
jest.spyOn(Vendor, 'create').mockResolvedValueOnce(mockVendorData)
//@ts-ignore
jest.spyOn(Vendor, 'find').mockResolvedValueOnce(null)
// jest.mocked(generateSalt).mockImplementation(mockSalt);
// jest.mocked(hashPassword).mockImplementation(mockHashedPassword);
const res = await supertest(app)
.post('/admin/vendor') // Adjust path if necessary
.send(mockVendorData)
.expect(201); // Expect successful creation status code
console.log(res.body)
expect(res.body.success).toHaveProperty('name', mockVendorData.name); // Check name in response
expect(res.body.success).toHaveProperty('email', mockVendorData.email);
}, 50000);
Error
CreateVendor function
× creates a new vendor successfully (10063 ms)
● CreateVendor function › creates a new vendor successfully
expected 201 "Created", got 404 "Not Found"
91 | .post('/admin/vendor') // Adjust path if necessary
92 | .send(mockVendorData)
> 93 | .expect(201); // Expect successful creation status code
| ^
94 | console.log(res.body)
95 | expect(res.body.success).toHaveProperty('name', mockVendorData.name); // Check name in response
96 | expect(res.body.success).toHaveProperty('email', mockVendorData.email);
at source/__test__/controllers/AdminController.test.ts:93:18
at source/__test__/controllers/AdminController.test.ts:8:71
at Object.<anonymous>.__awaiter (source/__test__/controllers/AdminController.test.ts:4:12)
at Object.<anonymous> (source/__test__/controllers/AdminController.test.ts:76:60)
----
at Test._assertStatus (node_modules/supertest/lib/test.js:252:14)
at node_modules/supertest/lib/test.js:308:13
at Test._assertFunction (node_modules/supertest/lib/test.js:285:13)
at Test.assert (node_modules/supertest/lib/test.js:164:23)
at Server.localAssert (node_modules/supertest/lib/test.js:120:14)
I tried importing the generateSalt
function from the ../../utilities.js
file and using jest.spyOn
to mock it. I provided a mock implementation that always returns a string value for the salt.
I expected the test to pass and the mocked generateSalt
function to be called once during the execution of the CreateVendor
function. However, the test fails.
Haris Bukhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.