-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(frontend): replace console with custom logger #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { LogManager } from "@hatchgrid/logger"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { nextTick } from "vue"; | ||
| import { useLocalStorage } from "../useLocalStorage"; | ||
|
|
@@ -90,21 +91,23 @@ describe("useLocalStorage", () => { | |
|
|
||
| it("handles invalid JSON gracefully and returns default value", () => { | ||
| const key = "test-key-invalid-json"; | ||
| const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
| const logger = LogManager.getLogger("web:use-local-storage"); | ||
| const loggerSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); | ||
|
Comment on lines
+94
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Consider extracting logger spy setup to reduce duplication. The logger mocking pattern is correctly implemented and consistent across tests. However, there's repetition in creating the logger instance and spy setup. Consider extracting the logger spy setup to a helper function or using describe("useLocalStorage", () => {
const defaultValue = { foo: "bar" };
+ let loggerSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
localStorageMock.clear();
vi.clearAllMocks();
+ const logger = LogManager.getLogger("web:use-local-storage");
+ loggerSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
});
afterEach(() => {
localStorageMock.clear();
+ loggerSpy?.mockRestore();
});Then remove the individual logger setup from each test and just use Also applies to: 103-104, 109-110, 120-121, 126-127, 139-140 🤖 Prompt for AI Agents |
||
|
|
||
| // Setup the mock to return invalid JSON | ||
| localStorageMock.getItem.mockReturnValueOnce("{invalid-json"); | ||
|
|
||
| const [state] = useLocalStorage(key, defaultValue); | ||
|
|
||
| expect(state.value).toEqual(defaultValue); | ||
| expect(consoleSpy).toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| expect(loggerSpy).toHaveBeenCalled(); | ||
| loggerSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("handles localStorage errors gracefully", () => { | ||
| const key = "test-key-error-get"; | ||
| const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
| const logger = LogManager.getLogger("web:use-local-storage"); | ||
| const loggerSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); | ||
|
|
||
| // Setup the mock to throw an error | ||
| localStorageMock.getItem.mockImplementationOnce(() => { | ||
|
|
@@ -114,13 +117,14 @@ describe("useLocalStorage", () => { | |
| const [state] = useLocalStorage(key, defaultValue); | ||
|
|
||
| expect(state.value).toEqual(defaultValue); | ||
| expect(consoleSpy).toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| expect(loggerSpy).toHaveBeenCalled(); | ||
| loggerSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it("handles setItem errors gracefully", async () => { | ||
| const key = "test-key-error-set"; | ||
| const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); | ||
| const logger = LogManager.getLogger("web:use-local-storage"); | ||
| const loggerSpy = vi.spyOn(logger, "warn").mockImplementation(() => {}); | ||
|
|
||
| // Setup the mock to throw an error | ||
| localStorageMock.setItem.mockImplementationOnce(() => { | ||
|
|
@@ -132,7 +136,7 @@ describe("useLocalStorage", () => { | |
| setState({ foo: "error" }); | ||
| await nextTick(); | ||
|
|
||
| expect(consoleSpy).toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| expect(loggerSpy).toHaveBeenCalled(); | ||
| loggerSpy.mockRestore(); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick (assertive)
Minor formatting nitpick
There's an extra blank line that could be removed for cleaner code formatting.
const logger = LogManager.getLogger("landing-page:category-service"); -📝 Committable suggestion
🤖 Prompt for AI Agents