Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 325a1d8

Browse files
committed
feat: add test for catching errors in Emitter
1 parent e0acb08 commit 325a1d8

File tree

1 file changed

+69
-36
lines changed

1 file changed

+69
-36
lines changed

‎test/emitter.test.ts‎

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,85 @@
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"
14
import { Emitter } from "../src/common/emitter"
25

3-
describe("emitter", () => {
4-
describe("Emitter", () => {
5-
it("should return an Emitter", async () => {
6-
const HELLO_WORLD = "HELLO_WORLD"
7-
const GOODBYE_WORLD = "GOODBYE_WORLD"
8-
const mockCallback = jest.fn(() => "Mock function called")
9-
const mockSecondCallback = jest.fn(() => undefined)
6+
describe("Emitter", () => {
7+
let spy: jest.SpyInstance
108

11-
const emitter = new Emitter<{ event: string; callback: () => void }>()
9+
beforeEach(() => {
10+
spy = jest.spyOn(logger, "error")
11+
})
12+
13+
afterEach(() => {
14+
jest.clearAllMocks()
15+
})
16+
17+
afterAll(() => {
18+
jest.restoreAllMocks()
19+
})
20+
21+
it("should run the correct callbacks", async () => {
22+
const HELLO_WORLD = "HELLO_WORLD"
23+
const GOODBYE_WORLD = "GOODBYE_WORLD"
24+
const mockCallback = jest.fn(() => "Mock function called")
25+
const mockSecondCallback = jest.fn(() => undefined)
26+
27+
const emitter = new Emitter<{ event: string; callback: () => void }>()
1228

13-
const onHelloWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
14-
if (event === HELLO_WORLD) {
15-
callback()
16-
}
29+
const onHelloWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
30+
if (event === HELLO_WORLD) {
31+
callback()
1732
}
33+
}
1834

19-
const onGoodbyeWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
20-
if (event === GOODBYE_WORLD) {
21-
callback()
22-
}
35+
const onGoodbyeWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
36+
if (event === GOODBYE_WORLD) {
37+
callback()
2338
}
39+
}
2440

25-
// Register the onHelloWorld listener
26-
// and the onGoodbyeWorld
27-
emitter.event(onHelloWorld)
28-
emitter.event(onGoodbyeWorld)
41+
// Register the onHelloWorld listener
42+
// and the onGoodbyeWorld
43+
emitter.event(onHelloWorld)
44+
emitter.event(onGoodbyeWorld)
2945

30-
await emitter.emit({ event: HELLO_WORLD, callback: mockCallback })
46+
await emitter.emit({ event: HELLO_WORLD, callback: mockCallback })
3147

32-
// Double-check that our callback is called only once
33-
expect(mockCallback).toHaveBeenCalled()
34-
expect(mockCallback).toHaveBeenCalledTimes(1)
48+
// Double-check that our callback is called only once
49+
expect(mockCallback).toHaveBeenCalled()
50+
expect(mockCallback).toHaveBeenCalledTimes(1)
3551

36-
await emitter.emit({ event: GOODBYE_WORLD, callback: mockSecondCallback })
52+
await emitter.emit({ event: GOODBYE_WORLD, callback: mockSecondCallback })
53+
54+
// Check that it works with multiple listeners
55+
expect(mockSecondCallback).toHaveBeenCalled()
56+
expect(mockSecondCallback).toHaveBeenCalledTimes(1)
57+
58+
// Dispose of all the listeners
59+
emitter.dispose()
60+
})
61+
62+
it("should log an error if something goes wrong", async () => {
63+
const HELLO_WORLD = "HELLO_WORLD"
64+
const mockCallback = jest.fn(() => "Mock function called")
65+
const message = "You don't have access to that folder."
66+
67+
const emitter = new Emitter<{ event: string; callback: () => void }>()
68+
69+
const onHelloWorld = ({ event, callback }: { event: string; callback: () => void }): void => {
70+
if (event === HELLO_WORLD) {
71+
callback()
72+
throw new Error(message)
73+
}
74+
}
3775

38-
// Check that it works with multiple listeners
39-
expect(mockSecondCallback).toHaveBeenCalled()
40-
expect(mockSecondCallback).toHaveBeenCalledTimes(1)
76+
emitter.event(onHelloWorld)
4177

42-
// Dispose of all the listeners
43-
emitter.dispose()
44-
})
78+
await emitter.emit({ event: HELLO_WORLD, callback: mockCallback })
4579

46-
it.skip("should log an error if something goes wrong", () => {
47-
// not sure how we're going to test this
48-
// need to mock logger
49-
// and then somehow throw or something in the callback
50-
})
80+
// Check that error was called
81+
expect(spy).toHaveBeenCalled()
82+
expect(spy).toHaveBeenCalledTimes(1)
83+
expect(spy).toHaveBeenCalledWith(message)
5184
})
5285
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /