Jest 파일 구조

describe("Produt Controller create", () => {
	beforeEach(() => {
		req.body = newProduct;
	});

	it("should have a createProduct function", () => {
		expect(typeof productController.createProduct).toBe("function");
	});

	it("should call Product.create", async () => {
		await productController.createProduct(req, res, next);
		
		expect(Product.create).toBeCalledWith(newProduct);
	});

	it("should return 201 response code", async () => {
		await productController.createProduct(req, res, next);

		expect(res.statusCode).toBe(201);
		expect(res._isEndCalled()).toBeTruthy();
	});

	it("should return json body in response", async () => {
		Product.create.mockReturnValue(newProduct);

		await productController.createProduct(req, res, next);
		
		expect(res._getJSONData()).toStrictEqual(newProduct);
	});

	it("should handle errors", async () => {
		const errorMessage = { message: "Done property missing" };
		const rejectedPromise = Promise.reject(errorMessage);

		Product.create.mockReturnValue(rejectedPromise);

		await productController.createProduct(req, res, next);

		expect(next).toBeCalledWith(errorMessage);
	});
});

스크린샷 2024-01-09 오후 1.24.20.png

스크린샷 2024-01-09 오후 1.30.26.png

스크린샷 2024-01-09 오후 1.32.02.png