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

docs(test): add instruction on how to integrate test in pre-commit hook #2598

Closed
wants to merge 16 commits into from
Closed
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ You can find the most recent version of this guide [here](https://github.com/fac
- [Running Tests](#running-tests)
- [Filename Conventions](#filename-conventions)
- [Command Line Interface](#command-line-interface)
- [Version Control Integration](#version-control-integration)
- [Writing Tests](#writing-tests)
- [Testing Components](#testing-components)
- [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries)
- [Initializing Test Environment](#initializing-test-environment)
- [Focusing and Excluding Tests](#focusing-and-excluding-tests)
- [Coverage Reporting](#coverage-reporting)
- [Version Control Integration](#version-control-integration)
- [Continuous Integration](#continuous-integration)
- [Pre-commit Hook](#pre-commit-hook)
- [Disabling jsdom](#disabling-jsdom)
- [Snapshot Testing](#snapshot-testing)
- [Editor Integration](#editor-integration)
Expand Down Expand Up @@ -1084,14 +1085,6 @@ The watcher includes an interactive command-line interface with the ability to r

![Jest watch mode](http://facebook.github.io/jest/img/blog/15-watch.gif)

### Version Control Integration

By default, when you run `npm test`, Jest will only run the tests related to files changed since the last commit. This is an optimization designed to make your tests run fast regardless of how many tests you have. However it assumes that you don’t often commit the code that doesn’t pass the tests.

Jest will always explicitly mention that it only ran tests related to the files changed since the last commit. You can also press `a` in the watch mode to force Jest to run all tests.

Jest will always run all tests on a [continuous integration](#continuous-integration) server or if the project is not inside a Git or Mercurial repository.

### Writing Tests

To create tests, add `it()` (or `test()`) blocks with the name of the test and its code. You may optionally wrap them in `describe()` blocks for logical grouping but this is neither required nor recommended.
Expand Down Expand Up @@ -1232,6 +1225,14 @@ Run `npm test -- --coverage` (note extra `--` in the middle) to include a covera

Note that tests run much slower with coverage so it is recommended to run it separately from your normal workflow.

### Version Control Integration

By default, when you run `npm test`, Jest will only run the tests related to files changed since the last commit. This is an optimization designed to make your tests run fast regardless of how many tests you have. However it assumes that you don’t often commit the code that doesn’t pass the tests.

Jest will always explicitly mention that it only ran tests related to the files changed since the last commit. You can also press `a` in the watch mode to force Jest to run all tests.

Jest will always run all tests on a [continuous integration](#continuous-integration) server or if the project is not inside a Git or Mercurial repository.

### Continuous Integration

By default `npm test` runs the watcher with interactive CLI. However, you can force it to run tests once and finish the process by setting an environment variable called `CI`.
Expand All @@ -1248,6 +1249,7 @@ Popular CI servers already set the environment variable `CI` by default but you
```
language: node_js
node_js:
- 4
- 6
cache:
directories:
Expand Down Expand Up @@ -1292,6 +1294,39 @@ The test command will force Jest to run tests once instead of launching the watc

The build command will check for linter warnings and fail if any are found.

### Pre-commit Hook

You can run tests against "staged" files before each Git commit by integrating the test script in pre-commit hook.
Copy link
Contributor

Choose a reason for hiding this comment

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

We just merged a very similar PR for Prettier commit hook. Should we unify those sections? We can leave non-hook descriptions in both and link to a section where both hooks are described with a common setup step. Can be called "Useful Hooks".

Copy link
Contributor Author

@luftywiranda13 luftywiranda13 Jun 28, 2017

Choose a reason for hiding this comment

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

Is it really needed to add docs about prettier in CRA? since there's no special step to do it in a CRA based project.
people can read through prettier docs then do exactly what the docs say, it'll just work. and IMO by merging #2006 will make CRA docs bloated day by day.

on the other hand, i think we should add docs & explain something which are specific/special in CRA based project like how to run test, storybook, coverage reporting, linting, etc. yeah basically just like what we did so far in our docs except for #2006.

what do you think? but if you think that it doesnt matter, i'll go on and do what you suggested here 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

We already added docs about Prettier (#2006 is merged). I'm referring to consolidating those two sections and removing duplication between them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, any suggestion on where to put the "Useful Hooks"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Put it somewhere you like, I'll take a look and think about it


First, install [husky](https://github.com/typicode/husky) & [lint-staged](https://github.com/okonet/lint-staged):
```sh
npm install --save-dev husky lint-staged
```

Because we don't need the tests to run in watch mode, we need to set `CI` environment variable to `true`. As described in section [Continuous Integration](#continuous-integration).

To make sure it's cross-platform, let's install [cross-env](https://github.com/kentcdodds/cross-env):
```sh
npm install --save-dev cross-env
```

Then add this config to `package.json`:
```
"scripts": {
...
"precommit": "lint-staged",
"test:staged": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"
},
"lint-staged": {
"src/**/*.js": [
"test:staged",
"git add"
]
}
```

This way, instead of running all tests, passing `--findRelatedTests` flag in test script will save our times a lot because Jest will run only the minimal amount of tests related to changes in your staging area.

### Disabling jsdom

By default, the `package.json` of the generated project looks like this:
Expand Down