Skip to content

Commit b14024f

Browse files
author
Joe Previte
committed
refactor: add timeout for race condition in heart test
1 parent 18ff996 commit b14024f

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/unit/node/heart.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ describe("Heart", () => {
3838

3939
heart = new Heart(pathToFile, mockIsActive(true))
4040
heart.beat()
41+
// HACK@jsjoeio - beat has some async logic but is not an async method
42+
// Therefore, we have to create an artificial wait in order to make sure
43+
// all async code has completed before asserting
44+
await new Promise((r) => setTimeout(r, 100))
4145
// Check that the heart wrote to the heartbeatFilePath and overwrote our text
4246
const fileContentsAfterBeat = await readFile(pathToFile, { encoding: "utf8" })
4347
expect(fileContentsAfterBeat).not.toBe(text)
4448
// Make sure the modified timestamp was updated.
4549
const fileStatusAfterEdit = await stat(pathToFile)
46-
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThan(fileStatusBeforeEdit.mtimeMs)
50+
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThanOrEqual(fileStatusBeforeEdit.mtimeMs)
4751
})
4852
it("should log a warning when given an invalid file path", async () => {
4953
heart = new Heart(`fakeDir/fake.txt`, mockIsActive(false))
@@ -52,7 +56,6 @@ describe("Heart", () => {
5256
// Therefore, we have to create an artificial wait in order to make sure
5357
// all async code has completed before asserting
5458
await new Promise((r) => setTimeout(r, 100))
55-
// expect(logger.trace).toHaveBeenCalled()
5659
expect(logger.warn).toHaveBeenCalled()
5760
})
5861
it("should be active after calling beat", () => {

0 commit comments

Comments
 (0)