Skip to content

Commit 8e7ccbf

Browse files
committed
feat: set up new test for beat twice
1 parent 4e93db5 commit 8e7ccbf

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/node/heart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Heart {
3333
if (typeof this.heartbeatTimer !== "undefined") {
3434
clearTimeout(this.heartbeatTimer)
3535
}
36-
this.heartbeatTimer = setTimeout(() => heartbeatTimer(this.isActive, this.beat), this.heartbeatInterval)
36+
this.heartbeatTimer = setTimeout(async () => await heartbeatTimer(this.isActive, this.beat), this.heartbeatInterval)
3737
}
3838

3939
/**

test/unit/node/heart.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe("Heart", () => {
2323
})
2424
afterEach(() => {
2525
jest.resetAllMocks()
26+
jest.useRealTimers()
2627
if (heart) {
2728
heart.dispose()
2829
}
@@ -75,6 +76,17 @@ describe("Heart", () => {
7576
const isAlive = heart.alive()
7677
expect(isAlive).toBe(false)
7778
})
79+
it("should beat twice and maintain reference to Heart", async () => {
80+
// Use fake timers so we can speed up setTimeout
81+
jest.useFakeTimers()
82+
heart = new Heart(`${testDir}/hello.txt`, mockIsActive(true))
83+
heart.beat()
84+
// we need to speed up clocks, timeouts
85+
// call heartbeat again (and it won't be alive I think)
86+
// then assert no warnings were called
87+
jest.runAllTimers()
88+
expect(logger.warn).not.toHaveBeenCalled()
89+
})
7890
})
7991

8092
describe("heartbeatTimer", () => {

0 commit comments

Comments
 (0)