6

I'm encountering a fun little problem with unit testing and the flux data stores.

Since the data stores are singletons that only get instantiated once (when the module is imported) any changes you make in your unit test persist.

This can (and is) causing me all sorts of head aches.

The solution I'm currently implementing is a reset method on each store which I run within the afterEach but I was wondering / hoping there's a simpler way to go around this?

Guy Nesher
  • 1,385
  • 2
  • 14
  • 26

1 Answers1

4

Require your dispatcher, store and get the reference to the callback all within a beforeEach(). This blows away the old stuff and gives you fresh stuff for each test.

Example of this is in the blog post: http://facebook.github.io/react/blog/2014/09/24/testing-flux-applications.html#putting-it-all-together

fisherwebdev
  • 12,658
  • 4
  • 28
  • 27
  • Cheers for that, I knew I must be missing something – Guy Nesher Oct 22 '14 at 21:07
  • 1
    @fisherwebdev I presume that's a feature of Jest, because my understanding of common js modules says that this would still be the same instance? – Tom Nov 13 '14 at 01:39