如何在茉莉花测试之间重置Flux商店?

时间:2015-04-23 15:50:09

标签: jasmine reactjs karma-runner karma-jasmine reactjs-flux

在具有业力的商店上运行测试时茉莉(而不是开玩笑),商店的状态在测试之间持续存在。每次测试重置状态的最佳方法是什么?

使用jest,每个测试都会自动在自己的环境中运行。 This post includes instructions关于如何清除业力测试之间的需求缓存(下面复制)以模仿开玩笑,但我似乎无法让它发挥作用。

//tests.webpack.js (suggested by linked article, modified to match my project structure)
// Create a Webpack require context so we can dynamically require our
// project's modules. Exclude test files in this context.
'use strict';

var projectContext = require.context('./path/to/js', true, /^(?!\.test).js$/);
// Extract the module ids that Webpack uses to track modules.
var projectModuleIds = projectContext.keys().map(function (module) {
  return String(projectContext.resolve(module));
});

beforeEach(function () {
  // Remove our modules from the require cache before each test case.
  projectModuleIds.forEach(function (id) {
    return delete require.cache[id];
  });
});

我当前的档案:

tests.webpack.js (current)
var context = require.context('./path/to/js', true, /\.test\.js$/);
context.keys().forEach(context);

不确定它是否有帮助,但这是我的测试代码的简化版本,显示了商店在测试之间持续存在的状态。

MissionStore.test.js
describe('MissionStore', function() {

  beforeEach(function() {
    this.MissionStore = rewire("./MissionStore");
    this.registeredCallback = this.MissionStore.__get__("registeredCallback");
  });

  // A bunch of initialization tests without data
  describe("set data", function(){
    beforeEach(function(){
        // Sets this.MissionStore.getMissions() to some sample data
        this.registeredCallback(fetchMissions); 
    });

    it('modifies mission data', function(){
        var mission = this.MissionStore.getMissions()[0];
        expect(mission.edit).not.toEqual(true);

        // modify mission to add an edit flag = true
        this.registeredCallback({
            actionType: MissionConstants.MISSION_TOGGLE_EDIT_FLAG,
            mission : mission
        });

        expect(this.MissionStore.getMissions()[0].edit).toEqual(true);
    });

    it('do something else', function(){
        expect(this.MissionStore.getMissions()[0].edit).not.toEqual(true); // Failing test
        console.log(this.MissionStore.getMissions()[0]);

        // prints {..., edit: true}
    });
  });
});

在'做其他事情'测试,我期待this.MissionStore状态在测试之间重置,因此编辑不再等于true。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您的git checkout testcommit file/to/restore RegExp应匹配每个以/^(?!\.test).js$/结尾但不包含.js的文件是错误的,它只会匹配.test.js。它应该是.js。请参阅Negative lookahead Regular Expression

所以我的猜测是/^(?!.*\.test\.js$).*\.js$/为空,并且您没有在projectModuleIds次来电之间使任何内容失效。