enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.96k stars 2.01k forks source link

TypeError: window.require is not a function #2575

Open cwu-ms opened 1 year ago

cwu-ms commented 1 year ago

Thanks for reporting an issue to us! We're glad you are using and invested in Enzyme. Before submitting, please read over our commonly reported issues to prevent duplicates!

All common issues

Notoriously common issues

If you haven't found any duplicated issues, please report it with your environment!

Current behavior

When testing my project, I get this error with the mount() function:

image
    TypeError: window.require is not a function

      57 |         };
      58 |
    > 59 |         const wrapper = mount(<HealthStatusAnchor healthStatus={health} resourceId={resourceId} isImpactedEnabled={isIREnabled}/> );
         |                         ^
      60 |
      61 |         wrapper.find('button').simulate('click');
      62 |         expect(openBladeSpy).toHaveBeenCalledTimes(1);

      at Object.getErrorBoundary (node_modules/azureportal-reactview/src/internal/ErrorBoundary.tsx:206:33)
      at _FrameworkIcon (node_modules/azureportal-reactview/src/FrameworkIcon.tsx:51:27)
      at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:14803:18)
      at mountIndeterminateComponent (node_modules/react-dom/cjs/react-dom.development.js:17482:13)
      at beginWork (node_modules/react-dom/cjs/react-dom.development.js:18596:16)
      at HTMLUnknownElement.callCallback (node_modules/react-dom/cjs/react-dom.development.js:188:14)
      at HTMLUnknownElement.callTheUserObjectsOperation (node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30)
      at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:338:25)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:274:3)
      at HTMLUnknownElementImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:221:9)
      at HTMLUnknownElementImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:94:17)
      at HTMLUnknownElement.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:231:34)
      at Object.invokeGuardedCallbackDev (node_modules/react-dom/cjs/react-dom.development.js:237:16)
      at invokeGuardedCallback (node_modules/react-dom/cjs/react-dom.development.js:292:31)
      at beginWork$1 (node_modules/react-dom/cjs/react-dom.development.js:23203:7)
      at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22157:12)
      at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:22130:22)
      at performSyncWorkOnRoot (node_modules/react-dom/cjs/react-dom.development.js:21756:9)
      at scheduleUpdateOnFiber (node_modules/react-dom/cjs/react-dom.development.js:21188:7)
      at updateContainer (node_modules/react-dom/cjs/react-dom.development.js:24373:3)
      at node_modules/react-dom/cjs/react-dom.development.js:24758:7
      at unbatchedUpdates (node_modules/react-dom/cjs/react-dom.development.js:21903:12)
      at legacyRenderSubtreeIntoContainer (node_modules/react-dom/cjs/react-dom.development.js:24757:5)
      at Object.render (node_modules/react-dom/cjs/react-dom.development.js:24840:10)
      at fn (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:488:26)
      at node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:405:37
      at batchedUpdates$1 (node_modules/react-dom/cjs/react-dom.development.js:21856:12)
      at Object.act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:929:14)
      at wrapAct (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:405:13)
      at Object.render (node_modules/enzyme-adapter-react-16/src/ReactSixteenAdapter.js:474:16)
      at new ReactWrapper (node_modules/enzyme/src/ReactWrapper.js:115:16)
      at Object.mount (node_modules/enzyme/src/mount.js:10:10)
      at Object.<anonymous> (components/common/__tests__/HealthStatusAnchor.spec.tsx:59:25)

Expected behavior

This test was running before, but after running yarn install, some of our other dependencies were updated and now the mount() function is throwing errors. We're not sure why this might be happening, since the relation between these updates and the new error is not clear. Does anyone know why this is happening?

Your environment

API

Version

library version
enzyme 3.11.0
react 16.14.0
react-dom 16.14.0
react-test-renderer 16.14.0
adapter (below)

Adapter

ljharb commented 1 year ago

To use any node module in a browser, it should be bundled by a working node module bundler. In node, require is always present in CJS modules. I'm really unsure where this error could be coming from, and I think you'll need to provide more info.

cwu-ms commented 1 year ago

@ljharb What other info would be helpful?

ljharb commented 1 year ago

@cwu-ms are you using a bundler? What test runner/framework are you using? Is this error occurring in a browser or in node? which node version?

cwu-ms commented 1 year ago

We are using Jest, and the error is appearing in node when we run npm run test, for jest --config ./test-config/jest.config.js --verbose. As for a bundler, we are using webpack.

ljharb commented 1 year ago

Presumably webpack isn’t involved for the jest tests, though - can you share your jest config?