sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.63k stars 769 forks source link

Error: Can't resolve 'process/browser' in ./node_modules/sinon/pkg/sinon-esm.js #2459

Closed henriquemattos closed 2 years ago

henriquemattos commented 2 years ago

Describe the bug Can't bundle tests via Karma using Webpack as sinon-ejm.js failed to resolve 'process/browser'.

The request 'process/browser' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

Solution

Added fallback to webpack configuration in karma.conf.js

fallback: {
    'process/browser': require.resolve('process/browser.js')
},

Expected behavior I would expect the module to be resolved without a custom fallback from the client.

Screenshots If applicable, add screenshots to help explain your problem.

Uncaught Error: Cannot find module 'process/browser'
  at /var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/commons.js:180396:170

  Error: Cannot find module 'process/browser'
      at webpackMissingModule (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/commons.js:180396:90)
      at Module../node_modules/sinon/pkg/sinon-esm.js (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/commons.js:180396:180)
      at __webpack_require__ (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/runtime.js:23:42)
      at Module../web/test/js/csp/src/SAP/EventTicketing/Wizard/ActionCodeWizard.1_test.js (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/commons.js:65630:63)
      at __webpack_require__ (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/runtime.js:23:42)
      at __webpack_exec__ (ActionCodeWizard.1_test.1724039794.js:4:48)
      at ActionCodeWizard.1_test.1724039794.js:5:55
      at Function.__webpack_require__.O (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/runtime.js:60:23)
      at ActionCodeWizard.1_test.1724039794.js:6:56
      at webpackJsonpCallback (/var/folders/tf/kg_36rhn4dz0xphylk9sc7q00000gn/T/_karma_webpack_430861/runtime.js:202:39)

Context (please complete the following information):

% node -v && npm -v
v14.19.1
6.14.16

"devDependencies": {
    "@babel/core": "^7.18.2",
    "@babel/eslint-parser": "^7.18.2",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/preset-env": "^7.18.2",
    "@babel/runtime": "^7.18.3",
    "chai": "^4.3.6",
    "eslint": "^8.16.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-chai-friendly": "^0.7.2",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-mocha": "^10.0.4",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.0.0",
    "istanbul-instrumenter-loader": "^3.0.1",
    "karma": "^6.3.20",
    "karma-chrome-launcher": "^3.1.1",
    "karma-coverage": "^2.2.0",
    "karma-firefox-launcher": "^2.1.2",
    "karma-fixture": "^0.2.6",
    "karma-json-fixtures-preprocessor": "0.0.6",
    "karma-junit-reporter": "^2.0.1",
    "karma-mocha": "^2.0.1",
    "karma-sinon": "^1.0.5",
    "karma-sourcemap-loader": "^0.3.8",
    "karma-spec-reporter": "0.0.34",
    "karma-webpack": "^5.0.0",
    "mocha": "^10.0.0",
    "process": "^0.11.10",
    "sinon": "^14.0.0",
    "sinon-chai": "^3.5.0",
    "webpack": "^5.72.1",
    "webpack-cli": "^4.9.2",
  },
  "dependencies": {
    "@babel/polyfill": "^7.10.4"
  }
fatso83 commented 2 years ago

Thank you for taking the time to report this. Unfortunately I cannot see how this is related to the Sinon project itself or anything we can do on our side.

As you have witnessed, bundling is inherently fraught with complexity, with ESM, transpilation, different runtimes, etc all coming into play.

The module you have to take manual steps to resolve is not even part of our bundle! Try looking at the file and search for the module yourself: https://unpkg.com/browse/sinon@14.0.0/pkg/sinon-esm.js

Since this is an artefact added by Webpack I cannot see how we are to do anything with it?