Skip to content

Commit 0fc29b0

Browse files
lobsterkatieAbhiPrasad
authored andcommitted
fix(tests): Silence dummy output in tests (#4979)
We have a number of tests which log messages to the console, for reasons other than communicating with the person running the tests. When trying to debug a large number of tests, across the whole repo, it's distracting to see this output (some of which actually even looks like failing tests). This silences that output, so it doesn't seem like tests are erroring when they're not.
1 parent f447012 commit 0fc29b0

File tree

6 files changed

+82
-46
lines changed

6 files changed

+82
-46
lines changed

packages/integrations/test/captureconsole.test.ts

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ const mockHub = {
1717
captureException: jest.fn(),
1818
};
1919

20+
const mockConsole = {
21+
debug: jest.fn(),
22+
log: jest.fn(),
23+
warn: jest.fn(),
24+
error: jest.fn(),
25+
assert: jest.fn(),
26+
info: jest.fn(),
27+
};
28+
2029
const getMockHubWithIntegration = (integration: Integration) =>
2130
({
2231
...mockHub,
@@ -27,63 +36,79 @@ const getMockHubWithIntegration = (integration: Integration) =>
2736
const originalConsole = Object.assign({}, global.console);
2837

2938
describe('CaptureConsole setup', () => {
39+
beforeEach(() => {
40+
// this suppresses output to the terminal running the tests, but doesn't interfere with our wrapping
41+
Object.assign(global.console, mockConsole);
42+
});
43+
3044
afterEach(() => {
3145
jest.clearAllMocks();
3246

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

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

44-
expect(global.console.error).toBe(originalConsole.error); // not monkey patched
45-
expect(global.console.log).not.toBe(originalConsole.log); // monkey patched
46-
expect(global.console.warn).not.toBe(originalConsole.warn); // monkey patched
47-
});
58+
it('should patch user-configured console levels', () => {
59+
const captureConsoleIntegration = new CaptureConsole({ levels: ['log', 'warn'] });
60+
captureConsoleIntegration.setupOnce(
61+
() => undefined,
62+
() => getMockHubWithIntegration(captureConsoleIntegration),
63+
);
4864

49-
it('should fall back to default console levels if none are provided', () => {
50-
const captureConsoleIntegration = new CaptureConsole();
51-
captureConsoleIntegration.setupOnce(
52-
() => undefined,
53-
() => getMockHubWithIntegration(captureConsoleIntegration),
54-
);
65+
expect(global.console.error).toBe(originalConsole.error); // not monkey patched
66+
expect(global.console.log).not.toBe(originalConsole.log); // monkey patched
67+
expect(global.console.warn).not.toBe(originalConsole.warn); // monkey patched
68+
});
5569

56-
// expect a set of defined console levels to have been monkey patched
57-
expect(global.console.debug).not.toBe(originalConsole.debug);
58-
expect(global.console.info).not.toBe(originalConsole.info);
59-
expect(global.console.warn).not.toBe(originalConsole.warn);
60-
expect(global.console.error).not.toBe(originalConsole.error);
61-
expect(global.console.log).not.toBe(originalConsole.log);
62-
expect(global.console.assert).not.toBe(originalConsole.assert);
70+
it('should fall back to default console levels if none are provided', () => {
71+
const captureConsoleIntegration = new CaptureConsole();
72+
captureConsoleIntegration.setupOnce(
73+
() => undefined,
74+
() => getMockHubWithIntegration(captureConsoleIntegration),
75+
);
6376

64-
// any other fields should not have been patched
65-
expect(global.console.trace).toBe(originalConsole.trace);
66-
expect(global.console.table).toBe(originalConsole.table);
67-
});
77+
// expect a set of defined console levels to have been monkey patched
78+
expect(global.console.debug).not.toBe(originalConsole.debug);
79+
expect(global.console.info).not.toBe(originalConsole.info);
80+
expect(global.console.warn).not.toBe(originalConsole.warn);
81+
expect(global.console.error).not.toBe(originalConsole.error);
82+
expect(global.console.log).not.toBe(originalConsole.log);
83+
expect(global.console.assert).not.toBe(originalConsole.assert);
84+
85+
// any other fields should not have been patched
86+
expect(global.console.trace).toBe(originalConsole.trace);
87+
expect(global.console.table).toBe(originalConsole.table);
88+
});
89+
90+
it('should not wrap any functions with an empty levels option', () => {
91+
const captureConsoleIntegration = new CaptureConsole({ levels: [] });
92+
captureConsoleIntegration.setupOnce(
93+
() => undefined,
94+
() => getMockHubWithIntegration(captureConsoleIntegration),
95+
);
6896

69-
it('should not wrap any functions with an empty levels option', () => {
70-
const captureConsoleIntegration = new CaptureConsole({ levels: [] });
71-
captureConsoleIntegration.setupOnce(
72-
() => undefined,
73-
() => getMockHubWithIntegration(captureConsoleIntegration),
74-
);
97+
// expect the default set of console levels not to have been monkey patched
98+
expect(global.console.debug).toBe(originalConsole.debug);
99+
expect(global.console.info).toBe(originalConsole.info);
100+
expect(global.console.warn).toBe(originalConsole.warn);
101+
expect(global.console.error).toBe(originalConsole.error);
102+
expect(global.console.log).toBe(originalConsole.log);
103+
expect(global.console.assert).toBe(originalConsole.assert);
75104

76-
// expect the default set of console levels not to have been monkey patched
77-
expect(global.console.debug).toBe(originalConsole.debug);
78-
expect(global.console.info).toBe(originalConsole.info);
79-
expect(global.console.warn).toBe(originalConsole.warn);
80-
expect(global.console.error).toBe(originalConsole.error);
81-
expect(global.console.log).toBe(originalConsole.log);
82-
expect(global.console.assert).toBe(originalConsole.assert);
105+
// suppress output from the logging we're about to do
106+
global.console.log = global.console.info = jest.fn();
83107

84-
// expect no message to be captured with console.log
85-
global.console.log('some message');
86-
expect(mockHub.captureMessage).not.toHaveBeenCalled();
108+
// expect no message to be captured with console.log
109+
global.console.log('some message');
110+
expect(mockHub.captureMessage).not.toHaveBeenCalled();
111+
});
87112
});
88113

89114
it('setup should fail gracefully when console is not available', () => {

packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import '@sentry/tracing';
33
import * as Sentry from '@sentry/node';
44
import { MongoClient } from 'mongodb';
55

6+
// suppress logging of the mongo download
7+
global.console.log = () => null;
8+
69
Sentry.init({
710
dsn: 'https://[email protected]/1337',
811
release: '1.0',

packages/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"test:express": "node test/manual/express-scope-separation/start.js",
6161
"test:jest": "jest",
6262
"test:release-health": "node test/manual/release-health/runner.js",
63-
"test:webpack": "cd test/manual/webpack-domain/ && yarn && node npm-build.js",
63+
"test:webpack": "cd test/manual/webpack-domain/ && yarn --silent && node npm-build.js",
6464
"test:watch": "jest --watch"
6565
},
6666
"volta": {

packages/node/test/domain.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import * as domain from 'domain';
44
// We need this import here to patch domain on the global object
55
import * as Sentry from '../src';
66

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

1011
describe('domains', () => {
1112
test('without domain', () => {

packages/node/test/manual/express-scope-separation/start.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const express = require('express');
33
const app = express();
44
const Sentry = require('../../../build/cjs');
55

6+
// 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
7+
global.console.error = () => null;
8+
69
function assertTags(actual, expected) {
710
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
811
console.error('FAILED: Scope contains incorrect tags');

packages/node/test/onunhandledrejection.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { Hub } from '@sentry/hub';
22

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

5+
// 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
6+
global.console.warn = () => null;
7+
global.console.error = () => null;
8+
59
jest.mock('@sentry/hub', () => {
610
// we just want to short-circuit it, so dont worry about types
711
const original = jest.requireActual('@sentry/hub');

0 commit comments

Comments
 (0)