2020-09-27 15:10:11 +03:00
|
|
|
## Tests
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
It's not strictly necessary to run tests locally while developing. You can
|
2020-09-27 15:10:11 +03:00
|
|
|
always open a pull request and rely on the CI service to run tests for you,
|
2022-07-19 06:01:26 +03:00
|
|
|
but it's helpful to run tests locally before pushing your changes to
|
2020-09-27 15:10:11 +03:00
|
|
|
GitHub.
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
Tests are written using [jest](https://ghub.io/jest), a framework maintained
|
|
|
|
by Facebook and used by many teams at GitHub.
|
|
|
|
Jest provides everything: a test runner, an assertion library, code coverage analysis,
|
2021-01-05 19:25:14 +03:00
|
|
|
custom reporters for different types of test output, etc.
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2021-05-25 01:40:50 +03:00
|
|
|
### Install optional dependencies
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
We typically rely on CI to run our tests, so some large test-only
|
|
|
|
dependencies are considered **optional** (for example, puppeteer). To run the tests locally, you'll
|
2021-05-25 01:40:50 +03:00
|
|
|
need to make sure optional dependencies are installed by running:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
npm ci --include=optional
|
|
|
|
```
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
If you run into the error "Could not find expected browser (chrome) locally", you may need to install the expected chromium version manually with:
|
2021-05-28 01:17:27 +03:00
|
|
|
```
|
|
|
|
node node_modules/puppeteer/install.js
|
|
|
|
```
|
|
|
|
|
2020-09-27 15:10:11 +03:00
|
|
|
### Running all the tests
|
|
|
|
|
|
|
|
Once you've followed the development instructions above, you can run the entire
|
|
|
|
test suite locally:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
script/test # or `npm test`
|
|
|
|
```
|
|
|
|
|
|
|
|
### Watching all the tests
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
You can run a script that continually watches for changes and
|
|
|
|
re-runs the tests whenever a change is made. This command notifies you
|
|
|
|
when tests change to and from a passing or failing state, and it prints
|
|
|
|
out a test coverage report so you can see what files need testing.
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
```sh
|
|
|
|
npm run test-watch
|
|
|
|
```
|
|
|
|
|
2021-08-26 02:19:50 +03:00
|
|
|
### Running individual tests
|
2020-09-27 15:10:11 +03:00
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
You can run specific tests in two ways:
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
```sh
|
2021-08-26 02:19:50 +03:00
|
|
|
# The TEST_NAME can be a filename, partial filename, or path to a file or directory
|
|
|
|
npm test -- <TEST_NAME>
|
|
|
|
|
|
|
|
NODE_OPTIONS=--experimental-vm-modules npx jest tests/unit
|
2020-09-27 15:10:11 +03:00
|
|
|
```
|
|
|
|
|
2021-08-26 02:19:50 +03:00
|
|
|
### Failed Local Tests
|
|
|
|
|
|
|
|
If the tests fail locally with an error like this:
|
|
|
|
|
|
|
|
`Could not find a production build in the '/Users/username/repos/docs-internal/.next' directory.`
|
|
|
|
|
|
|
|
You may need to run this before every test run:
|
2020-09-27 15:10:11 +03:00
|
|
|
|
|
|
|
```sh
|
2021-08-26 02:19:50 +03:00
|
|
|
npx next build
|
2020-09-27 15:10:11 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Linting
|
|
|
|
|
|
|
|
To validate all your JavaScript code (and auto-format some easily reparable mistakes),
|
|
|
|
run the linter:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
npm run lint
|
|
|
|
```
|
2022-03-19 00:46:07 +03:00
|
|
|
|
|
|
|
### Keeping the server running
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
When you run `jest` tests that depend on making real HTTP requests
|
2022-03-19 00:46:07 +03:00
|
|
|
to `localhost:4000`, the `jest` tests have a hook that starts the
|
2022-07-19 06:01:26 +03:00
|
|
|
server before running all/any tests and stops the server when done.
|
2022-03-19 00:46:07 +03:00
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
You can disable this, which might make it easier when debugging tests
|
2022-03-19 00:46:07 +03:00
|
|
|
since the server won't need to start and stop every time you run tests.
|
|
|
|
|
2022-07-19 06:07:56 +03:00
|
|
|
In one terminal, type:
|
2022-03-19 00:46:07 +03:00
|
|
|
|
|
|
|
```sh
|
2022-07-26 20:53:23 +03:00
|
|
|
NODE_ENV=test PORT=4000 node server.js
|
2022-03-19 00:46:07 +03:00
|
|
|
```
|
|
|
|
|
2022-07-19 06:07:56 +03:00
|
|
|
In another terminal, type:
|
2022-03-19 00:46:07 +03:00
|
|
|
|
|
|
|
```sh
|
|
|
|
START_JEST_SERVER=false jest tests/rendering/foo/bar.js
|
|
|
|
```
|
|
|
|
|
2022-07-19 06:01:26 +03:00
|
|
|
Or whatever the testing command you use is.
|
2022-07-19 06:07:56 +03:00
|
|
|
|
2022-07-26 11:23:38 +03:00
|
|
|
The `START_JEST_SERVER` environment variable needs to be set to `false`, or else `jest` will try to start
|
2022-03-19 00:46:07 +03:00
|
|
|
a server on `:4000` too.
|