0

So I'm testing a component where I need it to act with my store to get the correct testable behavior. I've read that, since the "View" requires the store to function, I need to not mock the store, and also to not mock the "object-assign" thingy.

Excerpts from test code:

jest.dontMock('object-assign');
jest.dontMock('../StationSearch.jsx');
jest.dontMock('../../stores/StationStore.js');

describe('StationSearch', function() {

var StationSearch;
var stationSearch;
var React;
var TestUtils;

beforeEach(function() {
    React = require('react/addons');
    TestUtils = React.addons.TestUtils;
    StationSearch = require('../StationSearch.jsx');
    stationSearch = TestUtils.renderIntoDocument(<StationSearch />);
});

it('is rendered without value by default', function() {
    // code here
}
);

And some code from the View:

var React = require('react');
var StationStore = require('../stores/StationStore');

var StationSearch = React.createClass({

  componentDidMount: function() {
    StationStore.addChangeListener(this._onChange);
  },

});

Now, when running my tests, I get

TypeError: Cannot call method 'addChangeListener' of undefined

...on my change listener line, despite that I (think that I) am doing the right things when it comes to "not mocking" things.

Anyone has a clue as to why I could be getting this, still?

joakimnorberg
  • 655
  • 3
  • 10
  • 16
  • It looks like StationStore is undefined in your view file, are you sure there is a StationStore.js where you are calling it (1 level up, into stores, and the filename would be StationStore.js) if that file is located there, check the file and make sure it has module.exports or exports set to something. I also noticed that in your test code, for the StationStore you go up twice instead of once for the file location. Showing your file structure might help us solve the problem. – Ryan Britton May 18 '15 at 15:01
  • Yeah, the file is fine - as my application is working as it should. It's just the tests that have issues. – joakimnorberg May 18 '15 at 15:56

1 Answers1

0

probably late but...

the store is undefined because object-assign is being mocked. unmock it with one of the following methods

1) add this line to the top of your jest test file

jest.dontMock('object-assign');

2) add this to your package.json file

"jest": {
    "unmockedModulePathPatterns": [
      "object-assign" 
    ]
  },
Simon
  • 1,681
  • 1
  • 21
  • 34