Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Nov 10, 2024
1 parent 1c4470d commit 1d6f5ee
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/node": "~16.11.7",
"prettier": "~2.8.1",
"rimraf": "~3.0.2",
"strip-ansi": "^7.1.0",
"supports-color": "^9.4.0",
"typescript": "~5.0.4",
"vitest": "^2.0.5"
Expand Down
48 changes: 35 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions tests/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,68 @@
exports[`logger > should create new logger with info level correctly 1`] = `
[
[
"[1m[31merror [39m[22m this is a error message",
"error this is a error message",
],
[
"[1m[36minfo [39m[22m this is an info message",
"info this is an info message",
],
[
"[1m[33mwarn [39m[22m this is a warn message",
"warn this is a warn message",
],
[
"[1m[32mready [39m[22m this is a ready message",
"ready this is a ready message",
],
[
"[1m[32msuccess[39m[22m this is a success message",
"success this is a success message",
],
]
`;

exports[`logger > should create new logger with warn level correctly 1`] = `
[
[
"[1m[31merror [39m[22m this is a error message",
"error this is a error message",
],
[
"[1m[33mwarn [39m[22m this is a warn message",
"warn this is a warn message",
],
]
`;

exports[`logger > should log as expected 1`] = `
[
[
"[1m[38;2;179;250;236m😊[39m[38;2;179;250;236m [39m[38;2;170;245;228mR[39m[38;2;160;240;221ms[39m[38;2;151;235;213ml[39m[38;2;141;230;206mo[39m[38;2;131;224;199mg[39m[38;2;131;224;199m [39m[38;2;122;219;191mv[39m[38;2;112;214;184m1[39m[38;2;103;209;176m.[39m[38;2;93;204;169m0[39m[38;2;84;199;161m.[39m[38;2;74;194;154m0[39m[38;2;74;194;154m
[39m[22m",
"😊 Rslog v1.0.0
",
],
[
"this is a log message",
],
[
"[1m[36minfo [39m[22m this is an info message",
"info this is an info message",
],
[
"[1m[33mwarn [39m[22m this is a warn message",
"warn this is a warn message",
],
[
"[1m[32mready [39m[22m this is a ready message",
"ready this is a ready message",
],
[
"[1m[32msuccess[39m[22m this is a success message",
"success this is a success message",
],
]
`;

exports[`logger > should log error with stack correctly 1`] = `
[1m[31merror [39m[22m this is an error message
[90m at <ROOT>/tests/logger.test.ts:68:18
error this is an error message
at <ROOT>/tests/logger.test.ts:81:18
at file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:146:14
at file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:61:7
at runTest (file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:960:17)
at runSuite (file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:1116:15)
at runSuite (file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:1116:15)
at runFiles (file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:1173:5)
at startTests (file://<ROOT>/node_modules/.pnpm/@[email protected]/node_modules/@vitest/runner/dist/index.js:1182:3)
at file://<ROOT>/node_modules/.pnpm/[email protected]_@[email protected][email protected].0/node_modules/vitest/dist/chunks/runBaseTests.CyvqmuC9.js:130:11
at withEnv (file://<ROOT>/node_modules/.pnpm/[email protected]_@[email protected][email protected].0/node_modules/vitest/dist/chunks/runBaseTests.CyvqmuC9.js:94:5)[39m
at file://<ROOT>/node_modules/.pnpm/[email protected]_@[email protected][email protected].[email protected]/node_modules/vitest/dist/chunks/runBaseTests.CyvqmuC9.js:130:11
at withEnv (file://<ROOT>/node_modules/.pnpm/[email protected]_@[email protected][email protected].[email protected]/node_modules/vitest/dist/chunks/runBaseTests.CyvqmuC9.js:94:5)
`;
21 changes: 17 additions & 4 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createLogger, logger } from '../src';
import { join } from 'path';
import { expect, test, describe, vi, Mock } from 'vitest';
import stripAnsi from 'strip-ansi';

const root = join(__dirname, '..');

expect.addSnapshotSerializer({
test: val => typeof val === 'string' && val.includes(root),
print: val => {
return (val as any).toString().replaceAll(root, '<ROOT>');
return stripAnsi((val as any).toString().replaceAll(root, '<ROOT>'));
},
});

Expand All @@ -23,7 +24,11 @@ describe('logger', () => {
logger.debug('this is a debug message');
logger.success('this is a success message');

expect((console.log as Mock).mock.calls).toMatchSnapshot();
expect(
(console.log as Mock).mock.calls.map(items =>
items.map(item => stripAnsi(item.toString())),
),
).toMatchSnapshot();
});

test('should create new logger with info level correctly', () => {
Expand All @@ -41,7 +46,11 @@ describe('logger', () => {
logger.debug('this is a debug message');
logger.success('this is a success message');

expect((console.log as Mock).mock.calls).toMatchSnapshot();
expect(
(console.log as Mock).mock.calls.map(items =>
items.map(item => stripAnsi(item.toString())),
),
).toMatchSnapshot();
});

test('should create new logger with warn level correctly', () => {
Expand All @@ -59,7 +68,11 @@ describe('logger', () => {
logger.debug('this is a debug message');
logger.success('this is a success message');

expect((console.log as Mock).mock.calls).toMatchSnapshot();
expect(
(console.log as Mock).mock.calls.map(items =>
items.map(item => stripAnsi(item.toString())),
),
).toMatchSnapshot();
});

test('should log error with stack correctly', () => {
Expand Down

0 comments on commit 1d6f5ee

Please sign in to comment.