New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha-sinon usage #357

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

Comments

Projects
None yet
2 participants
@ruswerner

ruswerner commented Nov 25, 2015

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

This comment has been minimized.

Show comment
Hide comment
@ArtemGovorov

ArtemGovorov Nov 26, 2015

Member

@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.

Member

ArtemGovorov commented Nov 26, 2015

@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.

@ruswerner

This comment has been minimized.

Show comment
Hide comment
@ruswerner

ruswerner Nov 26, 2015

@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.

ruswerner commented Nov 26, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@ArtemGovorov

ArtemGovorov Nov 26, 2015

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}
  };
};

?

Member

ArtemGovorov commented Nov 26, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@ruswerner

ruswerner Nov 26, 2015

@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.

ruswerner commented Nov 26, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@ArtemGovorov

ArtemGovorov Nov 26, 2015

Member

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

Member

ArtemGovorov commented Nov 26, 2015

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

@ArtemGovorov

This comment has been minimized.

Show comment
Hide comment
@ArtemGovorov

ArtemGovorov May 3, 2016

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.

Member

ArtemGovorov commented May 3, 2016

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