diff --git a/packages/logger/test/helpers.js b/packages/logger/test/helpers.js index 257100568..466d192ec 100644 --- a/packages/logger/test/helpers.js +++ b/packages/logger/test/helpers.js @@ -30,6 +30,26 @@ function TestIO(data, options) { } } +function spy(object, method, func) { + if (object[method].reset) { + object[method].reset(); + return object[method]; + } + + let spy = Object.assign(function spy(...args) { + spy.calls.push(args); + if (func) return func.apply(this, args); + }, { + restore: () => (object[method] = spy.originalValue), + reset: () => (spy.calls.length = 0), + originalValue: object[method], + calls: [] + }); + + object[method] = spy; + return spy; +} + const helpers = { constructor: Logger, loglevel: logger.loglevel, @@ -49,21 +69,15 @@ const helpers = { Logger.stdout = TestIO(helpers.stdout, options); Logger.stderr = TestIO(helpers.stderr, options); } else { - let write = Logger.prototype.write; - - if (!write.and) { - spyOn(Logger.prototype, 'write').and.callFake(function(lvl, msg) { - let stdio = lvl === 'info' ? 'stdout' : 'stderr'; - helpers[stdio].push(sanitizeLog(msg, helpers.options)); - return write.call(this, lvl, msg); - }); - } - - if (!console.log.and) { - spyOn(console, 'log'); - spyOn(console, 'warn'); - spyOn(console, 'error'); - } + spy(Logger.prototype, 'write', function(lvl, msg) { + let stdio = lvl === 'info' ? 'stdout' : 'stderr'; + helpers[stdio].push(sanitizeLog(msg, helpers.options)); + return this.write.originalValue.call(this, lvl, msg); + }); + + spy(console, 'log'); + spy(console, 'warn'); + spy(console, 'error'); } }, @@ -73,10 +87,10 @@ const helpers = { helpers.stdout.length = 0; helpers.stderr.length = 0; - if (console.log.and) { - console.log.calls.reset(); - console.warn.calls.reset(); - console.error.calls.reset(); + if (console.log.reset) { + console.log.reset(); + console.warn.reset(); + console.error.reset(); } }, diff --git a/packages/logger/test/index.test.js b/packages/logger/test/index.test.js index a081086d7..66551a311 100644 --- a/packages/logger/test/index.test.js +++ b/packages/logger/test/index.test.js @@ -319,22 +319,25 @@ describe('logger', () => { it('logs messages with CSS colors', () => { log.info('Colorful!'); - expect(console.log).toHaveBeenCalledOnceWith( - '[%cpercy%c] Colorful!', 'color:magenta', 'color:inherit'); + expect(console.log.calls).toEqual([ + ['[%cpercy%c] Colorful!', 'color:magenta', 'color:inherit'] + ]); }); it('logs errors with console.error', () => { log.error('ERR!'); - expect(console.error).toHaveBeenCalledOnceWith( - '[%cpercy%c] %cERR!%c', 'color:magenta', 'color:inherit', 'color:red', 'color:inherit'); + expect(console.error.calls).toEqual([ + ['[%cpercy%c] %cERR!%c', 'color:magenta', 'color:inherit', 'color:red', 'color:inherit'] + ]); }); it('logs warnings with console.warn', () => { log.warn('Warning!'); - expect(console.warn).toHaveBeenCalledOnceWith( - '[%cpercy%c] %cWarning!%c', 'color:magenta', 'color:inherit', 'color:yellow', 'color:inherit'); + expect(console.warn.calls).toEqual([ + ['[%cpercy%c] %cWarning!%c', 'color:magenta', 'color:inherit', 'color:yellow', 'color:inherit'] + ]); }); }); }