webpack: Sinon causing issues again
The new version of Sinon is throwing webpack for a loop again. This code:
function makePublicAPI(require, exports, module) {
module.exports = sinon;
sinon.spy = require("./sinon/spy");
sinon.spyCall = require("./sinon/call");
sinon.behavior = require("./sinon/behavior");
sinon.stub = require("./sinon/stub");
sinon.mock = require("./sinon/mock");
sinon.collection = require("./sinon/collection");
sinon.assert = require("./sinon/assert");
sinon.sandbox = require("./sinon/sandbox");
sinon.test = require("./sinon/test");
sinon.testCase = require("./sinon/test_case");
sinon.match = require("./sinon/match");
}
if (isAMD) {
define(makePublicAPI);
} else if (isNode) {
try {
formatio = require("formatio");
} catch (e) {}
makePublicAPI(require, exports, module); <========
}
Is causing this warning:
WARNING in ./~/sinon/lib/sinon.js
Critical dependencies:
365:22-29 require function is used in a way, in which dependencies cannot be statically extracted
@ ./~/sinon/lib/sinon.js 365:22-29
It’s not obvious to me how to work around this. Any of you have any bright ideas?
(the line number indicated in the error, 365, is clearly wrong, and the line with the arrow is actually the offending code)
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 2
- Comments: 51 (15 by maintainers)
Links to this issue
Commits related to this issue
- Tell webpack no to parse sinon includes Webpack has issues with the non-stardard setup that Sinon.js uses: https://github.com/webpack/webpack/issues/304 — committed to openstax/tutor-js by nathanstitt 9 years ago
- Tell webpack not to parse sinon includes Webpack has issues with the non-stardard setup that Sinon.js uses: https://github.com/webpack/webpack/issues/304 — committed to openstax/tutor-js by nathanstitt 9 years ago
- chore(~): upgrade sinon webpack workaround https://github.com/webpack/webpack/issues/304 — committed to bemusic/bemuse by dtinth 9 years ago
- Use Sinon spy in HelloWorld test With hacky workaround because Sinon doesn't currently play well with Webpack. https://github.com/webpack/webpack/issues/304 — committed to cooperka/react-simple-boilerplate by cooperka 9 years ago
- Use Sinon spy in HelloWorld test With hacky workaround because Sinon doesn't currently play well with Webpack. https://github.com/webpack/webpack/issues/304 — committed to cooperka/react-simple-boilerplate by cooperka 9 years ago
- added tests for new hashHistory behaviour - added sinon-library for spies - to get webpack work with sinon, a little hack is needed currently -- https://github.com/webpack/webpack/issues/304 - new te... — committed to cwrs/history by cwrs 9 years ago
- Changes to get npm tasks to run on windows — committed to react-boilerplate/react-boilerplate by jwinn 8 years ago
- setup specs with webpack The `watch` task lints and bundles the script and spec files if they change. It also watches the bundled `app/test/bundle.js` to reload the extension. Sinon hacks to work wi... — committed to oddui/webdriver-extension by oddui 8 years ago
- Set up Sinon Work around for issue discussed here: https://github.com/webpack/webpack/issues/304. — committed to bobwhitelock/react-base by bobwhitelock 8 years ago
- Add testing-related packages - Note that `sinon` uses a pre-release version. The reason is that versions of `sinon` prior to 2.0 has trouble working with webpack (https://github.com/webpack/webpa... — committed to huy-nguyen/restaurant-menu-builder by deleted user 8 years ago
- Update webpack.config.js See thread : https://github.com/webpack/webpack/issues/304 Nice you added the sinon alias, but from what 'heldr' says, noParse needs to be added too. This fixed my problem ... — committed to Gregoirevda/hackjam.rxjs by Gregoirevda 7 years ago
- Fix modules[moduleId] error when running JS tests Running LMS Karma tests was causing the following error: TypeError: modules[moduleId] is undefined at /edx/app/edxapp/edx-platform/lms/static/co... — committed to openedx/edx-platform by deleted user 6 years ago
- Guard against sinon bugs See https://github.com/acvetkov/sinon-chrome/issues/11 https://github.com/acvetkov/sinon-chrome/issues/37 https://github.com/webpack/webpack/issues/304 — committed to kevinjmann/view-addon-study by adimit 7 years ago
- chore(~): upgrade sinon webpack workaround https://github.com/webpack/webpack/issues/304 — committed to rajr5/bemuse by dtinth 9 years ago
It works for me just adding
noParse
rule on webpack and requiring the dist version instead.Example:
Because of https://github.com/peerigon/legacy-loader/issues/1 I’ve actually switched to using:
npm install --save-dev sinon@next
@dtinth you can also create an alias for sinon
then import goes easier 🍵
thanks @graingert , only ur solution with
npm install --save-dev sinon@next
works for me@realisation My
noParse
looks like this:It’s almost like @SpaceK33z’s version but with an added
\/
at the end to prevent the rule from matchingnode_modules/sinon-chai
.Oh hm, Sinon.js is a test framework. I actually spent quite a bit of time writing deadunit, which provides a test API and nice pretty test output for node.js and the browser. It doesn’t have any functionality for creating spies, but that’s ok because using test spies is bad practice
@alexgorbatchev yeah! The alias can also be combined with
noParse
instead of creating loader and entry.“works on my machine!” 😆
I’m using:
then you can use:
I found that the solution provided by @solcik and @vitalets was close but raised errors with sinon
1.17.2
and webpack1.12.9
.Eventually I got it working with
when sinon is installed into
./node_modules
in the regular way withnpm install sinon
.You can clean it up a little by taking advantage of the webpack config:
and then
I bumped into this yesterday. For now I’m using the script-loader to work around this.
@fresheneesz Sinon.js isn’t a test framework, it’s a library to be used in testing. I’m not sure how that stack-overflow question demonstrates spies being a bad practice, it looks more like a conversation about a design that is hard to test.
sinon is our special friend… ^^
If anyone comes here for this error in combination with
karma
andphantomjs
:Add Sinon as external in webpack config:
(currently webpack 1.x and sinon 2.1.0 for me)
@shults just use sinon@next
FYI sinon@next is very close to release: https://github.com/sinonjs/sinon/issues/966
@heldr +1, works for me,THX
@heldr solution worked =] you are the man!
@vitalets The imports-loader can import multiple symbols at once…
Thanks @heldr, that seems to work! In my webpack.config.js, I added this:
I’ll leave this here for others… if you’re depending on libraries for testing that use Sinon, you can’t control what version of Sinon the library uses. So you might run into a problem where the version it uses can’t even be used with script-loader. To deal with this:
Like so:
You should not normally do this because the library you depend on might upgrade to a version of Sinon that isn’t API compatible with your local copy, or you might depend on multiple packages that depend on varying versions of Sinon. This approach is totally circumventing both NPM and webpack. And for sure, NEVER use this approach in releasing code, only tests.
Perhaps once Sinon 2 is more prevalent, these issues will go away, but in the mean time…
You always want to test your interface. If you’re substituting testing your public interface with testing some inner function, I’d say that’s the wrong move, no matter how significant that inner function is. If you have duplication in your tests, you can always take advantage of that symmetry - factor it out into common functions.
I know this is off-topic, but personally I use Grey-box-testing. I know “something” about the implementation, but I don’t really test everything “behind the scenes”.
A common use-case for spies is that you have several functions calling a particular function. By just testing this particular function and by using spies in all other cases I can keep all tests simple and expressive. Otherwise I needed to duplicate all tests.
Testing something “behind the scenes” should be an exception, but sometimes it actually leads to cleaner and more maintainable tests.
Oh dear, I took a look at what it might take to switch it over to UMD and stumbled on this. What on earth? Not sure I’m going to want to tackle that immediately…
Anyone know a good SinonJS alternative? I’ve found the API hard to wrap my head around anyway, with all the extremely similar but not exactly the same functionality.