Skip to content

Sentry 7.30.0 upwards breaks our Jest tests by making fetch not exist #7363

Description

@gthb

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using? If you use the CDN bundles, please specify the exact bundle (e.g. bundle.tracing.min.js) in your SDK setup.

@sentry/nextjs

SDK Version

7.30.0

Framework Version

Next.js 12.3.2

Link to Sentry event

No response

SDK Setup

Sentry.init({enabled: isEnabled,dsn: SENTRY_DSN,environment: SENTRY_ENV,release: BUILD_ID,
ignoreErrors,beforeSend: event=>{const{ type, title, contexts }=event;if(type==='error'&&/^(?:iPhone|Mac|iPad)$/.test(contexts?.device?.family||'')&&(/^TypeError:(?:cancelled|geannuleerd|cancelado|annulleret|avbrutt|avbruten|Abgebrochen)$/i.test(title||''))){returnnull;}integrateWithFullstory(event);returnevent;},integrations: [newIntegrations.BrowserTracing({tracingOrigins: [getApiHost()],beforeNavigate: context=>{constdynamicRouteName=mapSlugToRoute(window.location.pathname);if(dynamicRouteName){return{
...context,name: dynamicRouteName,};}returncontext;},}),]tracesSampler: samplingRate,});

Steps to Reproduce

  1. With Sentry SDK at any version 7.29.0 or earlier, run Jest tests; they pass
  2. Upgrade @sentry/nextjs and @sentry/tracing and @sentry/utils to 7.30.0 or later
  3. Run Jest tests; they fail

Expected Result

Our Jest tests pass.

Actual Result

Our Jest tests fail with several tests outputting ReferenceError: fetch is not defined at the line:

returnfetch(resolvePath(url),options)

Actual app works fine, only our Jest tests fail.

Since this failure starts in Sentry SDK 7.30.0, I think it likely has to do with #6500 — in particular the wrapping of fetch and its uses of this check and/or this wrapping utility, interacting poorly with whatever shenanigans Jest gets up to.

Likely relevant: jest.config.js

module.exports={transformIgnorePatterns: ["/node_modules/(?!@grid-is/)"],clearMocks: true,coverageDirectory: './coverage/',testEnvironment: 'jsdom',testMatch: ['**/__tests__/**/*.[jt]s?(x)','**/?(*.)+(spec|test).[tj]s?(x)','**/?(*-)+(test).[tj]s?(x)',],moduleNameMapper: {'\\.(css|scss)$': 'identity-obj-proxy',},reporters: ['default','jest-junit'],testPathIgnorePatterns: ['.next','<rootDir>/migrate/'],setupFiles: ['jest-canvas-mock'],setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect','./jest.setup.js'],globals: {...},};

and jest.setup.js contains:

letunignorableConsoleErrorMessages;constoriginalError=global.console.error;constspy=jest.spyOn(global.console,'error');constinfoSpy=jest.spyOn(global.console,'info');// sprintf-like string format compatible with console.logconstsprintf=(str, ...argv)=>{if(!argv.length||!/%[sdif]/.test(str)){return[str, ...argv].join(' ');}returnsprintf(str.replace(/%[sdif]/,function(format){switch(format){case'%s': returnString(argv.shift());case'%d':
case'%i': returnparseInt(argv.shift(),10);case'%f': returnparseFloat(argv.shift(),10);default: returnformat;}}), ...argv);};constignorableConsoleErrorMessages=[/Can'tperformaReactstateupdateonanunmountedcomponent/,/Warning:Acomponentischanginganuncontrolledinputtobecontrolled./,/Warning:Functioncomponentscannotbegivenrefs.Attemptstoaccessthisrefwillfail.DidyoumeantouseReact.forwardRef()?/,];beforeEach(()=>{unignorableConsoleErrorMessages=newSet();spy.mockImplementation((...args)=>{originalError(...args);constmsg=sprintf(...args);for(constregexpofignorableConsoleErrorMessages){if(regexp.test(msg)){return;}}unignorableConsoleErrorMessages.add(msg);});infoSpy.mockImplementation(()=>{});});afterEach(()=>{if(unignorableConsoleErrorMessages.size){thrownewError('Unignorable console.error output from test execution:\n'+Array.from(unignorableConsoleErrorMessages).join('\n'));}});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions