-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use appropriate verbosity levels specifically in the browser en…
…vironment
- Loading branch information
1 parent
832a074
commit ecd2387
Showing
4 changed files
with
116 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@vue-storefront/logger": minor | ||
--- | ||
|
||
use appropriate verbosity levels in the browser environment |
96 changes: 96 additions & 0 deletions
96
packages/logger/__tests__/unit/reporter/consola/jsonReporter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { jsonReporter } from "../../../../src/reporters/consola/jsonReporter"; | ||
|
||
describe("jsonReporter", () => { | ||
let consoleSpy: jest.SpyInstance; | ||
let warnSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => {}); | ||
warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleSpy.mockRestore(); | ||
}); | ||
|
||
it("should log structuredLog directly in development environment", () => { | ||
process.env.NODE_ENV = "development"; | ||
const logObject: any = { | ||
type: "log", | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" }); | ||
}); | ||
|
||
it("should log structuredLog directly in browser environment", () => { | ||
delete process.env.NODE_ENV; | ||
(global as any).window = {}; | ||
|
||
const logObject: any = { | ||
type: "log", | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" }); | ||
|
||
delete (global as any).window; | ||
}); | ||
|
||
it("should log structuredLog as JSON in production environment", () => { | ||
process.env.NODE_ENV = "production"; | ||
const logObject: any = { | ||
type: "log", | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith( | ||
JSON.stringify({ message: "test message" }) | ||
); | ||
}); | ||
|
||
it("should use default log type if log type is not defined", () => { | ||
const logObject: any = { | ||
type: undefined, | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith( | ||
JSON.stringify({ message: "test message" }) | ||
); | ||
}); | ||
|
||
it("should use default log type if console does not support the log type", () => { | ||
const logObject: any = { | ||
type: "unsupportedType", | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(consoleSpy).toHaveBeenCalledWith( | ||
JSON.stringify({ message: "test message" }) | ||
); | ||
}); | ||
|
||
it("should use warn log type if log type is warn", () => { | ||
const logObject: any = { | ||
type: "warn", | ||
args: [{ structuredLog: { message: "test message" } }], | ||
}; | ||
|
||
jsonReporter(logObject); | ||
|
||
expect(warnSpy).toHaveBeenCalledWith( | ||
JSON.stringify({ message: "test message" }) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { LogObject } from "consola"; | ||
|
||
export const jsonReporter = (logObject: LogObject) => { | ||
const defLogType = "log"; | ||
const logType = logObject?.type ?? defLogType; | ||
const logFn = console[logType] ?? console[defLogType]; | ||
|
||
if (process.env.NODE_ENV === "development" || typeof window !== "undefined") { | ||
logFn(logObject.args[0].structuredLog); | ||
} else { | ||
logFn(JSON.stringify(logObject.args[0].structuredLog)); | ||
} | ||
}; |