interactions between tests running in mocha #5

jfischl opened this Issue Mar 12, 2012 · 8 comments


None yet
4 participants

jfischl commented Mar 12, 2012

I have a large suite of independent tests that all get run in a single process by mocha. It turns out that different mockery mocks are used in different test cases and this produces a situation where the order that the tests run causes problems since the first mock will still be loaded and cached when the second test runs that uses the same module. (I'm sure that was as clear as mud).

It seems to me that a possible solution to this would be to have the behavior of mockery.registerAllowable(module, true) get run on every module that had a registerMock called on it when deregisterAll gets called. Would that work?

This is an issue I struggle with often.

jfischl commented Mar 13, 2012

We've decided to take and modify it to meet our requirements for mocking. Since horaa doesn't mess around with the node module system, we should be able to get it to do what we want here.

Curious, what's the modification of horaa? I had tried it before but ran into some limitation.. I can't remember what it was though.

Also, sinon does pretty much the same thing as horaa, but with stub/mock abilities as well. I've found that I can do most things with sinon that I can with mockery, but without messing with module paths. The limitation of sinon, however, is that you can't just replace an entire module with a fake one (unless you stub a method that in turn requires the module), which makes it difficult to mock a class. I.e., I don't think you can stub "new MyClass" and have it return a fake object. If you can, I'd love to hear how. My point though is that I think this is the same limitation I ran into with horaa.

jfischl commented Mar 14, 2012

You are spot on!

Cool.. did you find a workaround for this with horaa?

beforeEach ->

afterEach ->

it 'does something', ->
  mockery.registerAllowable(moduleToTest, true)
  testMe = require moduleToTest

This code (coffee script), with combination of a patch I submitted that deletes the module from the cache when you require it, if its in allowable + unhook, should make all tests independent.

If you want to load 2 times a file that is not mocked, than you have to add it to mockery.registerAllowable(sth, true)


mfncooper commented Dec 29, 2012

The new "clean cache" option, available starting with mockery 1.4.0, is designed to address this issue. It lets you work with a clean module cache while mockery is enabled, so that you can effectively reset the cache between tests. See the docs for more. Feedback would be appreciated.

mfncooper closed this Jan 20, 2013

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