Skip to content
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

React recipe #747

Merged
merged 16 commits into from
Apr 27, 2016
Merged

React recipe #747

merged 16 commits into from
Apr 27, 2016

Conversation

adriantoine
Copy link
Contributor

Fixes #446

Hi!

Here is a first attempt to a React recipe. I'm not a native english speaker so, please carefully read the whole recipe and fix my grammar 😛 : https://github.com/adriantoine/ava/blob/master/docs/recipes/react.md

I might have forgotten something, please let me know!

Also, as a native french speaker, I'll be happy to make the translation to French once this is merged.

@thangngoc89
Copy link

I think you should add a note about adding babel-preset-react to AVA in order to use JSX inside test files

@adriantoine
Copy link
Contributor Author

@thangngoc89
Copy link

@adriantoine sorry. I missed that


## Setting up Babel

The first thing you need to do is to set up `babel` to transpile JSX code from the tests. To do that, I'd recommend the [babelrc recipe](https://github.com/sindresorhus/ava/blob/master/docs/recipes/babelrc.md) using `babel-preset-react`. You can also have a look at this sample project configuration: https://github.com/adriantoine/ava-enzyme-demo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linkify babel-preset-react and linkify sample project configuration instead of showing the full URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configuration => config

@sindresorhus
Copy link
Member

Nice work @adriantoine. This looks very promising :)

// @MoOx @kentcdodds @istarkov

@sindresorhus
Copy link
Member

Also, as a native french speaker, I'll be happy to make the translation to French once this is merged.

@forresst FYI ;)

@adriantoine
Copy link
Contributor Author

@sindresorhus thanks! I'll have a look at those comments

@MoOx
Copy link

MoOx commented Apr 11, 2016

Nice.

@MoOx
Copy link

MoOx commented Apr 11, 2016

I would add a note about how to play with webpack, loaders for assets etc by poiting users to this https://github.com/istarkov/babel-plugin-webpack-loaders#dynamic-config-path

@MoOx
Copy link

MoOx commented Apr 11, 2016

I would also add a note on the second solution to let user know that this one can "simulate" events too ;)

@kentcdodds
Copy link
Contributor

I would add a note about how to play with webpack

That's not really React specific. We should make that a recipe and reference it.

@adriantoine
Copy link
Contributor Author

@MoOx https://github.com/istarkov/babel-plugin-webpack-loaders#dynamic-config-path I think it should be added to the babelrc recipe, rather than the react one.


To see an example of AVA working together with Enzyme set up for browser testing, you can have a look at [this sample project](https://github.com/adriantoine/ava-enzyme-demo).

This is a basic example about how to integrate Enzyme with AVA, to get more information about using Enzyme for unit testing React component, please have a look at [Enzyme's documentation](http://airbnb.io/enzyme/).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with AVA, to get more information about => with AVA. For more information about

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have a look at => have a look at the

@adriantoine
Copy link
Contributor Author

Thanks @sindresorhus and @forresst, it's all fixed!

@adriantoine
Copy link
Contributor Author

adriantoine commented Apr 15, 2016

Another thing is, an update has been made to unexpected-react to make it work with React 15, I've created a repo to try it with AVA. It works well and I quite like the colourful JSX output:

unexpected-react output

compared to jsx-test-helpers:

unexpected-react output

On the other hand, you have to install unexpected and unexpected-react to your project which are both really big compared to jsx-test-helpers, so it might not suit everyone.

Should I mention unexpected-react just like expect-jsx has been mentioned? Is it worth an extra section? (considering expect-jsx doesn't have its own section)

@sindresorhus
Copy link
Member

I think it's worth mentioning.

@adriantoine
Copy link
Contributor Author

adriantoine commented Apr 18, 2016

I've updated the recipe with a new section for "Other assertion libraries" where I put expect-jsx and unexpected-react.

I need another round of review:
https://github.com/adriantoine/ava/blob/master/docs/recipes/react.md

Thanks!

@scarletsky
Copy link

Hello @adriantoine , I follow your react recipes with some problem.

It works as expected when your component is in the test file. But when you require the component from another file (some thing like import Foo from './Foo'), it will throw errors like #458.

Any guides ?

@MoOx
Copy link

MoOx commented Apr 21, 2016

You might need to use "babel": "inherit", see https://github.com/sindresorhus/ava#es2015-support

@scarletsky
Copy link

@MoOx
Yes, "babel": "inherit" works when I add "react" to my .babelrc file.

But I want to know why my config do not work.

// .babelrc
{
  "presets": ["es2015", "stage-1"],
  "plugins": [
    "transform-decorators-legacy"
  ],
  "env": {
    "development": {
      "presets": ["react-hmre"]
    }
  }
}

// package.json
{
  "ava": {
    "verbose": true,
    "files": [
      "tests/client/App.js"
    ],
    "require": [
      "babel-register",
      "babel-polyfill"
    ],
    "babel": {
      "babelrc": true,
      "presets": ["react"]
    }
  }
}

From the docs:

When specifying the Babel config for your tests, you can set the babelrc option to true. This will merge the specified plugins with those from your babelrc.

According to my understanding, my config should work too.
Do I miss anything ?

@MoOx
Copy link

MoOx commented Apr 21, 2016

Why don't you add react preset in your babelrc?

@scarletsky
Copy link

@MoOx Because I use babel in server side. In client side, I do something like:

// webpack.config.js
var babelrc = JSON.parse(
  fs.readFileSync('./.babelrc', 'utf8')
)
// .....
{
  test: /\.js$/,
  exclude: /node_modules/,
  loader: 'babel',
  query: Object.assign(
    {},
    babelrc,
    {
      presets: babelrc.presets.concat('react'),
      cacheDirectory: true
    }
  )
}

This works very well.

@scarletsky
Copy link

@MoOx Can you tell me why my prev config do not work ?

@MoOx
Copy link

MoOx commented Apr 22, 2016

Sorry I don't have the answer. I use a single babel config for browser and node (for tests I use babel-plugin-webpack-loaders)

@sindresorhus sindresorhus merged commit 4c69c79 into avajs:master Apr 27, 2016
@sindresorhus
Copy link
Member

Alright. Let's merge! Superb work on this one @adriantoine. I think a lot of people will benefit from the recipe :)

@sindresorhus
Copy link
Member

leo-clapping

sindresorhus added a commit that referenced this pull request Apr 27, 2016
@adriantoine
Copy link
Contributor Author

Great thanks! 😄

@forresst
Copy link
Contributor

Great @adriantoine

Also, as a native french speaker, I'll be happy to make the translation to French once this is merged.

@adriantoine It is with pleasure that I will review . Ping me !

@adriantoine
Copy link
Contributor Author

@forresst yep, I'm working on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recipe for testing with React
10 participants