From 56243060918a2186af58b2d2fe1e3bce964c17f8 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Fri, 17 Sep 2021 18:15:59 -0700 Subject: [PATCH 1/2] Replace bunyan with more stable, zero-dependency logger. --- package.json | 2 +- src/log.js | 8 ++----- src/log.test.js | 62 +++++++++++-------------------------------------- 3 files changed, 17 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index ddb6a0e..dfe6dfb 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "Jonas Holtkamp " ], "dependencies": { - "bunyan": "^1.8.12", + "diary": "^0.2.2", "expect": "^24.8.0" }, "devDependencies": { diff --git a/src/log.js b/src/log.js index 4e8cb1d..81dbda6 100644 --- a/src/log.js +++ b/src/log.js @@ -1,14 +1,10 @@ -const bunyan = require('bunyan') +const logger = require('diary') const packagejson = require('../package.json') const assert = require('assert') const newLogger = name => { assert(name, 'Name must be defined') - return bunyan.createLogger({ - name: name, - app: packagejson.name, - src: process.env.NODE_ENV === 'development' - }) + return logger.diary(`${packagejson.name}: ${name}`) } module.exports = newLogger diff --git a/src/log.test.js b/src/log.test.js index 68b7b7a..9fc03d3 100644 --- a/src/log.test.js +++ b/src/log.test.js @@ -1,6 +1,7 @@ const log = require('./log') -const packagejson = require('../package.json') -const { objectContaining } = expect +const { enable } = require('diary') + +enable('*') describe('the logger', () => { it('should create new loggers', () => { @@ -9,59 +10,24 @@ describe('the logger', () => { expect(logger.info).toBeDefined() }) - it('should use the package json name as the logger app', () => { - const logger = log('name') - - expect(logger.fields.app).toBe(packagejson.name) - }) - - it('should use the name as the loggers name', () => { - const name = 'some name' - - const logger = log(name) - - expect(logger.fields.name).toBe(name) - }) - - it('should fail if no name is passed', () => { - expect(() => log()).toThrow() - }) - - describe('should set "src" to the correct value', () => { - let mockCreateLogger, log + describe('when capture info output', () => { + const originalInfo = console.info beforeEach(() => { - mockCreateLogger = jest.fn() - jest.mock('bunyan', () => ({ - createLogger: mockCreateLogger - })) - - log = require('./log') + console.info = jest.fn() }) - afterEach(() => { - jest.resetModules() - jest.resetAllMocks() + console.info = originalInfo }) - it('should set "src" to true if the stage is development', () => { - const originalValue = process.env.NODE_ENV - process.env.NODE_ENV = 'development' - - log('name') - - process.env.NODE_ENV = originalValue - expect(mockCreateLogger).toBeCalledWith(objectContaining({ src: true })) + it('should use the package json name and logger name in the scope', () => { + const logger = log('name') + logger.info('hello world') + expect(console.info).toHaveBeenCalledWith('ℹ info [jest-when: name] hello world') }) + }) - it('should set "src" to false if the stage is NOT development', () => { - const originalValue = process.env.NODE_ENV - process.env.NODE_ENV = 'some other stage' - - log('name') - - process.env.NODE_ENV = originalValue - expect(mockCreateLogger).toBeCalledWith(objectContaining({ src: false })) - }) + it('should fail if no name is passed', () => { + expect(() => log()).toThrow() }) }) From fb72636ed4e46833aff3a6f94bcd6826a20b6526 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Sat, 18 Sep 2021 10:28:30 -0700 Subject: [PATCH 2/2] Fully remove logger. --- package.json | 1 - src/log.js | 10 ---------- src/log.test.js | 33 --------------------------------- src/when.js | 8 -------- src/when.test.js | 18 +----------------- 5 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 src/log.js delete mode 100644 src/log.test.js diff --git a/package.json b/package.json index dfe6dfb..06e2c08 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "Jonas Holtkamp " ], "dependencies": { - "diary": "^0.2.2", "expect": "^24.8.0" }, "devDependencies": { diff --git a/src/log.js b/src/log.js deleted file mode 100644 index 81dbda6..0000000 --- a/src/log.js +++ /dev/null @@ -1,10 +0,0 @@ -const logger = require('diary') -const packagejson = require('../package.json') -const assert = require('assert') - -const newLogger = name => { - assert(name, 'Name must be defined') - return logger.diary(`${packagejson.name}: ${name}`) -} - -module.exports = newLogger diff --git a/src/log.test.js b/src/log.test.js deleted file mode 100644 index 9fc03d3..0000000 --- a/src/log.test.js +++ /dev/null @@ -1,33 +0,0 @@ -const log = require('./log') -const { enable } = require('diary') - -enable('*') - -describe('the logger', () => { - it('should create new loggers', () => { - const logger = log('name') - - expect(logger.info).toBeDefined() - }) - - describe('when capture info output', () => { - const originalInfo = console.info - - beforeEach(() => { - console.info = jest.fn() - }) - afterEach(() => { - console.info = originalInfo - }) - - it('should use the package json name and logger name in the scope', () => { - const logger = log('name') - logger.info('hello world') - expect(console.info).toHaveBeenCalledWith('ℹ info [jest-when: name] hello world') - }) - }) - - it('should fail if no name is passed', () => { - expect(() => log()).toThrow() - }) -}) diff --git a/src/when.js b/src/when.js index 58435e9..39f1742 100644 --- a/src/when.js +++ b/src/when.js @@ -1,14 +1,11 @@ const assert = require('assert') const utils = require('expect/build/jasmineUtils') -const logger = require('./log')('when') let registry = new Set() const getCallLine = () => (new Error()).stack.split('\n')[4] const checkArgumentMatchers = (expectCall, args) => (match, matcher, i) => { - logger.debug(`matcher check, match: ${match}, index: ${i}`) - // Propagate failure to the end if (!match) { return false @@ -16,9 +13,6 @@ const checkArgumentMatchers = (expectCall, args) => (match, matcher, i) => { const arg = args[i] - logger.debug(` matcher: ${String(matcher)}`) - logger.debug(` arg: ${String(arg)}`) - const isFunctionMatcher = typeof matcher === 'function' && matcher._isFunctionMatcher // Assert the match for better messaging during a failure @@ -72,8 +66,6 @@ class WhenMock { this.nextCallMockId++ this.fn.mockImplementation((...args) => { - logger.debug('mocked impl', args) - for (let i = 0; i < this.callMocks.length; i++) { const { matchers, mockImplementation, expectCall, once, called } = this.callMocks[i] diff --git a/src/when.test.js b/src/when.test.js index 64b99ae..506be97 100644 --- a/src/when.test.js +++ b/src/when.test.js @@ -1,25 +1,12 @@ -const { stringContaining } = expect - const errMsg = ({ expect, actual }) => new RegExp(`Expected.*${expect}.*\\nReceived.*${actual}`) describe('When', () => { - let spyEquals, when, WhenMock, mockLogger, resetAllWhenMocks, verifyAllWhenMocksCalled + let spyEquals, when, WhenMock, resetAllWhenMocks, verifyAllWhenMocksCalled beforeEach(() => { spyEquals = jest.spyOn(require('expect/build/jasmineUtils'), 'equals') - mockLogger = { - info: jest.fn(), - debug: jest.fn(), - warn: jest.fn(), - error: jest.fn(), - fatal: jest.fn(), - trace: jest.fn() - } - - jest.mock('./log', () => () => mockLogger) - when = require('./when').when resetAllWhenMocks = require('./when').resetAllWhenMocks verifyAllWhenMocksCalled = require('./when').verifyAllWhenMocksCalled @@ -428,7 +415,6 @@ describe('When', () => { expect(fn(5)).toBeUndefined() expect(fn(symbol, 2)).toBe('x') - expect(mockLogger.debug).toBeCalledWith(stringContaining('matcher: Symbol(sym)')) }) it('returns nothing if no declared value matches', () => { @@ -437,8 +423,6 @@ describe('When', () => { when(fn).calledWith(1, 2).mockReturnValue('x') expect(fn(5, 6)).toBeUndefined() - expect(mockLogger.debug).toBeCalledWith(stringContaining('matcher: 1')) - expect(mockLogger.debug).not.toBeCalledWith(stringContaining('matcher: 2')) }) it('expectCalledWith: fails a test with error messaging if argument does not match', () => {