Skip to content

Commit

Permalink
✅ Make @percy/logger test helper framework agnostic (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Wilsman authored Mar 25, 2021
1 parent 04d3c7d commit dc9cbe5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
52 changes: 33 additions & 19 deletions packages/logger/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
},

Expand All @@ -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();
}
},

Expand Down
15 changes: 9 additions & 6 deletions packages/logger/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
]);
});
});
}
Expand Down

0 comments on commit dc9cbe5

Please sign in to comment.