Skip to content

Commit b44ba1e

Browse files
committed
Fix helpers not working in e2e tests
It errors that jest is not defined so put it behind a function instead of immediately creating the mock (this is probably a better pattern anyway).
1 parent 6f989f0 commit b44ba1e

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

test/unit/constants.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { commit, getPackageJson, version } from "../../src/node/constants"
2-
import { loggerModule } from "../utils/helpers"
2+
import { createLoggerMock } from "../utils/helpers"
33

44
// jest.mock is hoisted above the imports so we must use `require` here.
5+
const loggerModule = createLoggerMock()
56
jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)
67

78
describe("constants", () => {

test/unit/register.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSDOM } from "jsdom"
22
import { registerServiceWorker } from "../../src/browser/register"
3-
import { loggerModule } from "../utils/helpers"
3+
import { createLoggerMock } from "../utils/helpers"
44
import { LocationLike } from "./util.test"
55

66
describe("register", () => {
@@ -21,6 +21,8 @@ describe("register", () => {
2121
})
2222
})
2323

24+
const loggerModule = createLoggerMock()
25+
2426
beforeEach(() => {
2527
jest.clearAllMocks()
2628
jest.mock("@coder/logger", () => loggerModule)

test/unit/util.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
trimSlashes,
1212
normalize,
1313
} from "../../src/common/util"
14-
import { loggerModule } from "../utils/helpers"
14+
import { createLoggerMock} from "../utils/helpers"
1515

1616
const dom = new JSDOM()
1717
global.document = dom.window.document
@@ -229,6 +229,8 @@ describe("util", () => {
229229
jest.restoreAllMocks()
230230
})
231231

232+
const loggerModule = createLoggerMock()
233+
232234
it("should log an error with the message and stack trace", () => {
233235
const message = "You don't have access to that folder."
234236
const error = new Error(message)

test/utils/helpers.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ import * as fs from "fs"
22
import * as os from "os"
33
import * as path from "path"
44

5-
export const loggerModule = {
6-
field: jest.fn(),
7-
level: 2,
8-
logger: {
9-
debug: jest.fn(),
10-
error: jest.fn(),
11-
info: jest.fn(),
12-
trace: jest.fn(),
13-
warn: jest.fn(),
14-
},
5+
/**
6+
* Return a mock of @coder/logger.
7+
*/
8+
export function createLoggerMock() {
9+
return {
10+
field: jest.fn(),
11+
level: 2,
12+
logger: {
13+
debug: jest.fn(),
14+
error: jest.fn(),
15+
info: jest.fn(),
16+
trace: jest.fn(),
17+
warn: jest.fn(),
18+
},
19+
}
1520
}
1621

1722
/**

0 commit comments

Comments
 (0)