Skip to content

fix(tests): Silence dummy output in tests #4979

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

Merged
merged 6 commits into from
Apr 27, 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
111 changes: 68 additions & 43 deletions packages/integrations/test/captureconsole.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const mockHub = {
captureException: jest.fn(),
};

const mockConsole = {
debug: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
assert: jest.fn(),
info: jest.fn(),
};

const getMockHubWithIntegration = (integration: Integration) =>
({
...mockHub,
Expand All @@ -27,63 +36,79 @@ const getMockHubWithIntegration = (integration: Integration) =>
const originalConsole = Object.assign({}, global.console);

describe('CaptureConsole setup', () => {
beforeEach(() => {
// this suppresses output to the terminal running the tests, but doesn't interfere with our wrapping
Object.assign(global.console, mockConsole);
});

afterEach(() => {
jest.clearAllMocks();

// Un-monkey-patch the console functions
Object.assign(global.console, originalConsole);
});

it('should patch user-configured console levels', () => {
const captureConsoleIntegration = new CaptureConsole({ levels: ['log', 'warn'] });
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);
describe('monkeypatching', () => {
beforeEach(() => {
// for these tests only, we don't want to use the mock console, because we're testing for equality to methods from
// the original, so undo the global `beforeEach()`
Object.assign(global.console, originalConsole);
});

expect(global.console.error).toBe(originalConsole.error); // not monkey patched
expect(global.console.log).not.toBe(originalConsole.log); // monkey patched
expect(global.console.warn).not.toBe(originalConsole.warn); // monkey patched
});
it('should patch user-configured console levels', () => {
const captureConsoleIntegration = new CaptureConsole({ levels: ['log', 'warn'] });
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);

it('should fall back to default console levels if none are provided', () => {
const captureConsoleIntegration = new CaptureConsole();
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);
expect(global.console.error).toBe(originalConsole.error); // not monkey patched
expect(global.console.log).not.toBe(originalConsole.log); // monkey patched
expect(global.console.warn).not.toBe(originalConsole.warn); // monkey patched
});

// expect a set of defined console levels to have been monkey patched
expect(global.console.debug).not.toBe(originalConsole.debug);
expect(global.console.info).not.toBe(originalConsole.info);
expect(global.console.warn).not.toBe(originalConsole.warn);
expect(global.console.error).not.toBe(originalConsole.error);
expect(global.console.log).not.toBe(originalConsole.log);
expect(global.console.assert).not.toBe(originalConsole.assert);
it('should fall back to default console levels if none are provided', () => {
const captureConsoleIntegration = new CaptureConsole();
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);

// any other fields should not have been patched
expect(global.console.trace).toBe(originalConsole.trace);
expect(global.console.table).toBe(originalConsole.table);
});
// expect a set of defined console levels to have been monkey patched
expect(global.console.debug).not.toBe(originalConsole.debug);
expect(global.console.info).not.toBe(originalConsole.info);
expect(global.console.warn).not.toBe(originalConsole.warn);
expect(global.console.error).not.toBe(originalConsole.error);
expect(global.console.log).not.toBe(originalConsole.log);
expect(global.console.assert).not.toBe(originalConsole.assert);

// any other fields should not have been patched
expect(global.console.trace).toBe(originalConsole.trace);
expect(global.console.table).toBe(originalConsole.table);
});

it('should not wrap any functions with an empty levels option', () => {
const captureConsoleIntegration = new CaptureConsole({ levels: [] });
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);

it('should not wrap any functions with an empty levels option', () => {
const captureConsoleIntegration = new CaptureConsole({ levels: [] });
captureConsoleIntegration.setupOnce(
() => undefined,
() => getMockHubWithIntegration(captureConsoleIntegration),
);
// expect the default set of console levels not to have been monkey patched
expect(global.console.debug).toBe(originalConsole.debug);
expect(global.console.info).toBe(originalConsole.info);
expect(global.console.warn).toBe(originalConsole.warn);
expect(global.console.error).toBe(originalConsole.error);
expect(global.console.log).toBe(originalConsole.log);
expect(global.console.assert).toBe(originalConsole.assert);

// expect the default set of console levels not to have been monkey patched
expect(global.console.debug).toBe(originalConsole.debug);
expect(global.console.info).toBe(originalConsole.info);
expect(global.console.warn).toBe(originalConsole.warn);
expect(global.console.error).toBe(originalConsole.error);
expect(global.console.log).toBe(originalConsole.log);
expect(global.console.assert).toBe(originalConsole.assert);
// suppress output from the logging we're about to do
global.console.log = global.console.info = jest.fn();

// expect no message to be captured with console.log
global.console.log('some message');
expect(mockHub.captureMessage).not.toHaveBeenCalled();
// expect no message to be captured with console.log
global.console.log('some message');
expect(mockHub.captureMessage).not.toHaveBeenCalled();
});
});

it('setup should fail gracefully when console is not available', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import '@sentry/tracing';
import * as Sentry from '@sentry/node';
import { MongoClient } from 'mongodb';

// suppress logging of the mongo download
global.console.log = () => null;

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"test:express": "node test/manual/express-scope-separation/start.js",
"test:jest": "jest",
"test:release-health": "node test/manual/release-health/runner.js",
"test:webpack": "cd test/manual/webpack-domain/ && yarn && node npm-build.js",
"test:webpack": "cd test/manual/webpack-domain/ && yarn --silent && node npm-build.js",
"test:watch": "jest --watch"
},
"volta": {
Expand Down
5 changes: 3 additions & 2 deletions packages/node/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as domain from 'domain';
// We need this import here to patch domain on the global object
import * as Sentry from '../src';

// eslint-disable-next-line no-console
console.log(Sentry.SDK_NAME);
// TODO This is here because if we don't use the `Sentry` object, the 'concurrent domain hubs' test will fail. Is this a
// product of treeshaking?
Sentry.getCurrentHub();

describe('domains', () => {
test('without domain', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/node/test/manual/express-scope-separation/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const express = require('express');
const app = express();
const Sentry = require('../../../build/cjs');

// don't log the test errors we're going to throw, so at a quick glance it doesn't look like the test itself has failed
global.console.error = () => null;

function assertTags(actual, expected) {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
console.error('FAILED: Scope contains incorrect tags');
Expand Down
4 changes: 4 additions & 0 deletions packages/node/test/onunhandledrejection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Hub } from '@sentry/hub';

import { OnUnhandledRejection } from '../src/integrations/onunhandledrejection';

// don't log the test errors we're going to throw, so at a quick glance it doesn't look like the test itself has failed
global.console.warn = () => null;
global.console.error = () => null;

jest.mock('@sentry/hub', () => {
// we just want to short-circuit it, so dont worry about types
const original = jest.requireActual('@sentry/hub');
Expand Down