Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for toErrorMessage utility #161

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/__tests__/unit/utils/to-error-message.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AxiosError } from "axios"
import { describe, expect, it } from "vitest"

import { toErrorMessage } from "@/utils/to-error-message"

describe("toErrorMessage", () => {
it("should return the error message from the response", () => {
const mockError = {
response: {
data: {
message: "Custom error message",
},
},
} as AxiosError

const result = toErrorMessage(mockError)
expect(result).toBe("Custom error message")
})

it("should return default message if error message is not provided", () => {
const mockError = {} as AxiosError

const result = toErrorMessage(mockError)
expect(result).toBe("Something went wrong, please try again")
})

it("should return default message if response or data is undefined", () => {
const mockError = {
response: {
data: undefined,
},
} as AxiosError

const result = toErrorMessage(mockError)
expect(result).toBe("Something went wrong, please try again")
})
})
2 changes: 1 addition & 1 deletion src/utils/to-error-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { AxiosError } from "axios"

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const toErrorMessage = (error: AxiosError<any>) => {
const message = error.response?.data.message
const message = error.response?.data?.message
return message || "Something went wrong, please try again"
}
Loading