mocha-sinon usage #357

Closed
ruswerner opened this Issue Nov 25, 2015 · 6 comments

Projects

None yet

2 participants

@ruswerner
ruswerner commented Nov 25, 2015 edited

After battling with mocha-sinon for a few days, I finally figured out how to set this up properly (or not?) in Wallaby. This assumes you are using mocha for the test framework and node as the test environment.

Specify a setup function in your config file:

    setup: function(wallaby) {
      var mocha = wallaby.testFramework;

      // setup sinon hooks
      var sinon = require('sinon');
      mocha.suite.beforeEach('sinon before', function() {
        if (null == this.sinon) {
          this.sinon = sinon.sandbox.create();
        }
      });
      mocha.suite.afterEach('sinon after', function() {
        if (this.sinon && 'function' === typeof this.sinon.restore) {
          this.sinon.restore();
        }
      });
    ....

This achieves the intent of the mocha-sinon package, except for the global before and after hooks, which aren't exposed on the mocha Suite object. Any stubbed objects in your spec's beforeEach hooks will be included in the sinon sandbox and automatically restored.

@ArtemGovorov
Member
ArtemGovorov commented Nov 26, 2015 edited

@ruswerner Nice, added to the recipes list in wallaby docs.

BTW, What was the issue with using mocha-sinon with wallaby?

Re: global before/after, mocha exposes beforeAll/afterAll if that's what you're after.

setup: function () {
      var mocha = wallaby.testFramework;
      mocha.suite.beforeAll(function () {
        console.log('beforeAll');
      });
      mocha.suite.afterAll(function () {
        console.log('afterAll');
      });
    },

There's also some sinon specific cleanup happening in wallaby.js internally. We intercept sinon.wrapMethod and then restore all objects during teardown just in case if something wasn't cleaned up.

@ArtemGovorov ArtemGovorov closed this in becfd1c Nov 26, 2015
@ArtemGovorov ArtemGovorov reopened this Nov 26, 2015
@ruswerner

@ArtemGovorov yes, the beforeAll and afterAll was what I was looking for. This is what I was trying to replicate: https://github.com/elliotf/mocha-sinon/blob/master/mocha-sinon.js

I was having trouble with the wallaby workers just stalling out and the test run never completed. This would only happen when I required mocha-sinon in the wallaby bootstrap function. I also tried setting the workers to exactly 1 and recycling, without luck.

Either way, I prefer to have this just explicitly in the bootstrap so that it's obvious what global magic is happening.

@ArtemGovorov
Member

@ruswerner Interesting, recycling should help as it make wallaby kill processes. Did you try it like this:

module.exports = function () {
  return {
    files: [...],

    tests: [...],

    ...

    workers: {recycle: true}
  };
};

?

@ruswerner

@ArtemGovorov Yes I did. It is strange and had me completely stumped, hence the work around. The first test run would not even complete. I can try to isolate a test case for you and post back here; along with console output.

@ArtemGovorov
Member

@ruswerner it'd be great if you could create the repro, thanks.

@ArtemGovorov
Member

I'll close the issue for now as there's been no activity on it for a while. If the issue is still valid for you, please share a sample repo where I could reproduce it, happy to have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment