From 9fe29c5d8a82afa74ef1dd2f26a1ef3776487069 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Sun, 10 Apr 2022 20:28:25 -0700 Subject: [PATCH 1/6] suppress output in `CaptureConsole` tests --- .../integrations/test/captureconsole.test.ts | 111 +++++++++++------- 1 file changed, 68 insertions(+), 43 deletions(-) diff --git a/packages/integrations/test/captureconsole.test.ts b/packages/integrations/test/captureconsole.test.ts index 966c76879522..f7979497ba6b 100644 --- a/packages/integrations/test/captureconsole.test.ts +++ b/packages/integrations/test/captureconsole.test.ts @@ -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, @@ -27,6 +36,11 @@ 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(); @@ -34,56 +48,67 @@ describe('CaptureConsole setup', () => { 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', () => { From 4756941b9ab395a22cd821437f2855973c9a7f53 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Sun, 10 Apr 2022 20:29:20 -0700 Subject: [PATCH 2/6] don't log dummy error in node scope separation test --- packages/node/test/manual/express-scope-separation/start.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/node/test/manual/express-scope-separation/start.js b/packages/node/test/manual/express-scope-separation/start.js index 689c9ef358e8..37733cf142a5 100644 --- a/packages/node/test/manual/express-scope-separation/start.js +++ b/packages/node/test/manual/express-scope-separation/start.js @@ -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'); From c9dd2a21a6d97bdd2cc6aeda21d4337e44980774 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Mon, 11 Apr 2022 14:39:13 -0700 Subject: [PATCH 3/6] don't log dummy error in node onunhandledrejection test --- packages/node/test/onunhandledrejection.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/node/test/onunhandledrejection.test.ts b/packages/node/test/onunhandledrejection.test.ts index 8588c9edddd6..012e6b37643f 100644 --- a/packages/node/test/onunhandledrejection.test.ts +++ b/packages/node/test/onunhandledrejection.test.ts @@ -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'); From b80b886c085c0ab2be76a328427d7a51883ed2a3 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Sun, 10 Apr 2022 20:30:04 -0700 Subject: [PATCH 4/6] use something besides logging to force a side effect in domain test --- packages/node/test/domain.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/node/test/domain.test.ts b/packages/node/test/domain.test.ts index 2d70fe1275e2..fa6bb94db292 100644 --- a/packages/node/test/domain.test.ts +++ b/packages/node/test/domain.test.ts @@ -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', () => { From d8296332e1c8d13683dbfd945342f2bacfdd7aac Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Sun, 10 Apr 2022 20:30:23 -0700 Subject: [PATCH 5/6] silence yarn output in node webpack test --- packages/node/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node/package.json b/packages/node/package.json index d0710af4fe4c..cbb1e4e2f7b0 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -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": { From f2dee4e6230df1af6d96045b364cb05892f032e3 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Sun, 10 Apr 2022 20:30:40 -0700 Subject: [PATCH 6/6] silence download logs in mongo test --- .../suites/tracing/auto-instrument/mongodb/scenario.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts index d9c8afd2e9df..870553f8b0b4 100644 --- a/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts +++ b/packages/node-integration-tests/suites/tracing/auto-instrument/mongodb/scenario.ts @@ -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://public@dsn.ingest.sentry.io/1337', release: '1.0',