Skip to content

Commit 95fbdbf

Browse files
committed
silence extraneous output in tests
1 parent 8961dcf commit 95fbdbf

File tree

5 files changed

+80
-46
lines changed

5 files changed

+80
-46
lines changed

packages/integrations/test/captureconsole.test.ts

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

19+
const mockConsole = {
20+
debug: jest.fn(),
21+
log: jest.fn(),
22+
warn: jest.fn(),
23+
error: jest.fn(),
24+
assert: jest.fn(),
25+
info: jest.fn(),
26+
};
27+
1928
const getMockHubWithIntegration = (integration: Integration) => ({
2029
...mockHub,
2130
getIntegration: jest.fn(() => integration),
@@ -25,63 +34,81 @@ const getMockHubWithIntegration = (integration: Integration) => ({
2534
const originalConsole = Object.assign({}, global.console);
2635

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

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

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

42-
expect(global.console.error).toBe(originalConsole.error); // not monkey patched
43-
expect(global.console.log).not.toBe(originalConsole.log); // monkey patched
44-
expect(global.console.warn).not.toBe(originalConsole.warn); // monkey patched
45-
});
56+
it('should patch user-configured console levels', () => {
57+
global.console.error = originalConsole.error;
4658

47-
it('should fall back to default console levels if none are provided', () => {
48-
const captureConsoleIntegration = new CaptureConsole();
49-
captureConsoleIntegration.setupOnce(
50-
() => undefined,
51-
() => getMockHubWithIntegration(captureConsoleIntegration) as any,
52-
);
59+
const captureConsoleIntegration = new CaptureConsole({ levels: ['log', 'warn'] });
60+
captureConsoleIntegration.setupOnce(
61+
() => undefined,
62+
() => getMockHubWithIntegration(captureConsoleIntegration) as any,
63+
);
5364

54-
// expect a set of defined console levels to have been monkey patched
55-
expect(global.console.debug).not.toBe(originalConsole.debug);
56-
expect(global.console.info).not.toBe(originalConsole.info);
57-
expect(global.console.warn).not.toBe(originalConsole.warn);
58-
expect(global.console.error).not.toBe(originalConsole.error);
59-
expect(global.console.log).not.toBe(originalConsole.log);
60-
expect(global.console.assert).not.toBe(originalConsole.assert);
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+
});
6169

62-
// any other fields should not have been patched
63-
expect(global.console.trace).toBe(originalConsole.trace);
64-
expect(global.console.table).toBe(originalConsole.table);
65-
});
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) as any,
75+
);
6676

67-
it('should not wrap any functions with an empty levels option', () => {
68-
const captureConsoleIntegration = new CaptureConsole({ levels: [] });
69-
captureConsoleIntegration.setupOnce(
70-
() => undefined,
71-
() => getMockHubWithIntegration(captureConsoleIntegration) as any,
72-
);
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) as any,
95+
);
7396

74-
// expect the default set of console levels not to have been monkey patched
75-
expect(global.console.debug).toBe(originalConsole.debug);
76-
expect(global.console.info).toBe(originalConsole.info);
77-
expect(global.console.warn).toBe(originalConsole.warn);
78-
expect(global.console.error).toBe(originalConsole.error);
79-
expect(global.console.log).toBe(originalConsole.log);
80-
expect(global.console.assert).toBe(originalConsole.assert);
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);
81104

82-
// expect no message to be captured with console.log
83-
global.console.log('some message');
84-
expect(mockHub.captureMessage).not.toHaveBeenCalled();
105+
// suppress output from the logging we're about to do
106+
global.console.log = global.console.info = jest.fn();
107+
108+
// expect no message to be captured with console.log
109+
global.console.log('some message');
110+
expect(mockHub.captureMessage).not.toHaveBeenCalled();
111+
});
85112
});
86113

87114
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');

0 commit comments

Comments
 (0)