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 { 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!/,
),

View file

@ -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')