-
-
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.
Restructure the unit tests for logger
- Loading branch information
1 parent
664820a
commit f076f05
Showing
17 changed files
with
385 additions
and
336 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
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 {TimestampFormatCode, logFormats} from "../../src/index.js"; | ||
import {formatsTestCases} from "../fixtures/loggerFormats.js"; | ||
import {stubLoggerForConsole} from "../utils/chai.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 {TimestampFormatCode, logFormats} from "../../src/index.js"; | ||
import {formatsTestCases} from "../fixtures/loggerFormats.js"; | ||
import {stubLoggerForConsole} from "../utils/chai.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]]); | ||
}); | ||
} | ||
} | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.