refactor: logger

This commit is contained in:
Marc Cataford 2021-03-19 16:38:47 -04:00
parent 4eb2e87fd7
commit 2d9a0eec16
2 changed files with 17 additions and 11 deletions

View file

@ -2,6 +2,7 @@ import { promises as fs } from 'fs'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import logger from '../logger'
import type { Report } from '../index.d' import type { Report } from '../index.d'
import packwatch from '..' import packwatch from '..'
@ -37,9 +38,13 @@ async function createManifest(
} }
describe('Packwatch', () => { describe('Packwatch', () => {
let mockLogger let mockLogger
let mockWarn
let mockError
let workspacePath let workspacePath
beforeEach(() => { 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 () => { afterEach(async () => {
@ -92,13 +97,14 @@ describe('Packwatch', () => {
packwatch({ cwd: workspacePath }), packwatch({ cwd: workspacePath }),
).rejects.toThrow() ).rejects.toThrow()
expect(mockLogger.mock.calls).toHaveLength(2) expect(mockWarn.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockWarn.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(
/No Manifest to compare against! Current package stats written to \.packwatch\.json!\nPackage size \(\d+ B\) adopted as new limit\./, /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( 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!', '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 }) await packwatch({ cwd: workspacePath, isUpdatingManifest: true })
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockWarn.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockWarn.mock.calls[0][0]).toEqual(
expect.stringMatching( expect.stringMatching(
/No Manifest to compare against! Current package stats written to \.packwatch\.json!\nPackage size \(\d+ B\) adopted as new limit\./, /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 () => await expect(async () =>
packwatch({ cwd: workspacePath }), packwatch({ cwd: workspacePath }),
).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT') ).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT')
expect(mockLogger.mock.calls).toHaveLength(1) expect(mockError.mock.calls).toHaveLength(1)
expect(mockLogger.mock.calls[0][0]).toEqual( expect(mockError.mock.calls[0][0]).toEqual(
expect.stringMatching( 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!/, /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!/,
), ),

View file

@ -29,12 +29,12 @@ export default async function packwatch({
if (!existsSync(manifestPath)) { if (!existsSync(manifestPath)) {
createOrUpdateManifest({ manifestPath, current: currentStats }) 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.`, `📝 No Manifest to compare against! Current package stats written to ${MANIFEST_FILENAME}!\nPackage size (${currentStats.packageSize}) adopted as new limit.`,
) )
if (!isUpdatingManifest) { 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!', '❗ 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') throw new Error('NO_MANIFEST_NO_UPDATE')
@ -75,7 +75,7 @@ export default async function packwatch({
*/ */
if (hasExceededLimit) { 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!`, `🔥🔥📦🔥🔥 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') throw new Error('PACKAGE_EXCEEDS_LIMIT')