Skip to content

Commit

Permalink
test: if no log level set, only log failed tests
Browse files Browse the repository at this point in the history
also sets default logLevel to 4
  • Loading branch information
owenpearson committed Feb 29, 2024
1 parent 418c800 commit e8180c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
45 changes: 38 additions & 7 deletions test/common/globals/environment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Assumes process.env defined, or window.__env__ or populated via globals.env.js and karam-env-preprocessor plugin */

define(function (require) {
var defaultLogLevel = 2,
var defaultLogLevel = 4,
environment = isBrowser ? window.__env__ || {} : process.env,
ablyEnvironment = environment.ABLY_ENV || 'sandbox',
realtimeHost = environment.ABLY_REALTIME_HOST,
Expand All @@ -11,6 +11,8 @@ define(function (require) {
tls = 'ABLY_USE_TLS' in environment ? environment.ABLY_USE_TLS.toLowerCase() !== 'false' : true,
logLevel = environment.ABLY_LOG_LEVEL || defaultLogLevel;

let logLevelSet = environment.ABLY_LOG_LEVEL !== undefined;

if (isBrowser) {
var url = window.location.href,
keysValues = url.split(/[\?&]+/),
Expand All @@ -27,13 +29,39 @@ define(function (require) {
if (query['port']) port = query['port'];
if (query['tls_port']) tlsPort = query['tls_port'];
if (query['tls']) tls = query['tls'].toLowerCase() !== 'false';
if (query['log_level']) logLevel = Number(query['log_level']) || defaultLogLevel;
if (query['log_level']) {
logLevel = Number(query['log_level']);
logLevelSet = true;
}
} else if (process) {
process.on('uncaughtException', function (err) {
console.error(err.stack);
});
}

function getLogTimestamp() {
const time = new Date();
return (
time.getHours().toString().padStart(2, '0') +
':' +
time.getMinutes().toString().padStart(2, '0') +
':' +
time.getSeconds().toString().padStart(2, '0') +
'.' +
time.getMilliseconds().toString().padStart(3, '0')
);
}

let clientLogs = [];

function getLogs() {
return clientLogs;
}

function flushLogs() {
clientLogs = [];
}

return (module.exports = {
environment: ablyEnvironment,
realtimeHost: realtimeHost,
Expand All @@ -42,12 +70,15 @@ define(function (require) {
tlsPort: tlsPort,
tls: tls,
logLevel: logLevel,
getLogs,
flushLogs,

logHandler: function (msg) {
var time = new Date();
console.log(
time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds() + '.' + time.getMilliseconds(),
msg
);
if (logLevelSet) {
console.log(getLogTimestamp(), msg);
} else {
clientLogs.push([getLogTimestamp(), msg]);
}
},
});
});
16 changes: 15 additions & 1 deletion test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ define([
'test/common/modules/testapp_module',
'test/common/modules/client_module',
'test/common/modules/testapp_manager',
'globals',
'async',
'chai',
], function (testAppModule, clientModule, testAppManager, async, chai) {
], function (testAppModule, clientModule, testAppManager, globals, async, chai) {
var utils = clientModule.Ably.Realtime.Utils;
var platform = clientModule.Ably.Realtime.Platform;
var BufferUtils = platform.BufferUtils;
Expand Down Expand Up @@ -221,6 +222,19 @@ define([
expect(json1 === json2, 'JSON data contents mismatch.').to.be.ok;
}

afterEach(function () {
if (this.currentTest.isFailed()) {
console.group();
console.log('Logs for failing test: \n');
globals.getLogs().forEach(([timestamp, log]) => {
console.log(timestamp, log);
});
console.log();
console.groupEnd();
}
globals.flushLogs();
});

return (module.exports = {
setupApp: testAppModule.setup,
tearDownApp: testAppModule.tearDown,
Expand Down

0 comments on commit e8180c9

Please sign in to comment.