From 7cf4cd1d3d8d077a4ec99affcb8a1d5c5a53974c Mon Sep 17 00:00:00 2001 From: Kathleen Koh Date: Wed, 6 Mar 2024 15:23:30 +0800 Subject: [PATCH] chore: fix failing backend tests --- .../admin-form.assistance.controller.spec.ts | 41 ++++++------------- .../admin-form.assistance.service.spec.ts | 40 +++++------------- 2 files changed, 22 insertions(+), 59 deletions(-) diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.assistance.controller.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.assistance.controller.spec.ts index 84c571c471..6532e55171 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.assistance.controller.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.assistance.controller.spec.ts @@ -16,32 +16,14 @@ const mockReturnValue = { } // Mock azure openai -jest.mock('@azure/openai', () => { - return { - AzureKeyCredential: jest.fn().mockImplementation((apiKey) => { - return apiKey - }), - OpenAIClient: jest.fn().mockImplementation(() => { - return { - getChatCompletions: jest.fn().mockResolvedValue({ - choices: [ - { - message: { - role: 'user', - content: 'dummy content', - }, - }, - ], - }), - } - }), - } -}) - -const MockedOpenAIClient = jest.mocked(OpenAIClient) +jest.mock('@azure/openai') beforeEach(() => { jest.clearAllMocks() + + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') + .mockResolvedValue({ choices: [{ message: mockReturnValue }] } as any) }) describe('admin-form.assistance.controller', () => { @@ -91,9 +73,10 @@ describe('admin-form.assistance.controller', () => { }) // Mock OpenAI API throwing an error - MockedOpenAIClient.prototype.getChatCompletions = jest - .fn() - .mockRejectedValue(new Error('Some random error message')) + + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') + .mockRejectedValue({ choices: [{ message: mockReturnValue }] } as any) // Act await handleGenerateQuestions(MOCK_REQ, mockRes, jest.fn()) @@ -148,9 +131,9 @@ describe('admin-form.assistance.controller', () => { }) // Mock OpenAI API throwing an error - MockedOpenAIClient.prototype.getChatCompletions = jest - .fn() - .mockRejectedValue(new Error('Some random error message')) + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') + .mockRejectedValue({ choices: [{ message: mockReturnValue }] } as any) // Act await handleGenerateFormFields(MOCK_REQ, mockRes, jest.fn()) diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.assistance.service.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.assistance.service.spec.ts index 14b50c9fb1..9e13627840 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.assistance.service.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.assistance.service.spec.ts @@ -14,33 +14,12 @@ const mockReturnValue = { content: 'dummy content', } -// Mock azure openai -jest.mock('@azure/openai', () => { - return { - AzureKeyCredential: jest.fn().mockImplementation((apiKey) => { - return apiKey - }), - OpenAIClient: jest.fn().mockImplementation(() => { - return { - getChatCompletions: jest.fn().mockResolvedValue({ - choices: [ - { - message: { - role: 'user', - content: 'dummy content', - }, - }, - ], - }), - } - }), - } -}) - -const MockedOpenAIClient = jest.mocked(OpenAIClient) - beforeEach(() => { jest.clearAllMocks() + + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') + .mockResolvedValue({ choices: [{ message: mockReturnValue }] } as any) }) describe('admin-form.assistance.service', () => { @@ -78,13 +57,13 @@ describe('admin-form.assistance.service', () => { const content = 'Sample content' // Mock Azure OpenAI API throwing an error - MockedOpenAIClient.prototype.getChatCompletions = jest - .fn() + + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') .mockRejectedValue(new Error('Some random error message')) // Act const actualResult = await generateQuestions({ type, content }) - // assuming the generateQuestions function will be using the mocked openai API? // Assert expect(actualResult.isErr()).toEqual(true) @@ -129,8 +108,9 @@ describe('admin-form.assistance.service', () => { const questions = 'sample questions' // Mock OpenAI API throwing an error - MockedOpenAIClient.prototype.getChatCompletions = jest - .fn() + + jest + .spyOn(OpenAIClient.prototype, 'getChatCompletions') .mockRejectedValue(new Error('Some random error message')) // Act