From 23b233afdfb95d39a5c83599966bb6e6176f6260 Mon Sep 17 00:00:00 2001 From: Christoffer Artmann Date: Wed, 4 Sep 2024 19:45:43 +0200 Subject: [PATCH] fix: Mock chalk in tasks. --- .github/workflows/coverage.yml | 2 -- .github/workflows/main.yml | 2 -- src/logger.test.ts | 33 ++++++++++++++++++++++----------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index c9060c9..36e5a9c 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -1,7 +1,5 @@ name: Coverage on: [push] -env: - FORCE_COLOR: 2 jobs: build: name: Test Coverage diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b591fa7..4ba27f9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,7 +1,5 @@ name: CI on: [push] -env: - FORCE_COLOR: 2 jobs: build: name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }} diff --git a/src/logger.test.ts b/src/logger.test.ts index 61dd6a0..2994609 100644 --- a/src/logger.test.ts +++ b/src/logger.test.ts @@ -1,7 +1,14 @@ -import { beforeEach, describe, expect, test, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' -import { Colors, Logger } from './logger' -import chalk from 'chalk' +import { Logger } from './logger' + +vi.mock('chalk', async () => ({ + default: { + hex: () => { + return (text: string) => text + } + } +})) describe('Logger', () => { beforeEach(() => { @@ -9,6 +16,10 @@ describe('Logger', () => { vi.setSystemTime(new Date(2024, 8, 31)) }) + afterEach(() => { + vi.useRealTimers() + }) + test('properly logs trace messages to the console.', () => { const spy = vi.spyOn(console, 'log') @@ -16,7 +27,7 @@ describe('Logger', () => { logger.trace('Entering processOrder function with orderId=12345.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('TRACE')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Entering processOrder function with orderId=12345.` + const expectedMessage = `[TRACE] 2024-09-30T22:00:00.000Z Entering processOrder function with orderId=12345.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -28,7 +39,7 @@ describe('Logger', () => { logger.debug('Processing order with orderId=12345.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('DEBUG')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Processing order with orderId=12345.` + const expectedMessage = `[DEBUG] 2024-09-30T22:00:00.000Z Processing order with orderId=12345.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -40,7 +51,7 @@ describe('Logger', () => { logger.info('Order processed successfully.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('INFO')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Order processed successfully.` + const expectedMessage = `[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -52,7 +63,7 @@ describe('Logger', () => { logger.warn('Order with orderId=12345 is missing a delivery address.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('WARN')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Order with orderId=12345 is missing a delivery address.` + const expectedMessage = `[WARN] 2024-09-30T22:00:00.000Z Order with orderId=12345 is missing a delivery address.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -64,7 +75,7 @@ describe('Logger', () => { logger.error('Failed to process order with orderId=12345.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('ERROR')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Failed to process order with orderId=12345.` + const expectedMessage = `[ERROR] 2024-09-30T22:00:00.000Z Failed to process order with orderId=12345.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -76,7 +87,7 @@ describe('Logger', () => { logger.fatal('Failed to start the application.') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('FATAL')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Failed to start the application.` + const expectedMessage = `[FATAL] 2024-09-30T22:00:00.000Z Failed to start the application.` expect(spy).toHaveBeenCalledWith(expectedMessage) }) @@ -98,7 +109,7 @@ describe('Logger', () => { logger.info('Order processed successfully.', 'orderId=12345') - const expectedMessage = `[${chalk.hex(Colors.LightGray)('INFO')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} Order processed successfully.` + const expectedMessage = `[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.` expect(spy).toHaveBeenCalledWith(expectedMessage, 'orderId=12345') }) @@ -111,7 +122,7 @@ describe('Logger', () => { logger.info({ orderId: 12345 }, 'Order processed successfully.') expect(spy).toHaveBeenCalledWith( - `[${chalk.hex(Colors.LightGray)('INFO')}] ${chalk.hex(Colors.LightGray)('2024-09-30T22:00:00.000Z')} `, + `[INFO] 2024-09-30T22:00:00.000Z `, { orderId: 12345 }, 'Order processed successfully.' )