Mobx: Question: How to avoid multiple instances during karma test run?

Created on 14 Jul 2017  路  4Comments  路  Source: mobxjs/mobx

After upgrading to version 3.2.1 I receive a lot of warnings about multiple instances of mobx in my test logs:
[mobx] Warning: there are multiple mobx instances active. This might lead to unexpected results. See https://github.com/mobxjs/mobx/issues/1082 for details.

I read #1082 but I have no clue why multiple mobx instances are created during a test run. There are no warnings in development or production mode.

My test stack comprises webpack, karma and jasmine. Even with the tiniest example project with almost no configuration I can reproduce this behavior. Is this a known problem or are there some things I have to take care to avoid multiple instances during a test run?

Most helpful comment

Works, thanks a lot for your support.

All 4 comments

your current project may be import externals libraries, and those externals libraries has use "mobx".
so, mobx log [warning] multiple mobx instances active.

In my little example project I import no other dependencies except mobx and can reproduce the warnings.
You can see the example project here: https://github.com/dgrawunder/mobx-test

After cloning the project, you can run yarn install and yarn test and you will see the logged warnings.

I cloned your project "mobx-test"!
[mobx] Warning: there are multiple mobx instances active.
karma loads all code files that matched and run in the single window scope more than once.

I have a simple solution:
steps:

  • add webpack conf in "externals: ['mobx']" in webpack.config.js. make webpack do not compile the 'mobx' file and All compiled output files do not contain [mobx].
  • add mobx.js once in the main content scope.
    In "karma.conf.js" > files : options property add [mobx] libray.
    files: [
     // mobx library (notice: use umd)
      {pattern: 'node_modules/mobx/lib/mobx.umd.js', watched: false, served: true, included: true},

      // others code files
      {pattern: 'src/**/*.spec.@(js|jsx)', watched: false, served: true, included: true}

    ]

yarn test

Works, thanks a lot for your support.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FouMez picture FouMez  路  31Comments

sonaye picture sonaye  路  70Comments

mweststrate picture mweststrate  路  105Comments

creativefctr picture creativefctr  路  35Comments

jefffriesen picture jefffriesen  路  29Comments