Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dgrawunder opened this issue Jul 14, 2017 · 4 comments
Closed

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

dgrawunder opened this issue Jul 14, 2017 · 4 comments

Comments

@dgrawunder
Copy link

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?

@zonebond
Copy link

zonebond commented Jul 24, 2017

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

@dgrawunder
Copy link
Author

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.

@zonebond
Copy link

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

@dgrawunder
Copy link
Author

Works, thanks a lot for your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants