Skip to content

Commit

Permalink
fix(test): ci environment doesn't handle replacing console with a mock
Browse files Browse the repository at this point in the history
  • Loading branch information
MSNev committed Jan 28, 2021
1 parent 9f2643a commit 08cac50
Showing 1 changed file with 80 additions and 60 deletions.
140 changes: 80 additions & 60 deletions packages/opentelemetry-api/test/diag/consoleLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ describe('DiagConsoleLogger', () => {
let errorCalledArgs: unknown;
let logCalledArgs: unknown;

let canMockConsole = true;

try {
// eslint-disable-next-line no-global-assign
console = origConsole;
} catch (ex) {
// Not supported on CI pipeline (works locally)
canMockConsole = false;
}

beforeEach(() => {
// mock
console.debug = (...args: unknown[]) => {
Expand Down Expand Up @@ -62,8 +72,16 @@ describe('DiagConsoleLogger', () => {
errorCalledArgs = null;
logCalledArgs = null;
traceCalledArgs = null;
// eslint-disable-next-line no-global-assign
console = origConsole;

if (canMockConsole) {
try {
// eslint-disable-next-line no-global-assign
console = origConsole;
} catch (ex) {
// Not supported on CI pipeline
canMockConsole = false;
}
}
console.debug = origDebug;
console.info = origInfo;
console.warn = origWarn;
Expand Down Expand Up @@ -265,66 +283,68 @@ describe('DiagConsoleLogger', () => {
assert.deepStrictEqual(infoCalledArgs, null);
});

it('should not throw even when console is invalid after construction', () => {
const invalidConsole = {
debug: 1,
warn: 2,
error: 3,
trace: 4,
info: 5,
log: 6,
};
if (canMockConsole) {
it('should not throw even when console is invalid after construction', () => {
const invalidConsole = {
debug: 1,
warn: 2,
error: 3,
trace: 4,
info: 5,
log: 6,
};

const consoleLogger = new DiagConsoleLogger();
(console as any) = invalidConsole;
consoleLogger.terminal('terminal called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.critical('critical called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.error('error called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, null);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, null);
consoleLogger.trace('trace called %s', 'param1');
assert.deepStrictEqual(traceCalledArgs, null);
assert.deepStrictEqual(logCalledArgs, null);
consoleLogger.forcedInfo('forcedInfo called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
});
const consoleLogger = new DiagConsoleLogger();
(console as any) = invalidConsole;
consoleLogger.terminal('terminal called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.critical('critical called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.error('error called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, null);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, null);
consoleLogger.trace('trace called %s', 'param1');
assert.deepStrictEqual(traceCalledArgs, null);
assert.deepStrictEqual(logCalledArgs, null);
consoleLogger.forcedInfo('forcedInfo called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
});

it('should not throw even when console is invalid before construction', () => {
const invalidConsole = {
debug: 1,
warn: 2,
error: 3,
trace: 4,
info: 5,
log: 6,
};
it('should not throw even when console is invalid before construction', () => {
const invalidConsole = {
debug: 1,
warn: 2,
error: 3,
trace: 4,
info: 5,
log: 6,
};

(console as any) = invalidConsole;
const consoleLogger = new DiagConsoleLogger();
consoleLogger.terminal('terminal called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.critical('critical called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.error('error called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, null);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, null);
consoleLogger.trace('trace called %s', 'param1');
assert.deepStrictEqual(traceCalledArgs, null);
assert.deepStrictEqual(logCalledArgs, null);
consoleLogger.forcedInfo('forcedInfo called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
});
(console as any) = invalidConsole;
const consoleLogger = new DiagConsoleLogger();
consoleLogger.terminal('terminal called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.critical('critical called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.error('error called %s', 'param1');
assert.deepStrictEqual(errorCalledArgs, null);
consoleLogger.warn('warn called %s', 'param1');
assert.deepStrictEqual(warnCalledArgs, null);
consoleLogger.info('info called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
consoleLogger.debug('debug called %s', 'param1');
assert.deepStrictEqual(debugCalledArgs, null);
consoleLogger.trace('trace called %s', 'param1');
assert.deepStrictEqual(traceCalledArgs, null);
assert.deepStrictEqual(logCalledArgs, null);
consoleLogger.forcedInfo('forcedInfo called %s', 'param1');
assert.deepStrictEqual(infoCalledArgs, null);
});
}
});
});

0 comments on commit 08cac50

Please sign in to comment.