Sinon: Using Sinon with Webpack and Babel fails on import

Created on 8 Sep 2015  ·  26Comments  ·  Source: sinonjs/sinon

When trying to import sinon in a test with "import sinon from 'sinon'" or "var sinon = require('sinon')". I get the following error:

TypeError: 'undefined' is not an object (evaluating 'modules[moduleId].call')

Now sinon is unusable in ES6 projects. Is it possible to fix this?

var sinon = require('sinon');

1.x AMD Bug Help wanted

Most helpful comment

PS. You can get the pre-release of 2.0 by doing npm install sinon@next

All 26 comments

Are you using sinon from npm? Which version?

This webpack issue covers the same thing, without a solid solution and lots of working around the problem: webpack/webpack#304. It's not the exact same error, but basically the same problem.

Yes I am using the latest npm version.

Yes I am using the latest npm version.

Just for the sake of history, it would be great if you could provide the version number. Future readers of this thread would then know which version you were talking about.

This webpack issue covers the same thing, without a solid solution and lots of working around the problem: webpack/webpack#304. It's not the exact same error, but basically the same problem.

Yeah, it looks like the module-_mess_ in Sinon is getting to be more and more of an issue. I've taken a few attempts at rewriting it for CommonJS, but things are complicated. Pull requests welcome :)

I've created a ticket for the work of getting the source ported to CommonJS #834, which will most likely obviate this issue.

I think this issue is solved now in the master with #850

Any updates on this? #850 doesn't solve the problem.

PS: I'm using v1.17.0

@Robertmw you can try pointing sinon to git in your package.json to get the latest master and see if that helps

@grassator I tried with the below git from https://github.com/webpack/webpack/issues/304#issuecomment-136922511 but I'll try it with the original git right away.

devDependencies: {
     "sinon": "git+https://github.com/uberVU/Sinon.JS"
 }

@grassator I can confirm that point the dependency on my package.json to:

"sinon": "git+https://github.com/sinonjs/sinon.git"

solves my problems with Webpack.

I am also facing the same issue. For now, I am using require('sinon') before I initialize babel.

@tusharmath : have you tried using the version hosted on GitHub as mentioned above? Fixes for this is going into version 2, which is yet to be published. This is unlikely to be fixed in the 1.x branch.

@fatso83 I can confirm that using master fixes the issue. Sorry to hear that the fix won't make it into 1.x.

2.0 release should happen soon, so nothing to be sad about

I've had a project relying on the sinon-2.0 branch (git://github.com/cjohansen/Sinon.JS.git#sinon-2.0). This has been working great up until I tried it today. The branch seems to have vanished. Is there a suitable replacement for this branch yet?

@frank-weindel : The master branch is what is effectively becoming 2.0, so you could just as well point to that. A pre-release of 2.0 should be on NPM in a few days.

PS. You can get the pre-release of 2.0 by doing npm install sinon@next

any update on when sinon 2 will be @latest instead of @next?

We need help to get Sinon 2 out the door. See #991 and #1000 (and related issues) for remaining big issues if you can spare some time in helping us out.

Note #966 tracks the outstanding tasks required for shipping Sinon 2.0 which uses CommonJS modules internally - help very much appreciated in getting it out the door.

try to use it as a webpack plugin

...
plugins: [
        new webpack.ProvidePlugin( {
          'sinon': 'sinon'
       })
      ],
...

Just for the record, using it as with webpack.ProvidePlugin doesn't work either.

Personally I just stopped using Karma and thus not needing Webpack for unit tests. The makes them way faster to run also. Does this issue still exist? Other wise we can close it.

The issue still exists in sinon < 2.0, which is still not officially released. I recommend closing this issue either with a fix or when sinon 2.0 ships.

This works in Sinon 2.0 and the README has since long had a BIG FAT WARNING that bundlers like WebPack _do not work_ with Sinon 1.x. If you need to bundle, use one of the pre-release versions of Sinon. Closing this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JakobJingleheimer picture JakobJingleheimer  ·  3Comments

optimatex picture optimatex  ·  4Comments

NathanHazout picture NathanHazout  ·  3Comments

fearphage picture fearphage  ·  3Comments

ndhoule picture ndhoule  ·  4Comments