Title: Sentry 7.30.0 upwards breaks our Jest tests by making `fetch` not exist · Issue #7363 · getsentry/sentry-javascript · GitHub
Open Graph Title: Sentry 7.30.0 upwards breaks our Jest tests by making `fetch` not exist · Issue #7363 · getsentry/sentry-javascript
X Title: Sentry 7.30.0 upwards breaks our Jest tests by making `fetch` not exist · Issue #7363 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using the latest SDK release https://github.com/...
Open Graph Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
X Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/7363
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sentry 7.30.0 upwards breaks our Jest tests by making `fetch` not exist","articleBody":"### Is there an existing issue for this?\n\n- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues\n- [X] I have reviewed the documentation https://docs.sentry.io/\n- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases\n\n### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### 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.\n\n@sentry/nextjs\n\n### SDK Version\n\n7.30.0\n\n### Framework Version\n\nNext.js 12.3.2\n\n### Link to Sentry event\n\n_No response_\n\n### SDK Setup\n\n```javascript\r\nSentry.init({\r\n enabled: isEnabled,\r\n dsn: SENTRY_DSN,\r\n environment: SENTRY_ENV,\r\n release: BUILD_ID,\r\n ignoreErrors,\r\n beforeSend: event => {\r\n const { type, title, contexts } = event;\r\n if (type === 'error' &&\r\n /^(?:iPhone|Mac|iPad)$/.test(contexts?.device?.family || '') &&\r\n (\r\n /^TypeError: (?:cancelled|geannuleerd|cancelado|annulleret|avbrutt|avbruten|Abgebrochen)$/i.test(title || '')\r\n )) {\r\n return null;\r\n }\r\n\r\n integrateWithFullstory(event);\r\n return event;\r\n },\r\n integrations: [\r\n new Integrations.BrowserTracing({\r\n tracingOrigins: [ getApiHost() ],\r\n beforeNavigate: context => {\r\n const dynamicRouteName = mapSlugToRoute(window.location.pathname);\r\n if (dynamicRouteName) {\r\n return {\r\n ...context, name: dynamicRouteName,\r\n };\r\n }\r\n return context;\r\n },\r\n }),\r\n ]\r\n tracesSampler: samplingRate,\r\n});\r\n```\n\n### Steps to Reproduce\n\n1. With Sentry SDK at any version 7.29.0 or earlier, run Jest tests; they pass\r\n2. Upgrade `@sentry/nextjs` and `@sentry/tracing` and `@sentry/utils` to 7.30.0 or later\r\n3. Run Jest tests; they fail\n\n### Expected Result\n\nOur Jest tests pass.\n\n### Actual Result\n\nOur Jest tests fail with several tests outputting `ReferenceError: fetch is not defined` at the line:\r\n```javascript\r\n return fetch(resolvePath(url), options)\r\n```\r\n\r\nActual app works fine, only our Jest tests fail.\r\n\r\nSince this failure starts in Sentry SDK 7.30.0, I think it likely has to do with https://github.com/getsentry/sentry-javascript/pull/6500/ — in particular [the wrapping of fetch](https://github.com/getsentry/sentry-javascript/pull/6500/files#diff-008072d6cb713fbfe5da7662c7b529e1da16b8fcfdddd51d21eb2544a392c3c8R278-R306) and its uses of [this check](https://github.com/getsentry/sentry-javascript/blob/e497bcda305a9c248dcdc78f48fe548cc58f8233/packages/utils/src/supports.ts#L55-L122) and/or [this wrapping utility](https://github.com/getsentry/sentry-javascript/blob/e497bcda305a9c248dcdc78f48fe548cc58f8233/packages/utils/src/object.ts#L9-L40), interacting poorly with whatever shenanigans Jest gets up to.\r\n\r\nLikely relevant: `jest.config.js`\r\n```javascript\r\nmodule.exports = {\r\n transformIgnorePatterns: [ \"/node_modules/(?!@grid-is/)\" ],\r\n clearMocks: true,\r\n coverageDirectory: './coverage/',\r\n testEnvironment: 'jsdom',\r\n testMatch: [\r\n '**/__tests__/**/*.[jt]s?(x)',\r\n '**/?(*.)+(spec|test).[tj]s?(x)',\r\n '**/?(*-)+(test).[tj]s?(x)',\r\n ],\r\n moduleNameMapper: {\r\n '\\\\.(css|scss)$': 'identity-obj-proxy',\r\n },\r\n reporters: [ 'default', 'jest-junit' ],\r\n testPathIgnorePatterns: [ '.next', '\u003crootDir>/migrate/' ],\r\n setupFiles: [ 'jest-canvas-mock' ],\r\n setupFilesAfterEnv: [ '@testing-library/jest-dom/extend-expect', './jest.setup.js' ],\r\n globals: {...},\r\n};\r\n```\r\nand `jest.setup.js` contains:\r\n```javascript\r\nlet unignorableConsoleErrorMessages;\r\nconst originalError = global.console.error;\r\nconst spy = jest.spyOn(global.console, 'error');\r\nconst infoSpy = jest.spyOn(global.console, 'info');\r\n\r\n// sprintf-like string format compatible with console.log\r\nconst sprintf = (str, ...argv) => {\r\n if (!argv.length || !/%[sdif]/.test(str)) {\r\n return [ str, ...argv ].join(' ');\r\n }\r\n\r\n return sprintf(str.replace(/%[sdif]/, function (format) {\r\n switch (format) {\r\n case '%s': return String(argv.shift());\r\n case '%d':\r\n case '%i': return parseInt(argv.shift(), 10);\r\n case '%f': return parseFloat(argv.shift(), 10);\r\n default: return format;\r\n }\r\n }), ...argv);\r\n};\r\n\r\nconst ignorableConsoleErrorMessages = [\r\n /Can't perform a React state update on an unmounted component/,\r\n /Warning: A component is changing an uncontrolled input to be controlled./,\r\n /Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?/,\r\n];\r\n\r\nbeforeEach(() => {\r\n unignorableConsoleErrorMessages = new Set();\r\n spy.mockImplementation((...args) => {\r\n originalError(...args);\r\n const msg = sprintf(...args);\r\n for (const regexp of ignorableConsoleErrorMessages) {\r\n if (regexp.test(msg)) {\r\n return;\r\n }\r\n }\r\n unignorableConsoleErrorMessages.add(msg);\r\n });\r\n infoSpy.mockImplementation(() => {});\r\n});\r\nafterEach(() => {\r\n if (unignorableConsoleErrorMessages.size) {\r\n throw new Error(\r\n 'Unignorable console.error output from test execution:\\n' +\r\n Array.from(unignorableConsoleErrorMessages).join('\\n')\r\n );\r\n }\r\n});\r\n```","author":{"url":"https://github.com/gthb","@type":"Person","name":"gthb"},"datePublished":"2023-03-07T10:53:51Z","url":"https://github.com/getsentry/sentry-javascript/issues/7363"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2838295f-0a40-4864-324c-ff117e9d1b36 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD5A:1D3CBE:26B70:33D1A:6B042A0F |
| html-safe-nonce | 04fff5d8514960ed95889f06b1fcfb9f505191c0caec1b96303feee6218617e7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDVBOjFEM0NCRToyNkI3MDozM0QxQTo2QjA0MkEwRiIsInZpc2l0b3JfaWQiOiIyNTA4MTk5OTM5ODkzNjM1NTk5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7a30114b777d3d44c29aa3c6c5e5b298f23bc2baf19008c3a43a168591bc239d |
| hovercard-subject-tag | issue:1613188562 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/7363/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9d77c0a115d4659550eb4eefbe9b04f2abdc98df4e14721a950054477cf6b3c7/getsentry/sentry-javascript/issues/7363 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9d77c0a115d4659550eb4eefbe9b04f2abdc98df4e14721a950054477cf6b3c7/getsentry/sentry-javascript/issues/7363 |
| og:image:alt | Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gthb |
| hostname | github.com |
| expected-hostname | github.com |
| None | b7a1d2b9101496921b08579fbee988d014345aef2f5bd660d1f8c7332d81560e |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c3dd496c3da6ea6b77e69c09279d2b81604654f1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width