You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An important target group for this app (imo) are beginners and prototype builders. From my experience, both these groups would benefit greatly from having simple test infrastructure bundled with this starter.
From my own experience, I usually have a script.js that I'd include into the main module, but that's pretty hacky until I finally setup (whatever flavor of test infra is popular). The value I get is from being able to write code that'll run on every edit-save separately from my app, but that later morphs into a unit test suite or so on. For beginners, they'd build their app, but write tests for smaller parts of the app they're building, while learning to write tests for their code.
To be clear, I'm not advocating any specific framework/suite/assert libs; I'd just like to be able to run even a if(true) throw new Error script on every change.
How about tape? It's super simple and it already almost works -- if you restrict yourself to the subset of ES6 that your version of node supports you can use it now; all one would need to get it working is running it in babel-node; i.e. react-scripts test would run babel-node src/test.js.
Reacted by Michael LaCroix, Kristian Roebuck, Dan Prince, Alex Bardas, Vince Speelman, Eric Elliott, Yuichi Hagio and Michael Terry
With you on zero config; running src/tests.js in a 'watch' mode on npm start (and once on npm run build?) would be good. You sure about not running it in a 'browser' though? Phantomjs works pretty nicely, and wouldn't need any config/ explicit invocation/require iirc.
Huge vote for Mocha + Enzyme + Sinon. This probably requires JSdom, at least according to Enzyme docs.
Reacted by Dan Thareja, Regis Boudinot, William Horton, Zach Stoltz, Thibaud Colas, Bill Brock, camwest, Charlie, Subir, Martin Svarrer Christensen and 13 more
Is PhantomJS really needed though? It seems a bit overkill, when Enzyme can easily simulate events on components. If I'm not mistaken, PhantomJS & Selenium are more for E2E testing, not unit testing.
Reacted by Scott Bronson, Vince Speelman and Dave Ceddia
To make it clear, the tool currently only abstracts build dependencies so it shouldn’t matter too much what library you use to make assertions / mount tree / etc. Choosing the runner and conventions is what we need to do.
I think what's important here is that we decrease the friction for creating tests as much as possible.
Elixir does this with all new projects and having that passing test ready to go makes me write 10x more tests compared to JS projects because frankly it's chore to get everything setup.
Reacted by Tristan O'Neil, dan, William Horton, Max Stoiber, Jake Demarest-Mays, Edo, Adam Brodzinski, Javier Villanueva, Ari Porad, Regis Boudinot and 2 more
I added a PR (#216) to show a basic test setup. I used Mocha, but the most important parts are framework-agnostic, and we should be able to easily swap in another framework if one pulls ahead significantly.
I think what's important here is that we decrease the friction for creating tests as much as possible.
I've gotten a lot of feedback from new React developers that setting up a test environment is a PITA. A production ready test environment that properly deals with CSS/images, mocking/stubbing/spys, shallow rendering, etc. is not a trivial thing to set up and would probably be very beneficial to abstract away.
Elixir does this with all new projects and having that passing test ready to go makes me write 10x more tests compared to JS projects because frankly it's chore to get everything setup.
rails new does this as well. I think an important aspect is to allow users to disable the 'default' test config so they can use their own if they're opinionated.
For example, often in rails projects I pass the -T flag to NOT use minitest and then set up RSpec myself b/c that is the framework I prefer.
Maybe a good compromise is to include the simplest testing setup possible that will be compatible with this project and then allow users to disable instead of enable the testing setup if they decide they want to use a different framework or something more complex. That way people are automatically opted-in to doing something they should be doing (think pit of success) and have to make an active choice to opt out of having a testing environment setup included in their new projects.
Reacted by Regis Boudinot, Pietro Giorgianni and Janis Krasemann
Jest aaaaalmost works out of the box with create-react-app if you install babel-jest, the only problem is that create-react-app doesn't put a .babelrc file in the root.
@mpj Does babel-jest handle non-js imports like import "foo.css" and import "foo.png"?
I am working on the react-scripts run as described by @ForbesLindesay & discussed in #218 . That should be useful for some quick-and-dirty testing and maybe some lighter-weight test frameworks can actually just be invoked through it.
I'm a big fan of TDD and testing which means I always want it to be easy for people to include tests or to learn more about writing tests.
If a BDD framework such as Mocha or Jasmine were included, it would be nice, however, it is kind of an extra.
I'm thinking back to the first React app I wrote at a Railsbridge (yes, it was Railsbridge, they do js, front-end workshops too). I was wrapping my head around building components and using a store instead of using templates and models. I didn't write any tests. Maybe it's not as important if this is a setup for an initial Hello Components! react app.
Also, there are a lot of opinions that come with testing. The one I see in this thread is folder structure. If I'm encouraging someone to do more testing, they need to be able to arrange their tests in a way that suits them.
My team downloaded create-react-app and found it pretty easy to add tests in a separate test folder. We had to include babel but it was nbd.
I'm with @timjacobi that it seems early to add in testing, but it's not too early for a pointer to anyone who wants to do a test setup. Also I hope people are encouraged to set up tests with this project and it makes me so happy to see this thread!
I'll add my two cents. Contrary to what I expected, it took me just a few minutes to setup mocha, enzyme, expect.js (mostly spent googling, as I had never used any of them).
The inconvenience is that I have two terminal tabs, one for create-react-app, the other for mocha watch. right now, for me this is already good enough; I'm willing to trade a little inconvenience for the ability to ditch, say, mocha for Jasmine whenever I see fit, or to use a single test dir instead of many __tests__ dirs—tastes, as they say.
I'm no beginner, though, and a beginner cannot be expected to know how to hack a testing solution on an existing project, or whether to use Mocha or Jasmine or Karma or whatnot.
So, my proposal:
have exactly one full testing solution nicely integrated (opt out) in create-react-app, with sample tests, pointers to docs, etc.—I have personally no real preferences here, I'm used to Jasmine but I'll use whatever works, they are all very similar after all.
leave room for the integration of alternative solutions—No idea how hard it would be!
have pointers to a few how to docs for other solutions.
This should help beginners to start using React with proper tests without hassle, and should also allow everyone to opt for a different testing setup with reasonable effort.
#250 was merged and should fulfill most of the wishes people expressed in this thread. If you'd like additional features, please feel free to create new issues and cc me on them :)
Activity
gaearon commentedon Jul 22, 2016
I agree we should ship a test runner (Jest, ava, mocha, whatever) as long as we can stick with “no configuration” as the guiding design principle.
Other thoughts:
modernserf commentedon Jul 22, 2016
How about tape? It's super simple and it already almost works -- if you restrict yourself to the subset of ES6 that your version of node supports you can use it now; all one would need to get it working is running it in babel-node; i.e.
react-scripts testwould runbabel-node src/test.js.threepointone commentedon Jul 22, 2016
With you on zero config; running
src/tests.jsin a 'watch' mode onnpm start(and once onnpm run build?) would be good. You sure about not running it in a 'browser' though? Phantomjs works pretty nicely, and wouldn't need any config/ explicit invocation/require iirc.ffxsam commentedon Jul 22, 2016
Huge vote for Mocha + Enzyme + Sinon. This probably requires JSdom, at least according to Enzyme docs.
aaronplanell commentedon Jul 22, 2016
I'm agree with Threepointone. Maybe PhantomJS with Mocha.
selfup commentedon Jul 22, 2016
@ffxsam Enzyme would keep things nice and simple.
Thanks adding it to the conversation!
threepointone commentedon Jul 22, 2016
also headless chrome is round the corner, so that'll be a great solution with no 'dependencies'
aweary commentedon Jul 22, 2016
I'd be happy to help and/or answer any questions about using Enzyme here!
ffxsam commentedon Jul 22, 2016
Is PhantomJS really needed though? It seems a bit overkill, when Enzyme can easily simulate events on components. If I'm not mistaken, PhantomJS & Selenium are more for E2E testing, not unit testing.
wdhorton commentedon Jul 22, 2016
@ffxsam Agree on Enzyme (it's great), and we wouldn't need jsdom if we do shallow rendering.
ffxsam commentedon Jul 22, 2016
jsdom is only required for
mountandrender, correct?tristanoneil commentedon Jul 22, 2016
Another vote for Enzyme, simplifies testing considerably, also the official React docs kind of recommend it now 😄
gaearon commentedon Jul 22, 2016
To make it clear, the tool currently only abstracts build dependencies so it shouldn’t matter too much what library you use to make assertions / mount tree / etc. Choosing the runner and conventions is what we need to do.
wdhorton commentedon Jul 22, 2016
I'd vote Mocha then
25 remaining items
AdamBrodzinski commentedon Jul 25, 2016
I think what's important here is that we decrease the friction for creating tests as much as possible.
Elixir does this with all new projects and having that passing test ready to go makes me write 10x more tests compared to JS projects because frankly it's chore to get everything setup.
gaearon commentedon Jul 25, 2016
Please file an issue if there’s something about that config that breaks enzyme.
wdhorton commentedon Jul 26, 2016
I added a PR (#216) to show a basic test setup. I used Mocha, but the most important parts are framework-agnostic, and we should be able to easily swap in another framework if one pulls ahead significantly.
SpencerCDixon commentedon Jul 26, 2016
I've gotten a lot of feedback from new React developers that setting up a test environment is a PITA. A production ready test environment that properly deals with CSS/images, mocking/stubbing/spys, shallow rendering, etc. is not a trivial thing to set up and would probably be very beneficial to abstract away.
rails newdoes this as well. I think an important aspect is to allow users to disable the 'default' test config so they can use their own if they're opinionated.For example, often in rails projects I pass the
-Tflag to NOT use minitest and then set up RSpec myself b/c that is the framework I prefer.Maybe a good compromise is to include the simplest testing setup possible that will be compatible with this project and then allow users to disable instead of enable the testing setup if they decide they want to use a different framework or something more complex. That way people are automatically opted-in to doing something they should be doing (think pit of success) and have to make an active choice to opt out of having a testing environment setup included in their new projects.
ariporad commentedon Jul 26, 2016
@gaearon: @wdhorton already fixed the bug in #216 here.
mpj commentedon Jul 27, 2016
Jest aaaaalmost works out of the box with create-react-app if you install babel-jest, the only problem is that create-react-app doesn't put a .babelrc file in the root.
lacker commentedon Jul 27, 2016
@mpj Does
babel-jesthandle non-js imports likeimport "foo.css"andimport "foo.png"?I am working on the
react-scripts runas described by @ForbesLindesay & discussed in #218 . That should be useful for some quick-and-dirty testing and maybe some lighter-weight test frameworks can actually just be invoked through it.cpojer commentedon Jul 28, 2016
A few people asked me to contribute a pull request with an initial Jest integration: #250
If you'd like to find out more about snapshot testing, please read our new blog post and documentation.
Marlena commentedon Jul 30, 2016
I'm a big fan of TDD and testing which means I always want it to be easy for people to include tests or to learn more about writing tests.
If a BDD framework such as Mocha or Jasmine were included, it would be nice, however, it is kind of an extra.
I'm thinking back to the first React app I wrote at a Railsbridge (yes, it was Railsbridge, they do js, front-end workshops too). I was wrapping my head around building components and using a store instead of using templates and models. I didn't write any tests. Maybe it's not as important if this is a setup for an initial Hello Components! react app.
Also, there are a lot of opinions that come with testing. The one I see in this thread is folder structure. If I'm encouraging someone to do more testing, they need to be able to arrange their tests in a way that suits them.
My team downloaded create-react-app and found it pretty easy to add tests in a separate test folder. We had to include babel but it was nbd.
I'm with @timjacobi that it seems early to add in testing, but it's not too early for a pointer to anyone who wants to do a test setup. Also I hope people are encouraged to set up tests with this project and it makes me so happy to see this thread!
giorgian commentedon Jul 30, 2016
I'll add my two cents. Contrary to what I expected, it took me just a few minutes to setup mocha, enzyme, expect.js (mostly spent googling, as I had never used any of them).
The inconvenience is that I have two terminal tabs, one for create-react-app, the other for mocha watch. right now, for me this is already good enough; I'm willing to trade a little inconvenience for the ability to ditch, say, mocha for Jasmine whenever I see fit, or to use a single
testdir instead of many__tests__dirs—tastes, as they say.I'm no beginner, though, and a beginner cannot be expected to know how to hack a testing solution on an existing project, or whether to use Mocha or Jasmine or Karma or whatnot.
So, my proposal:
This should help beginners to start using React with proper tests without hassle, and should also allow everyone to opt for a different testing setup with reasonable effort.
cpojer commentedon Aug 2, 2016
#250 was merged and should fulfill most of the wishes people expressed in this thread. If you'd like additional features, please feel free to create new issues and cc me on them :)
gaearon commentedon Sep 2, 2016
0.3.0 is out with support for testing.
Read the usage guide and the migration instructions.
💜
Merge pull request react#80 from zinserjan/fix-code-coverage