Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix sdk-utils test helper log capture #920

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/sdk-utils/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const LOG_LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
// Create a small logger util using the specified namespace
export function logger(namespace) {
return Object.keys(LOG_LEVELS).reduce((ns, lvl) => (
Object.assign(ns, { [lvl]: log.bind(null, namespace, lvl) })
Object.assign(ns, { [lvl]: (...a) => logger.log(namespace, lvl, ...a) })
), {});
}

Expand Down Expand Up @@ -90,8 +90,8 @@ const remote = logger.remote = async timeout => {
if (log.history) ws.send(JSON.stringify({ messages: log.history }));
} catch (err) {
// there was an error connecting, will fallback to minimal logging
log('utils', 'debug', 'Unable to connect to remote logger');
log('utils', 'debug', err);
logger.log('utils', 'debug', 'Unable to connect to remote logger');
logger.log('utils', 'debug', err);
}
};

Expand Down
29 changes: 17 additions & 12 deletions packages/sdk-utils/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const helpers = {
},

async teardown() {
utils.logger.log.restore?.();

if (process.env.__PERCY_BROWSERIFIED__) {
for (let m of ['warn', 'error', 'log']) console[m].restore?.();
} else {
Expand Down Expand Up @@ -68,21 +70,23 @@ const helpers = {
async mock() {
helpers.logger.reset();

let capture = err => msg => {
helpers.logger[err ? 'stderr' : 'stdout']
.push(sanitizeLog(msg));
};
let shouldCaptureLogs = false;

stub(utils.logger, 'log', (...args) => {
shouldCaptureLogs = true;
utils.logger.log.originalValue(...args);
shouldCaptureLogs = false;
});

let stubLogs = (ctx, method, err) => stub(ctx, method, msg => {
if (!shouldCaptureLogs) return ctx[method].originalValue.call(ctx, msg);
else helpers.logger[err ? 'stderr' : 'stdout'].push(sanitizeLog(msg));
});

if (process.env.__PERCY_BROWSERIFIED__) {
// use console[warn|error|log] in browsers
for (let m of ['warn', 'error', 'log']) {
stub(console, m, capture(m !== 'log'));
}
for (let m of ['warn', 'error', 'log']) stubLogs(console, m, m !== 'log');
} else {
// use process[stdout|stderr].write in node
for (let io of ['stdout', 'stderr']) {
stub(process[io], 'write', capture(io === 'stderr'));
}
for (let io of ['stdout', 'stderr']) stubLogs(process[io], 'write', io === 'stderr');
}
},

Expand All @@ -93,6 +97,7 @@ const helpers = {

helpers.logger.stdout.length = 0;
helpers.logger.stderr.length = 0;
utils.logger.log.reset?.();

if (process.env.__PERCY_BROWSERIFIED__) {
for (let m of ['warn', 'error', 'log']) console[m].reset?.();
Expand Down