Skip to content

Commit

Permalink
fix: Mock chalk in tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artmann committed Sep 4, 2024
1 parent 625c000 commit 23b233a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
name: Coverage
on: [push]
env:
FORCE_COLOR: 2
jobs:
build:
name: Test Coverage
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Expand Down
33 changes: 22 additions & 11 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
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(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date(2024, 8, 31))
})

afterEach(() => {
vi.useRealTimers()
})

test('properly logs trace messages to the console.', () => {
const spy = vi.spyOn(console, 'log')

const logger = new 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)

Check failure on line 32 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs trace messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[TRACE] 2024-09-30T22:00:00.000Z Entering processOrder function with orderId=12345.", + "[TRACE] 2024-10-01T00:00:00.000Z Entering processOrder function with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:32:17

Check failure on line 32 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs trace messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[TRACE] 2024-09-30T22:00:00.000Z Entering processOrder function with orderId=12345.", + "[TRACE] 2024-10-01T00:00:00.000Z Entering processOrder function with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:32:17
})
Expand All @@ -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)

Check failure on line 44 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs debug messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[DEBUG] 2024-09-30T22:00:00.000Z Processing order with orderId=12345.", + "[DEBUG] 2024-10-01T00:00:00.000Z Processing order with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:44:17

Check failure on line 44 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs debug messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[DEBUG] 2024-09-30T22:00:00.000Z Processing order with orderId=12345.", + "[DEBUG] 2024-10-01T00:00:00.000Z Processing order with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:44:17
})
Expand All @@ -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)

Check failure on line 56 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs info messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.", + "[INFO] 2024-10-01T00:00:00.000Z Order processed successfully.", ] Number of calls: 1 ❯ src/logger.test.ts:56:17

Check failure on line 56 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs info messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.", + "[INFO] 2024-10-01T00:00:00.000Z Order processed successfully.", ] Number of calls: 1 ❯ src/logger.test.ts:56:17
})
Expand All @@ -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)

Check failure on line 68 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs warn messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[WARN] 2024-09-30T22:00:00.000Z Order with orderId=12345 is missing a delivery address.", + "[WARN] 2024-10-01T00:00:00.000Z Order with orderId=12345 is missing a delivery address.", ] Number of calls: 1 ❯ src/logger.test.ts:68:17

Check failure on line 68 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs warn messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[WARN] 2024-09-30T22:00:00.000Z Order with orderId=12345 is missing a delivery address.", + "[WARN] 2024-10-01T00:00:00.000Z Order with orderId=12345 is missing a delivery address.", ] Number of calls: 1 ❯ src/logger.test.ts:68:17
})
Expand All @@ -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)

Check failure on line 80 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs error messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[ERROR] 2024-09-30T22:00:00.000Z Failed to process order with orderId=12345.", + "[ERROR] 2024-10-01T00:00:00.000Z Failed to process order with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:80:17

Check failure on line 80 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs error messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[ERROR] 2024-09-30T22:00:00.000Z Failed to process order with orderId=12345.", + "[ERROR] 2024-10-01T00:00:00.000Z Failed to process order with orderId=12345.", ] Number of calls: 1 ❯ src/logger.test.ts:80:17
})
Expand All @@ -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)

Check failure on line 92 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > properly logs fatal messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[FATAL] 2024-09-30T22:00:00.000Z Failed to start the application.", + "[FATAL] 2024-10-01T00:00:00.000Z Failed to start the application.", ] Number of calls: 1 ❯ src/logger.test.ts:92:17

Check failure on line 92 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > properly logs fatal messages to the console.

AssertionError: expected "log" to be called with arguments: [ Array(1) ] Received: 1st log call: Array [ - "[FATAL] 2024-09-30T22:00:00.000Z Failed to start the application.", + "[FATAL] 2024-10-01T00:00:00.000Z Failed to start the application.", ] Number of calls: 1 ❯ src/logger.test.ts:92:17
})
Expand All @@ -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')

Check failure on line 114 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > logs messages with multiple arguments.

AssertionError: expected "log" to be called with arguments: [ …(2) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.", + "[INFO] 2024-10-01T00:00:00.000Z Order processed successfully.", "orderId=12345", ] Number of calls: 1 ❯ src/logger.test.ts:114:17

Check failure on line 114 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > logs messages with multiple arguments.

AssertionError: expected "log" to be called with arguments: [ …(2) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z Order processed successfully.", + "[INFO] 2024-10-01T00:00:00.000Z Order processed successfully.", "orderId=12345", ] Number of calls: 1 ❯ src/logger.test.ts:114:17
})
Expand All @@ -111,7 +122,7 @@ describe('Logger', () => {
logger.info({ orderId: 12345 }, 'Order processed successfully.')

expect(spy).toHaveBeenCalledWith(

Check failure on line 124 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Test Coverage

src/logger.test.ts > Logger > logs messages with a message object.

AssertionError: expected "log" to be called with arguments: [ …(3) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z ", + "[INFO] 2024-10-01T00:00:00.000Z ", Object { "orderId": 12345, }, "Order processed successfully.", ] Number of calls: 1 ❯ src/logger.test.ts:124:17

Check failure on line 124 in src/logger.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

src/logger.test.ts > Logger > logs messages with a message object.

AssertionError: expected "log" to be called with arguments: [ …(3) ] Received: 1st log call: Array [ - "[INFO] 2024-09-30T22:00:00.000Z ", + "[INFO] 2024-10-01T00:00:00.000Z ", Object { "orderId": 12345, }, "Order processed successfully.", ] Number of calls: 1 ❯ src/logger.test.ts:124:17
`[${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.'
)
Expand Down

0 comments on commit 23b233a

Please sign in to comment.