-
-
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
docs(test): add instruction on how to integrate test in pre-commit hook #2598
Closed
Closed
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f6b723b
docs(test): reorganize `Running Test` section
luftywiranda13 126089a
docs(test): add instruction on how to integrate test in pre-commit hook
luftywiranda13 9438203
Merge branch 'master' into chore/docs
luftywiranda13 89bc434
docs(test): use `cross-env`
luftywiranda13 f63c8cc
Merge branch 'master' into chore/docs
luftywiranda13 68f4d5c
Merge branch 'master' into chore/docs
luftywiranda13 96fa3ed
chore(docs): move VC & CI sections
luftywiranda13 6cec549
chore(docs): list `pre-commit` in TOC
luftywiranda13 5e42aea
chore(docs): move `pre-commit` after `continuous integration` section
luftywiranda13 d437cd8
docs: add `Useful Hooks` section
luftywiranda13 a0e0246
Merge remote-tracking branch 'origin/master' into pr/2598
luftywiranda13 a4983ac
docs: move `Formatting Code Automatically` under `Useful Hooks`
luftywiranda13 c16f277
docs: update link about `prettier` to point to CRA internal docs
luftywiranda13 1f29882
docs: move `Pre-Commit Hook` under `Useful Hooks`
luftywiranda13 2c662c1
docs: update heading
luftywiranda13 b0942ae
style: add whitespace
luftywiranda13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this right after |
||
|
||
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 | ||
|
||
|
@@ -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`. | ||
|
@@ -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: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.