Reputation: 21
TypeError: Cannot create property '__mobxInstanceCount' on boolean 'true'
at Object.<anonymous> (node_modules/mobx/lib/mobx.js:2620:38)
at Object.<anonymous> (src/Stores/DashboardStore.ts:9:14)
at Object.<anonymous> (src/__tests__/DashboardStore.test.ts:3:24)
Test Suites: 1 failed, 1 total
mobx.js:2620 mentions about global multiple instance.
Dashboard.test.ts import DashboardStore from '../Stores/DashboardStore';
describe('DashboardStore', () => {
let dashboardStore;
beforeEach(() => {
dashboardStore = DashboardStore;
});
// tslint:disable-next-line:no-console
console.log(dashboardStore);
});
FAIL src\__tests__\DashboardStore.test.ts
? Test suite failed to run
My request is provide some hint on how to unit test a component and store. Thanks you.
Upvotes: 2
Views: 374
Reputation: 937
I don't know why, for me it was a problem in jest configuration file, when I tried to declare window as global.
Upvotes: 1
Reputation: 11661
It looks like your code imports mobx
multiple times (looking at where this error comes from).
The messages seems to originate here out of the source: https://github.com/mobxjs/mobx/blob/master/src/core/globalstate.ts
The fail only triggers if __mobxInstanceCount is already set which means 2 different files globalstate.ts are included (maybe 2 different versions of mobx?)
Im not sure about jest, but with mocha the tests can run at the same time.
This means that the beforeEach does in this case not always ensure a clean dashboardStore variable. (beoreEach could run twice and then the 2 tests can run afterwards, giving issues, because there is only dashboardStore variable and is overwritten on every beforeEach()
call).
If you really want to make unit tests for your react components. i'd advise to abstract away from directly using the mobx store by using the @inject function instead: https://github.com/mobxjs/mobx-react#inject-as-function
This way you don't need to mock any mobx Store, but only need to insert the needed props in the react components. (keeping your components more declarative in what they really need.)
Upvotes: 0