Skip to content

Commit d8864ad

Browse files
committed
feat: add tests for logError
1 parent 513e7fe commit d8864ad

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

test/util.test.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
import { logger as l } from "@coder/logger"
2-
import { arrayify, getFirstString, normalize, plural, resolveBase, split, trimSlashes } from "../src/common/util"
1+
// Note: we need to import logger from the root
2+
// because this is the logger used in logError in ../src/common/util
3+
import { logger } from "../node_modules/@coder/logger"
4+
import {
5+
arrayify,
6+
getFirstString,
7+
logError,
8+
normalize,
9+
plural,
10+
resolveBase,
11+
split,
12+
trimSlashes,
13+
} from "../src/common/util"
314

415
type LocationLike = Pick<Location, "pathname" | "origin">
516

@@ -96,6 +107,7 @@ describe("util", () => {
96107
it("should return value it's already an array", () => {
97108
expect(arrayify(["hello", "world"])).toStrictEqual(["hello", "world"])
98109
})
110+
99111
it("should wrap the value in an array if not an array", () => {
100112
expect(
101113
arrayify({
@@ -104,6 +116,7 @@ describe("util", () => {
104116
}),
105117
).toStrictEqual([{ name: "Coder", version: "3.8" }])
106118
})
119+
107120
it("should return an empty array if the value is undefined", () => {
108121
expect(arrayify(undefined)).toStrictEqual([])
109122
})
@@ -113,11 +126,46 @@ describe("util", () => {
113126
it("should return the string if passed a string", () => {
114127
expect(getFirstString("Hello world!")).toBe("Hello world!")
115128
})
129+
116130
it("should get the first string from an array", () => {
117131
expect(getFirstString(["Hello", "World"])).toBe("Hello")
118132
})
133+
119134
it("should return undefined if the value isn't an array or a string", () => {
120135
expect(getFirstString({ name: "Coder" })).toBe(undefined)
121136
})
122137
})
138+
139+
describe("logError", () => {
140+
let spy: jest.SpyInstance
141+
142+
beforeEach(() => {
143+
spy = jest.spyOn(logger, "error")
144+
})
145+
146+
afterEach(() => {
147+
jest.clearAllMocks()
148+
})
149+
150+
afterAll(() => {
151+
jest.restoreAllMocks()
152+
})
153+
154+
it("should log an error with the message and stack trace", () => {
155+
const message = "You don't have access to that folder."
156+
const error = new Error(message)
157+
158+
logError("ui", error)
159+
160+
expect(spy).toHaveBeenCalled()
161+
expect(spy).toHaveBeenCalledWith(`ui: ${error.message} ${error.stack}`)
162+
})
163+
164+
it("should log an error, even if not an instance of error", () => {
165+
logError("api", "oh no")
166+
167+
expect(spy).toHaveBeenCalled()
168+
expect(spy).toHaveBeenCalledWith("api: oh no")
169+
})
170+
})
123171
})

0 commit comments

Comments
 (0)