Skip to content

Commit f110bb6

Browse files
committed
feat: add test for hash when error
1 parent 439f27c commit f110bb6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

test/unit/node/util.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as cp from "child_process"
2+
import { logger } from "@coder/logger"
23
import { promises as fs } from "fs"
34
import * as path from "path"
45
import { generateUuid } from "../../../src/common/util"
56
import { tmpdir } from "../../../src/node/constants"
67
import * as util from "../../../src/node/util"
8+
import { mockLogger } from "../../utils/helpers"
79

810
describe("getEnvPaths", () => {
911
describe("on darwin", () => {
@@ -99,11 +101,31 @@ describe("getEnvPaths", () => {
99101
})
100102

101103
describe("hash", () => {
104+
beforeAll(() => {
105+
mockLogger()
106+
})
107+
108+
afterEach(() => {
109+
jest.clearAllMocks()
110+
})
111+
102112
it("should return a hash of the string passed in", async () => {
103113
const plainTextPassword = "mySecretPassword123"
104114
const hashed = await util.hash(plainTextPassword)
105115
expect(hashed).not.toBe(plainTextPassword)
106116
})
117+
it("should log an error and return an empty string on error", async () => {
118+
// Although we don't expect to pass the wrong type to our `hash`
119+
// function, we do want to make sure our function handles errors correctly.
120+
// Therefore, we mock a scenario where the password is `undefined`
121+
// which gets us into the catch block and checks that we handle errors by
122+
// logging it and returning an empty string (as opposed to throwing an Error)
123+
// and crashing our program for a silly password error.
124+
// @ts-expect-error See description above
125+
const hashed = await util.hash(undefined)
126+
expect(hashed).toBe("")
127+
expect(logger.error).toHaveBeenCalled()
128+
})
107129
})
108130

109131
describe("isHashMatch", () => {

0 commit comments

Comments
 (0)