-
-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add a simple test runner? #80
Comments
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:
|
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. |
With you on zero config; running |
Huge vote for Mocha + Enzyme + Sinon. This probably requires JSdom, at least according to Enzyme docs. |
I'm agree with Threepointone. Maybe PhantomJS with Mocha. |
@ffxsam Enzyme would keep things nice and simple. Thanks adding it to the conversation! |
also headless chrome is round the corner, so that'll be a great solution with no 'dependencies' |
I'd be happy to help and/or answer any questions about using Enzyme here! |
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 Agree on Enzyme (it's great), and we wouldn't need jsdom if we do shallow rendering. |
jsdom is only required for |
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'd vote Mocha then |
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. |
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. |
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. |
A few questions about conventions: I'm thinking something like:
|
I'd err on the side of having |
Hi all, I have no qualifications whatsoever, so feel free to completely ignore this comment. I personally would advocate for considering anything ending with |
I agree that imports begin to look ugly. In past projects i've usually used webpack's resolve.root feature to resolve imports from the src/ folder as well. This could be a solution |
Is there any chance at using Karma as the test runner? It is fairly agnostic to assertion libraries, has a rich plugin ecosystem for everything, supports console (via PhantomJS, SlimerJS, jsdom) and browser testing. The major downside is not way for test splitting/parallelism/concurrency. My concern would be that providing no support for browser testing would make this less useful for the majority of application (but perhaps less so for library authors). Not everything can, or should, be shallowlyy rendered or run in jsdom. |
I am not sure that we should ship a test runner. We need to avoid "boilerplate disease" where we add more dependencies because a feature is useful, but many people don't like those dependencies and would prefer to customize it, so in the end most people dislike that boilerplate. Maybe it would be possible to have some addon system like |
Ava is as simple as tape, with an unopinionated design that could accommodate any kind of project. In combination with nyc, code coverage could also be measured without hassle. |
This ends up being a really large decision, considering that whatever test framework is chosen, we end up essentially anointing it as official and as a result encourage its use in the community. While there are certainly cases of framework tooling that was eventually dropped in favor of something better(minitest => rspec), that is the exception, and not the rule. So in my eyes, a testing framework that is forward looking and encourages the kinds of design principles that the core team sees as the way forward for React should be respected. I believe this points towards something like Ava, because of how it encourages a more functional style(which in turn is easier to keep concurrent), much like how Redux is encouraging such a paradigm. |
Agreed. I think as long as it’s unobtrusive it could work though. I’m really interested in how well new snapshot testing works in Jest btw. It could plausibly be a good default for React apps. |
Perhaps extending the
This allows the tool to still retain its simplicity. Some beginners may not be writing any tests yet so it may be best to avoid having them learning a testing framework as well. |
I'd like to make an argument against including any test framework in this. Everyone has an opinion on which test framework works best and most of them are valid. I feel like the scope of this package is perfect at the moment. Yes it is opinionated to some extend but not in a too invasive fashion.
If we include a test framework we dictate how the user should write their tests. We might then go ahead and let the user choose which test framework they want to use by adding a selection dialog in the CLI but at that point the user might as well use a Yeoman generator. Also adding your favorite test runner really doesn't take long. I think we should focus on optimizing what is currently there so we can guarantee highest possible quality and best user experience. In short: Let's leave this package un-opinionated around testing and let the user decide. |
I'm kinda 👍 for what @timjacobi said, adding Mocha to a generated app is a single command and an entry in a JSON file: npm install mocha {
"scripts": "mocha *.test.js"
} |
It's not so simple, you'd have to configure it to be compatible with our set of Babel plugins. |
Originally I was excited to include testing support but @timjacobi makes a very compelling argument that has changed my opinion. I think it's almost impossible to include testing without forcing the user into a specific set of tools/config which they may not want. Additionally I agree that the primary use case for this is for new users who aren't likely to be experimenting with testing (as much as we all wish they would! :P) simply because the react learning curve is the first thing people are looking to solve. I think the 'eject' feature is where this project really shines for users who want to do things like set up automated testing. |
Actually, most popular testing framworks are following the |
The difficulty with expecting users to do this themselves is that it requires running babel and (to some extent) webpack. We make that even harder by hiding the babel and webpack configs from the user. I think the least opinionated thing we can do here is provide a way to run a JavaScript file (or set of JavaScript files) in node.js with webpack + babel done for the user. This would mean providing something like |
That sounds very sensible @ForbesLindesay! |
@timjacobi I disagree with your reasoning about how opinionated this project already is. Despite you saying that it shouldn't tell you how to write your code, it already includes ESLint, a tool whose only purpose is to tell you how to write your code. And babel also dictates how you can write your code, based on the specific plugins that have been chosen. Similar to the Mocha/Ava/Karma/Jest debate we're having here, there are bundlers besides Webpack that are commonly used by JS developers, like Browserify and Rollup, and yet @gaearon chose to use one of those over all the others. (For the record, I love Webpack and think it was the right choice.) So I don't really see how choosing a test runner to include would go beyond the bounds of what has already been established by this project.
@ForbesLindesay This might be a good idea, but it doesn't really solve the problem for anybody who wants to use a test runner. For example, mocha runs with its own CLI, so even if you had this set up to handle the webpack + babel stuff, you wouldn't be able to do |
@wdhorton I never said it doesn't dictate you how to write your code. I said it wasn't too invasive in doing so. Perhaps I should have been more clear about the framework aspect. If you introduce a test framework you dictate the user to use it. Babel doesn't dictate you to use it since you can still write ES5 code an it will happily transpile it for you even though this arguably turns into a no-op. Same for Webpack. No one forces you to run |
Having webpack, and eslint is fine in my opinion. The user is not really forced to learning anything extra on that end. There's no additional API/frameworks they need to learn. It's just JavaScript in the end and the only framework they require to learn is React. I'm going to have to agree with @timjacobi on not including any testing framework. Adding these testing framework can be beneficial but that requires a new user to learn those frameworks on top of learning React itself. We should be asking who the target audience is? Should we assume users to already know a testing framework? If so, should we assume they know x framework or y framework? |
Doesn’t “require”, you can keep writing your app without any tests. However most apps need at least some tests pretty soon.
Knowledge of testing frameworks is super portable because most just use |
I personally vote for a test runner (probably mocha) to be included, but, at least as an interim step, we should make sure that the webpack config is enzyme compatible. |
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. |
Please file an issue if there’s something about that config that breaks enzyme. |
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'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.
For example, often in rails projects I pass the 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. |
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 I am working on the |
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. |
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 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. |
#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 :) |
0.3.0 is out with support for testing. 💜 |
Fix code-coverage reports for ts/tsx
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!
The text was updated successfully, but these errors were encountered: