-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: the browser logger and improve tests for logger package (#5883)
* Restructure the unit tests for logger * Add browser tests support * Move logger test utils to relevant package
- Loading branch information
1 parent
fa5c6cf
commit 4967d38
Showing
20 changed files
with
404 additions
and
338 deletions.
There are no files selected for viewing
3 changes: 1 addition & 2 deletions
3
packages/logger/test/unit/logger.test.ts → packages/cli/test/unit/util/logger.test.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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
colors: true | ||
node-option: | ||
- "loader=ts-node/esm" | ||
require: | ||
- ./test/setup.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,9 @@ | ||
const karmaConfig = require("../../karma.base.config.js"); | ||
const webpackConfig = require("./webpack.test.config.cjs"); | ||
|
||
module.exports = function karmaConfigurator(config) { | ||
config.set({ | ||
...karmaConfig, | ||
webpack: webpackConfig, | ||
}); | ||
}; |
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
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
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,48 @@ | ||
import {LodestarError} from "@lodestar/utils"; | ||
import {LogData, LogFormat} from "../../src/index.js"; | ||
|
||
type TestCase = { | ||
id: string; | ||
message: string; | ||
context?: LogData; | ||
error?: Error; | ||
output: {[P in LogFormat]: string}; | ||
}; | ||
|
||
/* eslint-disable quotes */ | ||
export const formatsTestCases: (TestCase | (() => TestCase))[] = [ | ||
{ | ||
id: "regular log with metadata", | ||
message: "foo bar", | ||
context: {meta: "data"}, | ||
output: { | ||
human: "[] \u001b[33mwarn\u001b[39m: foo bar meta=data", | ||
json: '{"context":{"meta":"data"},"level":"warn","message":"foo bar","module":""}', | ||
}, | ||
}, | ||
|
||
{ | ||
id: "regular log with big int metadata", | ||
message: "big int", | ||
context: {data: BigInt(1)}, | ||
output: { | ||
human: "[] \u001b[33mwarn\u001b[39m: big int data=1", | ||
json: '{"context":{"data":"1"},"level":"warn","message":"big int","module":""}', | ||
}, | ||
}, | ||
|
||
() => { | ||
const error = new LodestarError({code: "SAMPLE_ERROR", data: {foo: "bar"}}); | ||
error.stack = "$STACK"; | ||
return { | ||
id: "error with metadata", | ||
opts: {format: "human", module: "SAMPLE"}, | ||
message: "foo bar", | ||
error: error, | ||
output: { | ||
human: `[] \u001b[33mwarn\u001b[39m: foo bar code=SAMPLE_ERROR, data=foo=bar\n${error.stack}`, | ||
json: '{"error":{"code":"SAMPLE_ERROR","data":{"foo":"bar"},"stack":"$STACK"},"level":"warn","message":"foo bar","module":""}', | ||
}, | ||
}; | ||
}, | ||
]; |
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,25 @@ | ||
import {expect} from "chai"; | ||
import {LogLevel} from "@lodestar/utils"; | ||
import {stubLoggerForConsole} from "@lodestar/test-utils/mocha"; | ||
import {TimestampFormatCode, logFormats} from "../../src/index.js"; | ||
import {formatsTestCases} from "../fixtures/loggerFormats.js"; | ||
import {getBrowserLogger} from "../../src/browser.js"; | ||
|
||
describe("browser logger", () => { | ||
describe("format and options", () => { | ||
for (const testCase of formatsTestCases) { | ||
const {id, message, context, error, output} = typeof testCase === "function" ? testCase() : testCase; | ||
for (const format of logFormats) { | ||
it(`${id} ${format} output`, async () => { | ||
const logger = stubLoggerForConsole( | ||
getBrowserLogger({level: LogLevel.info, format, timestampFormat: {format: TimestampFormatCode.Hidden}}) | ||
); | ||
|
||
logger.warn(message, context, error); | ||
logger.restoreStubs(); | ||
expect(logger.getLogs()).deep.equals([output[format]]); | ||
}); | ||
} | ||
} | ||
}); | ||
}); |
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,28 @@ | ||
import {expect} from "chai"; | ||
import {LogLevel} from "@lodestar/utils"; | ||
import {stubLoggerForConsole} from "@lodestar/test-utils/mocha"; | ||
import {TimestampFormatCode, logFormats} from "../../src/index.js"; | ||
import {formatsTestCases} from "../fixtures/loggerFormats.js"; | ||
import {getEnvLogger} from "../../src/env.js"; | ||
|
||
describe("env logger", () => { | ||
describe("format and options", () => { | ||
for (const testCase of formatsTestCases) { | ||
const {id, message, context, error, output} = typeof testCase === "function" ? testCase() : testCase; | ||
for (const format of logFormats) { | ||
it(`${id} ${format} output`, async () => { | ||
// Set env variables | ||
process.env.LOG_LEVEL = LogLevel.info; | ||
process.env.LOG_FORMAT = format; | ||
process.env.LOG_TIMESTAMP_FORMAT = TimestampFormatCode.Hidden; | ||
|
||
const logger = stubLoggerForConsole(getEnvLogger()); | ||
|
||
logger.warn(message, context, error); | ||
logger.restoreStubs(); | ||
expect(logger.getLogs()).deep.equals([output[format]]); | ||
}); | ||
} | ||
} | ||
}); | ||
}); |
Oops, something went wrong.