flux icon indicating copy to clipboard operation
flux copied to clipboard

Advice: Best way to test in a browser?

Open elicwhite opened this issue 11 years ago • 6 comments

I understand that Facebook has released Jest for testing Flux and React applications. From my understanding Jest runs the unit tests in a node based environment which is ideal when you want to make sure your code works in isomorphic situations so it can be rendered on a server.

I don't have the requirement for server rendered react, and do have a lot of browser code that I want to interact with via flux / react and I'm trying to figure out good ways to accomplish this. Do you guys have any recommendations of approaches?

The main issue I have is being able to get a single store's register call to the dispatcher since I won't be able to use Jest and its beautiful mock all by default implementation which keeps other stores from registering. What are some good approaches people have taken towards this problem?

elicwhite avatar Feb 13 '15 06:02 elicwhite

Jest uses Node so that we can run tests from the command line. I don't think there is any other reason beyond that. We aren't doing much isomorphic Flux with Node at Facebook yet. We might be doing some server-side rendering later, with Relay, but we're not doing that with Flux.

If you want to use promises, for example, which a browser might support but Node does not, you would probably want to add a polyfill and then provide a mock implementation of the polyfill. Alternatively, you could simply tell Jest not to mock the polyfill, but you might experience some async weirdness in the test in that case.

mock return values: https://facebook.github.io/jest/docs/api.html#mockfn-mockreturnvalue-value mock implementations: https://facebook.github.io/jest/docs/api.html#mockfn-mockimplementation-fn mock timers: https://facebook.github.io/jest/docs/timer-mocks.html#content don't mock, if you really need it: https://facebook.github.io/jest/docs/api.html#jest-dontmock-modulename nuke jest automocking: https://facebook.github.io/jest/docs/api.html#jest-automockoff

@jeffmo would know more about using Jest with promises (or other browser-specific features), and I think asking this question in Jest repo would be appropriate.

fisherwebdev avatar Feb 23 '15 07:02 fisherwebdev

Thanks for that @fisherwebdev. That gives me some good direction for the questions we talked about a while back and I should definitely take that conversation deeper on the Jest repo.

In this case I was wondering if you guys have seen or have any suggestions for testing a Flux app in one of the other classic testing frameworks (mocha, jasmine, qunit, etc), but still being able to get a reference to the specific store under test's dispatcher callback without it being muddled by the other store's in your app registering themselves to the dispatcher.

elicwhite avatar Feb 23 '15 07:02 elicwhite

@fisherwebdev FYI, Node 0.12 and iojs both have native promises, though bluebird can usually be faster. In General iojs will be in close lockstep with V8 in terms of JavaScript features.

tracker1 avatar Feb 23 '15 21:02 tracker1

Ooh, good to know. Lets ignore Promises for now though, that seems to be distracting from the actual issue. :)

Perhaps a better example is that we are adding some react/flux examples to a large non-react/non-flux code base that has a lot of other DOM manipulation stuff. We don't want to split out into two different test frameworks, so we are required to test in the browser with everything else. :)

elicwhite avatar Feb 23 '15 22:02 elicwhite

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

It appears like this is mostly answered. Feel free to re-open if there are more questions here.

kyldvs avatar Nov 18 '15 21:11 kyldvs