Skip to content

Commit

Permalink
Add file path test for watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-face committed Jul 25, 2024
1 parent cad12f3 commit 5ef6fb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export function startFileWatcher(watchPaths: string[], callback: Function): FSWa
const absolutePath = path.resolve(relativePath)
callback(event, absolutePath);
});

return watcher;
}
40 changes: 40 additions & 0 deletions tests/unit/devNoMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { startFileWatcher } from "@/lib/watcher";
import path from "path";
import fs from "fs";

describe("File Watcher Tests", () => {
let watcher;
const tempDirPath = path.join(__dirname, "temp-test");

it("should call the file watcher callback with the correct attributes", async () => {
const mockCallback = jest.fn();

fs.mkdirSync(tempDirPath, { recursive: true });

const tempFilePath = path.join(tempDirPath, "temp-file.txt");

const relativeTempFilePath = path.relative("./", tempFilePath)

fs.writeFileSync(tempFilePath, "initial content");

watcher = startFileWatcher([relativeTempFilePath], mockCallback);

await new Promise((resolve) => setTimeout(resolve, 1000));

fs.appendFileSync(tempFilePath, "\nadded content");

await new Promise((resolve) => setTimeout(resolve, 1000));

expect(mockCallback).toHaveBeenCalled();
expect(mockCallback.mock.calls[0][0]).toBe("change");
expect(mockCallback.mock.calls[0][1]).toBe(path.resolve(tempFilePath));
});

afterAll(async () => {
if (watcher) await watcher.close();

if (fs.existsSync(tempDirPath)) {
fs.rmSync(tempDirPath, { recursive: true, force: true });
}
});
});

0 comments on commit 5ef6fb2

Please sign in to comment.