From 2d9a0eec160aed868087fa047908cee31db47c37 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 19 Mar 2021 16:38:47 -0400 Subject: [PATCH] refactor: logger --- src/__tests__/index.test.ts | 22 ++++++++++++++-------- src/index.ts | 6 +++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 87e08d0..8ace7d3 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -2,6 +2,7 @@ import { promises as fs } from 'fs' import { tmpdir } from 'os' import { join, resolve } from 'path' +import logger from '../logger' import type { Report } from '../index.d' import packwatch from '..' @@ -37,9 +38,13 @@ async function createManifest( } describe('Packwatch', () => { let mockLogger + let mockWarn + let mockError let workspacePath beforeEach(() => { - mockLogger = jest.spyOn(global.console, 'log').mockImplementation() + mockLogger = jest.spyOn(logger, 'log').mockImplementation() + mockWarn = jest.spyOn(logger, 'warn').mockImplementation() + mockError = jest.spyOn(logger, 'error').mockImplementation() }) afterEach(async () => { @@ -92,13 +97,14 @@ describe('Packwatch', () => { packwatch({ cwd: workspacePath }), ).rejects.toThrow() - expect(mockLogger.mock.calls).toHaveLength(2) - expect(mockLogger.mock.calls[0][0]).toEqual( + expect(mockWarn.mock.calls).toHaveLength(1) + expect(mockWarn.mock.calls[0][0]).toEqual( expect.stringMatching( /No Manifest to compare against! Current package stats written to \.packwatch\.json!\nPackage size \(\d+ B\) adopted as new limit\./, ), ) - expect(mockLogger.mock.calls[1][0]).toEqual( + expect(mockError.mock.calls).toHaveLength(1) + expect(mockError.mock.calls[1][0]).toEqual( expect.stringMatching( 'It looks like you ran PackWatch without a manifest. To prevent accidental passes in CI or hooks, packwatch will terminate with an error. If you are running packwatch for the first time in your project, this is expected!', ), @@ -112,8 +118,8 @@ describe('Packwatch', () => { await packwatch({ cwd: workspacePath, isUpdatingManifest: true }) - expect(mockLogger.mock.calls).toHaveLength(1) - expect(mockLogger.mock.calls[0][0]).toEqual( + expect(mockWarn.mock.calls).toHaveLength(1) + expect(mockWarn.mock.calls[0][0]).toEqual( expect.stringMatching( /No Manifest to compare against! Current package stats written to \.packwatch\.json!\nPackage size \(\d+ B\) adopted as new limit\./, ), @@ -207,8 +213,8 @@ describe('Packwatch', () => { await expect(async () => packwatch({ cwd: workspacePath }), ).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT') - expect(mockLogger.mock.calls).toHaveLength(1) - expect(mockLogger.mock.calls[0][0]).toEqual( + expect(mockError.mock.calls).toHaveLength(1) + expect(mockError.mock.calls[0][0]).toEqual( expect.stringMatching( /Your package exceeds the limit set in \.packwatch\.json! \d+ B > 10B\nEither update the limit by using the --update-manifest flag or trim down your packed files!/, ), diff --git a/src/index.ts b/src/index.ts index 5e6c827..9adeaa7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,12 +29,12 @@ export default async function packwatch({ if (!existsSync(manifestPath)) { createOrUpdateManifest({ manifestPath, current: currentStats }) - logger.log( + logger.warn( `📝 No Manifest to compare against! Current package stats written to ${MANIFEST_FILENAME}!\nPackage size (${currentStats.packageSize}) adopted as new limit.`, ) if (!isUpdatingManifest) { - logger.log( + logger.error( '❗ It looks like you ran PackWatch without a manifest. To prevent accidental passes in CI or hooks, packwatch will terminate with an error. If you are running packwatch for the first time in your project, this is expected!', ) throw new Error('NO_MANIFEST_NO_UPDATE') @@ -75,7 +75,7 @@ export default async function packwatch({ */ if (hasExceededLimit) { - logger.log( + logger.error( `🔥🔥📦🔥🔥 Your package exceeds the limit set in ${MANIFEST_FILENAME}! ${packageSize} > ${limit}\nEither update the limit by using the --update-manifest flag or trim down your packed files!`, ) throw new Error('PACKAGE_EXCEEDS_LIMIT')