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', () => { 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', 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": { 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', () => { 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'); 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');