Skip to content

Commit

Permalink
Aa/chore/adjust logger tests (#3193)
Browse files Browse the repository at this point in the history
* Keeping console clean during tests

* Comparing strings without ANSI color codes

* Adding changeset
  • Loading branch information
arboleya authored Sep 21, 2024
1 parent 8930417 commit ef50619
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changeset/fair-walls-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: adjust logger tests
17 changes: 12 additions & 5 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ describe('Logger Tests', () => {

beforeEach(() => {
debug.enable('test');
debugSpy = vi.spyOn(debug, 'log');
debugSpy = vi.spyOn(debug, 'log').mockImplementation(() => ({}));
});

afterEach(() => {
debugSpy.mockRestore();
});

// Removes ANSI color codes from strings
function clean(s: string) {
// eslint-disable-next-line no-control-regex
const reg = /\u001b[^m]*?m/g;
return s.replace(reg, '');
}

it('should log info messages correctly', () => {
const log = logger('test');
const message = 'This is a message';
log(message);
const callArgs = debugSpy.mock.calls[0][0];
expect(callArgs).toContain(`test ${message}`);
expect(clean(callArgs)).toContain(`test ${message}`);
});

it('should format a b256 string correctly', () => {
Expand All @@ -39,7 +46,7 @@ describe('Logger Tests', () => {
log(formattedMessage, mockAddress);

const callArgs = debugSpy.mock.calls[0][0];
expect(callArgs).toContain('0x123456789abcdef');
expect(clean(callArgs)).toContain('0x123456789abcdef');
});

it('should prefix log messages correctly using prefixLogger', () => {
Expand All @@ -53,7 +60,7 @@ describe('Logger Tests', () => {

log(message);
const callArgs = debugSpy.mock.calls[0][0];
expect(callArgs).toContain(`${prefix}:${component} ${message}`);
expect(clean(callArgs)).toContain(`${prefix}:${component} ${message}`);
});

it('should create a default logger and log messages correctly', () => {
Expand All @@ -65,7 +72,7 @@ describe('Logger Tests', () => {
log(message);

const callArgs = debugSpy.mock.calls[0][0];
expect(callArgs).toContain(`test-component ${message}`);
expect(clean(callArgs)).toContain(`test-component ${message}`);
});

it('should format BN values with commas correctly using formatter a', () => {
Expand Down

0 comments on commit ef50619

Please sign in to comment.