|
| 1 | +--- |
| 2 | +name: Installation |
| 3 | +route: '/installation' |
| 4 | +--- |
| 5 | + |
| 6 | +# Installation |
| 7 | + |
| 8 | +## Getting started |
| 9 | + |
| 10 | +This module is distributed via [npm](https://www.npmjs.com/) which is bundled with |
| 11 | +[node](https://nodejs.org) and should be installed as one of your project's `devDependencies`: |
| 12 | + |
| 13 | +```sh |
| 14 | +# if you're using npm |
| 15 | +npm install --save-dev @testing-library/react-hooks |
| 16 | +# if you're using yarn |
| 17 | +yarn add --dev @testing-library/react-hooks |
| 18 | +``` |
| 19 | + |
| 20 | +### Peer Dependencies |
| 21 | + |
| 22 | +`react-hooks-testing-library` does not come bundled with a version of |
| 23 | +[`react`](https://www.npmjs.com/package/react) to allow you to install the specific version you want |
| 24 | +to test against. It also does not come installed with a specific renderer, we currently support |
| 25 | +[`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer) and |
| 26 | +[`react-dom`](https://www.npmjs.com/package/react-dom), you only need to install one of them. For |
| 27 | +more information see [Renderer](/installation#renderer) |
| 28 | + |
| 29 | +```sh |
| 30 | +npm install react@^16.9.0 |
| 31 | +npm install --save-dev react-test-renderer@^16.9.0 |
| 32 | +``` |
| 33 | + |
| 34 | +> **NOTE: The minimum supported version of `react`, `react-test-renderer` and `react-dom` is |
| 35 | +> `^16.9.0`.** |
| 36 | +
|
| 37 | +## Renderer |
| 38 | + |
| 39 | +React requires a rendering engine, typically when creating an application people use `react-dom`. |
| 40 | +When running tests, React still requires an engine. We currently support two different engines: |
| 41 | + |
| 42 | +- `react-test-renderer` |
| 43 | +- `react-dom` |
| 44 | + |
| 45 | +If you have both installed in your project, assuming you're using the default imports (see below) |
| 46 | +the library defaults to using `react-test-renderer`. This is because rtr is the defacto test |
| 47 | +renderer for `react-native` for tests and it enables hooks to be correctly tested that are written |
| 48 | +for both `react` & `react-native`. |
| 49 | + |
| 50 | +```js |
| 51 | +import { renderHook } from '@testing-library/react-hooks' |
| 52 | +``` |
| 53 | + |
| 54 | +> The auto detection function may not work if tests are bundled to run in the browser. |
| 55 | +
|
| 56 | +## Being specific |
| 57 | + |
| 58 | +If, however, for certain tests you want to use a specific renderer (e.g. you want to use `react-dom` |
| 59 | +for SSR) you can import the server module directly: |
| 60 | + |
| 61 | +```ts |
| 62 | +import { renderHook } from '@testing-library/react-hooks/server` |
| 63 | +``` |
| 64 | + |
| 65 | +We have the following exports available: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { renderHook, act } from '@testing-library/react-hooks' // will try to auto-detect |
| 69 | +
|
| 70 | +import { renderHook, act } from '@testing-library/react-hooks/dom' // will use react-dom |
| 71 | +
|
| 72 | +import { renderHook, act } from '@testing-library/react-hooks/native' // will use react-test-renderer |
| 73 | +
|
| 74 | +import { renderHook, act } from '@testing-library/react-hooks/server' // will use react-dom |
| 75 | +``` |
| 76 | +
|
| 77 | +## Testing Framework |
| 78 | +
|
| 79 | +In order to run tests, you will probably want to be using a test framework. If you have not already |
| 80 | +got one, we recommend using [Jest](https://jestjs.io/), but this library should work without issues |
| 81 | +with any of the alternatives. |
0 commit comments