如何使用browserify重新加载模块

时间:2015-11-22 07:58:53

标签: reactjs mocha browserify sinon flux

我正在使用React,Facebook&DispatcherFlux Utils使用karmamocha,{{{{}建立一个应用程序1}},sinon用于测试Flux商店。这是我的测试片段:

browserify

如果我只执行一个测试,一切正常。如果我添加另一个测试,我会收到一个错误,因为商店在需要时没有重新加载(我没有获得商店的新副本,它在之前的测试后已经有了一些状态)。因此,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(); }); }); 没有lastCall属性,因为调度程序已经注册了商店。 与Dispatcher相同的是,其他商店可以看到调度的操作,因此,正确测试商店很麻烦。

我已尝试使用registerSpy,但它没有浏览器支持。我还尝试取消rewire,但浏览器要求没有像节点那样的缓存属性。

如何使用browserify获取模块的新副本?

1 个答案:

答案 0 :(得分:0)

所以,我尝试了很多东西。 首先,我意识到我不需要浏览器来测试商店,他们不会操作DOM。所以,我使用删除require.cache的解决方案。但是,就像提到here一样,它可能导致依赖关系之间的无限循环等问题。我的测试发生的第一件事就是无限循环:)。

This answer还表示可以在函数中包装功能。

我一直在从here等商店文件中导出实例。我从商店导出了类,并在测试文件中创建了一个实例。与调度员一样的事情。所以现在,我在每个测试中都有新的清洁存储和调度程序。

商店示例(现在我导出商店类和实例):

"No object name specified textScrollList"

测试文件(现在我从flux utils获得了干净的调度程序。每次测试都会创建一个新的商店,因此每个测试都有自己的商店。)

import {ReduceStore} from 'flux/utils';
import Immutable from 'immutable';
import Dispatcher from '../../dispatcher';

export class AppMessageStore extends ReduceStore {

  getInitialState() {
    return Immutable.List();
  }

  reduce(state, action) {
    switch (action.type) {
      case 'APP_MESSAGE':
        return Immutable.List([action.payload.message]);

      default:
        return state;
    }
  }

}

const instance = new AppMessageStore(Dispatcher);
export default instance;