Unit tests - flux and data persistency
Asked Answered
F

1

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?

Flexuous answered 22/10, 2014 at 15:11 Comment(0)
R
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

Remonaremonetize answered 22/10, 2014 at 16:40 Comment(2)
Cheers for that, I knew I must be missing somethingFlexuous
@Remonaremonetize I presume that's a feature of Jest, because my understanding of common js modules says that this would still be the same instance?Purity

© 2022 - 2024 — McMap. All rights reserved.