docs/tests/README.md

105 строки
2.8 KiB
Markdown
Исходник Обычный вид История

## Tests
It's not strictly necessary to run tests locally while developing. You can
always open a pull request and rely on the CI service to run tests for you,
but it's helpful to run tests locally before pushing your changes to
GitHub.
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,
custom reporters for different types of test output, etc.
Heroku dev deps (#19431) * fix: req.csrfToken doesn't always exist (e.g. 500 page) * feat: update dockerfile and add nextjs to build * fix: run linter * move @babel deps -> dev deps * move webpack looking things from deps -> dev deps * move pa11y-ci to optional dep * explicitly include optional deps for pa11y * allow heroku dev deps to be installed * fix: update postcss module * fix: update dockerfile build * tmp: disable renderReact * see if another deploy is slower/faster * move a few more packages to devDeps * upgrade to package-lock v2 * use dayjs instead of date-fns * move cross-env to devDeps * remove unused 'del' package * commit husky precommit hooks * add hrtime to clone-for-build.js * Revert "add hrtime to clone-for-build.js" This reverts commit 70ee647bacce833f4ed2f621f62c63c1d85e5413. * update babel/eslint * fix: remove unused plugin * try a .slugignore * fix: heroku-postbuild to use npm run build * fix: i cannot spell dereferenced * add .next/cache to heroku cacheDirectories * test cached build * remove aws-sdk, see what breaks * move jest-puppeteer to optional deps * fix: update browser-test.yml to use newer node version * move jimp to optional dependencies * move puppeteer to optional dependencies * fix: ci optional include * fix: bad copy pasta * remove previous react experiment * update tests/README.md with note about optional deps * bump node test version back to 14 * convert package-lock back to v1 * fix: use node 15.x to leverage npm optional deps * fix: optional dep install * test: see what happens with heroku/nodejs-typescript buildpack * back to heroku/nodejs buildpack * move jest to optional * revert jest move * remove .slugignore * cleanup dockerfile, move xlsx-population to optional, add comment about optional deps * Update Dockerfile Co-authored-by: James M. Greene <JamesMGreene@github.com> Co-authored-by: James M. Greene <JamesMGreene@github.com>
2021-05-25 01:40:50 +03:00
### Install optional dependencies
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
Heroku dev deps (#19431) * fix: req.csrfToken doesn't always exist (e.g. 500 page) * feat: update dockerfile and add nextjs to build * fix: run linter * move @babel deps -> dev deps * move webpack looking things from deps -> dev deps * move pa11y-ci to optional dep * explicitly include optional deps for pa11y * allow heroku dev deps to be installed * fix: update postcss module * fix: update dockerfile build * tmp: disable renderReact * see if another deploy is slower/faster * move a few more packages to devDeps * upgrade to package-lock v2 * use dayjs instead of date-fns * move cross-env to devDeps * remove unused 'del' package * commit husky precommit hooks * add hrtime to clone-for-build.js * Revert "add hrtime to clone-for-build.js" This reverts commit 70ee647bacce833f4ed2f621f62c63c1d85e5413. * update babel/eslint * fix: remove unused plugin * try a .slugignore * fix: heroku-postbuild to use npm run build * fix: i cannot spell dereferenced * add .next/cache to heroku cacheDirectories * test cached build * remove aws-sdk, see what breaks * move jest-puppeteer to optional deps * fix: update browser-test.yml to use newer node version * move jimp to optional dependencies * move puppeteer to optional dependencies * fix: ci optional include * fix: bad copy pasta * remove previous react experiment * update tests/README.md with note about optional deps * bump node test version back to 14 * convert package-lock back to v1 * fix: use node 15.x to leverage npm optional deps * fix: optional dep install * test: see what happens with heroku/nodejs-typescript buildpack * back to heroku/nodejs buildpack * move jest to optional * revert jest move * remove .slugignore * cleanup dockerfile, move xlsx-population to optional, add comment about optional deps * Update Dockerfile Co-authored-by: James M. Greene <JamesMGreene@github.com> Co-authored-by: James M. Greene <JamesMGreene@github.com>
2021-05-25 01:40:50 +03:00
need to make sure optional dependencies are installed by running:
```sh
npm ci --include=optional
```
If you run into the error "Could not find expected browser (chrome) locally", you may need to install the expected chromium version manually with:
```
node node_modules/puppeteer/install.js
```
### 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
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.
```sh
npm run test-watch
```
### Running individual tests
You can run specific tests in two ways:
```sh
# 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
```
### 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:
```sh
npx next build
```
### Linting
To validate all your JavaScript code (and auto-format some easily reparable mistakes),
run the linter:
```sh
npm run lint
```
### Keeping the server running
When you run `jest` tests that depend on making real HTTP requests
to `localhost:4000`, the `jest` tests have a hook that starts the
server before running all/any tests and stops the server when done.
You can disable this, which might make it easier when debugging tests
since the server won't need to start and stop every time you run tests.
In one terminal, type:
```sh
NODE_ENV=test PORT=4000 node server.js
```
In another terminal, type:
```sh
START_JEST_SERVER=false jest tests/rendering/foo/bar.js
```
Or whatever the testing command you use is.
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
a server on `:4000` too.