create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

add a simple test runner?

Open threepointone opened this issue 10 years ago • 51 comments

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.

Thoughts?

Thanks for the great tool!

threepointone avatar Jul 22 '16 17:07 threepointone

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:

  • We’re probably not going to include anything for running tests in browser because of the complexity it involves.
  • We’re probably not going to include jsdom either.
  • Testing app logic with a simple Node runner using Jasmine (or what do people use these days?) seems like a good idea.

gaearon avatar Jul 22 '16 17:07 gaearon

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.

modernserf avatar Jul 22 '16 17:07 modernserf

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.

threepointone avatar Jul 22 '16 18:07 threepointone

Huge vote for Mocha + Enzyme + Sinon. This probably requires JSdom, at least according to Enzyme docs.

ffxsam avatar Jul 22 '16 18:07 ffxsam

I'm agree with Threepointone. Maybe PhantomJS with Mocha.

aaronplanell avatar Jul 22 '16 18:07 aaronplanell

@ffxsam Enzyme would keep things nice and simple.

Thanks adding it to the conversation!

selfup avatar Jul 22 '16 18:07 selfup

also headless chrome is round the corner, so that'll be a great solution with no 'dependencies'

threepointone avatar Jul 22 '16 18:07 threepointone

I'd be happy to help and/or answer any questions about using Enzyme here!

aweary avatar Jul 22 '16 18:07 aweary

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.

ffxsam avatar Jul 22 '16 18:07 ffxsam

@ffxsam Agree on Enzyme (it's great), and we wouldn't need jsdom if we do shallow rendering.

wdhorton avatar Jul 22 '16 18:07 wdhorton

jsdom is only required for mount and render, correct?

ffxsam avatar Jul 22 '16 18:07 ffxsam

Another vote for Enzyme, simplifies testing considerably, also the official React docs kind of recommend it now 😄

screen shot 2016-07-22 at 3 20 06 pm

tristanoneil avatar Jul 22 '16 19:07 tristanoneil

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.

gaearon avatar Jul 22 '16 19:07 gaearon

I'd vote Mocha then

wdhorton avatar Jul 22 '16 20:07 wdhorton

Personally I think a "no config" setup would make sense with Jasmine, as it has all the things you need (assertions, mocking, etc..) with no configs. Where as Mocha might require chai and other things.

AdamBrodzinski avatar Jul 22 '16 20:07 AdamBrodzinski

That's a good point, but based on what @gaearon said above, I don't think we're as concerned with the assertion library because it's not a build dependency. I'd still favor Mocha just because I think it's more commonly used, but I have no problem with Jasmine if that's the way we want to go.

wdhorton avatar Jul 22 '16 20:07 wdhorton

If many people will be using Enzyme, I think Mocha makes more sense and it's quite easy to set up. Enzyme was designed to be used with Mocha, though of course it can be used with any test-runner.

Enzyme+Jasmine is a separate repo, FYI.

ffxsam avatar Jul 22 '16 20:07 ffxsam

A few questions about conventions: Can we put the tests in a test folder on the same level as src? Should we include a template test, since there's a template component? Do people prefer explicit extensions on the test files (i.e. '.spec.js') or just run everything in the test folder?

I'm thinking something like:

my-app/
  src/
    App.js
    ...
  test/
    App[.spec?].js

wdhorton avatar Jul 22 '16 21:07 wdhorton

I'd err on the side of having __tests__ folders anywhere in the tree. It's very painful to write imports that go many folders up and then many folders down. Also colocating related code helps comprehension.

gaearon avatar Jul 22 '16 22:07 gaearon

Hi all,

I have no qualifications whatsoever, so feel free to completely ignore this comment.

I personally would advocate for considering anything ending with .test.js or .spec.js as a test, because it allows you to either put your tests next to your code (as I prefer to do), or throughout the tree in __tests__. It would also be really easy to look for tests in a tests directory on the same level as src as well.

ariporad avatar Jul 22 '16 22:07 ariporad