I'm building an app with React, Facebook's Dispatcher and Flux Utils I'm using karma
, mocha
, sinon
, browserify
for testing Flux stores. Here is my snippet for testing:
import {expect} from 'chai';
import sinon from 'sinon';
describe('Store', function() {
let registerSpy, Store, registeredCallback, isDispatchingStub;
beforeEach(function(done) {
const Dispatcher = require('../../../dispatcher');
registerSpy = sinon.spy(Dispatcher, 'register');
Store = require('../store');
registeredCallback = registerSpy.lastCall.args[0];
isDispatchingStub
= sinon.stub(Dispatcher, "isDispatching", function() { return true; });
done();
});
afterEach(function(done) {
registerSpy.restore();
isDispatchingStub.restore();
done();
});
it('should update its state on message', function(done) {
registeredCallback({
type: 'MESSAGE',
payload: {
message: 'Our message'
}
});
let state = Store.getState();
expect(state.size).to.equal(1);
done();
});
});
Everything works fine if I have only one test executing. If I add another test I get an error since stores aren't reloaded when they are required (I don't get a fresh copy of the store, it already has some state after previous tests). So, registerSpy
doesn't have a lastCall property because dispatcher has already registered the store.
The same thing with Dispatcher, other stores can see dispatched actions, so, it is troublesome to test stores properly.
I've tried using rewire
but it doesn't have browserify support. I've also tried cancelling require.cache
but Browserify's require doesn't have cache property like node's one has.
How can I get a fresh copy of a module with browserify?