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 8 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
45 changes: 40 additions & 5 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ 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)
- [Pre-commit Hook](#pre-commit-hook)
- [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)
- [Disabling jsdom](#disabling-jsdom)
- [Snapshot Testing](#snapshot-testing)
Expand Down Expand Up @@ -1084,13 +1085,38 @@ 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
### 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.

Seems like this should either be #### or become a new entry in table of contents.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's move this right after Continuous Integration section?


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.
You can run tests against "staged" files before each Git commit by integrating the test script in pre-commit hook.

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.
First, install [husky](https://github.com/typicode/husky) & [lint-staged](https://github.com/okonet/lint-staged):
```sh
npm install --save-dev husky lint-staged
```

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.
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.

### Writing Tests

Expand Down Expand Up @@ -1232,6 +1258,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 +1282,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