webpack: Can't run sinon in node when webpacked
webpack is converting this:
var isNode = typeof module !== "undefined" && module.exports;
var isAMD = typeof define === 'function' && typeof define.amd === 'object' && define.amd;
if (isAMD) {
define(function(){
return sinon;
});
} else if (isNode) {
...
}
into this:
var isNode = typeof module !== "undefined" && module.exports;
var isAMD = 'function' === 'function' && typeof (require(11)) === 'object' && (require(11));
if (isAMD) {
{var __WEBPACK_AMD_DEFINE_RESULT__ = (function(){
return sinon;
}(require, exports, module)); if(__WEBPACK_AMD_DEFINE_RESULT__ !== undefined) module.exports = __WEBPACK_AMD_DEFINE_RESULT__;};
} else if (isNode) {
...
}
and ‘11’ refers to some webpack code which is definitely defined.
Is there a way to make webpack not load a module in such a way that define.amd
gets converted to something that is defined?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 42 (8 by maintainers)
Commits related to this issue
- Use sinon for stubbing in browser tests. Note that we're using a recent sinon commit from github, as the existing sinon on npm has issues with webpack: https://github.com/webpack/webpack/issues/177 ... — committed to mozilla/learning.mozilla.org by toolness 9 years ago
- Use sinon for stubbing in browser tests. Note that we're using a recent sinon commit from github, as the existing sinon on npm has issues with webpack: https://github.com/webpack/webpack/issues/177 ... — committed to vazquez/teach.webmaker.org by toolness 9 years ago
- refactor: add resolve.root to simplify requires - use webpack's resolve.root to allow resolving from src; - move utils folder to src to take advantage of above; - change webpack and npm scripts to co... — committed to StoryShop/app by joshdmiller 8 years ago
- Add tests for throttle and debounce functions This also includes an update to Sinon so that the useFakeTimers function will work with Webpack, see: https://github.com/webpack/webpack/issues/177#issue... — committed to Financial-Times/o-viewport by onishiweb 8 years ago
- Ember Upgrade via blueprint This is expected to fail due to a compatibility issue with Sinon and Webpack 5/Ember-auto-import v2 which I have yet to solve. See also: https://github.com/webpack/webpac... — committed to elwayman02/ember-sinon-qunit by elwayman02 a year ago
- Ember Upgrade via blueprint This is expected to fail due to a compatibility issue with Sinon and Webpack 5/Ember-auto-import v2 which I have yet to solve. See also: https://github.com/webpack/webpac... — committed to elwayman02/ember-sinon-qunit by elwayman02 a year ago
AMD is just stupid…
I have to read the entire thread to figure out what I actually need to do. To summarize this for everyone, all the webpack.config changes required to get sinon to work (don’t forget to install imports-loader):
nm, just use sinon@2.0.0-pre
I encountered the same issue: sinon doesn’t export itself correctly when webpacked.
Solution without karma
I first ended up with this solution.
Add the following imports loader configuration in webpack configuration loaders array:
Notice the addition of
require=>false
to @simple10 's solution. Then sinon can be required using the built package:You are free to add an alias from “sinon” to “sinon/pkg/sinon” in your test webpack configuration.
Solution with karma
My case was browser-side, so packages like karma-sinon-chai which can make the browser load the built file of sinon are a solution. Sinon is not part of the test webpacked bundle file any more.
And in case anyone stumbles on this in the future, you want to work around such issues in your webpack configuration, both because the library itself my require itself (such as SinonJS does) or some other library might require it, and you want all of them to require the same AMD-undefined version of sinon. So in my configuration I have this:
K, I’ll stop talking to myself now 😃
We ran into this issue when using Enzyme to test react components, and what worked for us (to document for posterity) was a combination of above approaches:
@jbaiter I ran into the same issue when using
require("imports?define=>false!sinon");
SinonJS requires itself with several circular dependencies. Requiring sinon.js requires sinon/spy.js which in turn requires sinon.js. But when spy.js requires sinon.js, it does not use the imports?define=>false shim, so you end up with an error.
To get everything to work properly, you must do two things:
npm install imports-loader --save-dev
Then just use
require('sinon');
as normal and everything should work.Thanks to @altano for the solution above. I just missed his comment the first time and also didn’t have imports-loader installed.
By far, the easiest solution is to use sinon@2.0.0-pre. I tested with sinon@2.0.0-pre.2 and sinon.useFakeXMLHttpRequest, worked great.
I filed an issue with SinonJS (https://github.com/cjohansen/Sinon.JS/issues/416) but in the mean time I worked around the problem by shimming the module to disable amd:
var sinon = require("imports?define=>false!sinon");
@ericlau-solid does that work for you when calling useFakeTimers?
Sinon 1.12.1 requires lolex in util/fake_timer.js which breaks in webpack since it’s a conditional require that webpack doesn’t find by default.
There’s probably a clever way to force webpack to include lolex in the bundle, but I couldn’t find a quick solution.
Here’s what I got working.
There’s only one little customization in my modified sinon to make sure webpack requires lolex. Not an ideal solution, but works.