Skip to content

Commit

Permalink
chore: fix failing backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kathleenkhy committed Mar 6, 2024
1 parent 0514e77 commit 7cf4cd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7cf4cd1

Please sign in to comment.