I am using rewire
to import my react/flux store for the sake of unit-test.
This is how the test file looks:
'use strict';
import {expect} from 'chai';
const rewire = require('rewire');
let SessionStore;
describe.only('Session Store', () => {
beforeEach(() => {
SessionStore = rewire('../../app/stores/SessionStore');
});
it('should ....', () => {
expect(1).to.equal(1);
});
});
When running the tests it shows me an error that the data
of undefined cannot be read:
TypeError: Cannot read property 'data' of undefined
at eval (eval at <anonymous> (path/tests/stores/SessionStore.js:2346:2), <anonymous>:1:546)
at eval (eval at <anonymous> (path/tests/stores/SessionStore.js:2346:2), <anonymous>:1:677)
at Object.eval (eval at <anonymous> (path/tests/stores/SessionStore.js:2346:2), <anonymous>:177:9)
at Object.eval (eval at <anonymous> (path/tests/stores/SessionStore.js:2346:2), <anonymous>:178:30)
at Object.<anonymous> (path/tests/stores/SessionStore.js:2346:2)
at rewire (eval at <anonymous> (path/tests/stores/SessionStore.js:2340:2), <anonymous>:10:35)
at Context.eval (eval at <anonymous> (path/tests/stores/SessionStore.js:535:2), <anonymous>:50:20)
Does anyone know how to solve the issue?
PS. for more info this is the store that I am using:
SessionStore.dispatchToken = AppDispatcher.register(function(payload) {
const action = payload.action;
switch (action.type) {
case ActionTypes.REQUEST_SESSION:
_isDatafetching = true;
SessionStore.emitChange();
break;
case ActionTypes.REQUEST_SESSION_SUCCESS:
_isRequested = true;
_isDatafetching = false;
_isSessionValid = true;
_currentUserId = action.response.data.user_id;
SessionStore.emitChange();
break;
case ActionTypes.REQUEST_SESSION_ERROR:
_isRequested = true;
_isSessionValid = false;
SessionStore.emitChange();
break;
default:
// noop
}
});
export default SessionStore;
I went to the same error message.
I write my code in ES6 and run tests with karma.
All rewire modules I was using doesn't solve the error.
I finally install babel-rewire plugin (https://www.npmjs.com/package/babel-rewire-plugin) and after configuration in webpack, all went well.