docs/tests
Peter Bengtsson eae2903419
don't re-read content pages that don't exist (#32164)
2022-10-28 20:46:22 +00:00
..
browser Switch search backend from Lunr to Elasticsearch (#31257) 2022-10-04 19:04:47 +00:00
content optimize the getCurrentProductTree function (#31620) 2022-10-27 20:13:00 +00:00
fixtures [2022-10-07]: Security Features Enablement Enterprise-Level Policies - [GA] (#31406) 2022-10-07 20:39:32 +00:00
graphql fix: remove usage of readFileAsync (#28053) 2022-06-01 14:49:30 +00:00
helpers Migrate links check to JS pattern (#30175) 2022-10-17 21:47:21 +00:00
linting Add linter for personal access token literals (#31824) 2022-10-24 19:50:36 +00:00
meta Update `site-policy-reminder` action to include info about syncing and merging PRs (#30809) 2022-09-20 11:06:25 -07:00
rendering optimize the getCurrentProductTree function (#31620) 2022-10-27 20:13:00 +00:00
routing Revert "set max-age=0 when you want no cache (#31657)" 2022-10-12 13:10:34 -07:00
translations Revert "set max-age=0 when you want no cache (#31657)" 2022-10-12 13:10:34 -07:00
unit don't re-read content pages that don't exist (#32164) 2022-10-28 20:46:22 +00:00
README.md Merge branch 'main' into SmolParascythe-update-test-README 2022-07-26 19:31:10 -05:00

README.md

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

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 need to make sure optional dependencies are installed by running:

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:

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.

npm run test-watch

Running individual tests

You can run specific tests in two ways:

# 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:

npx next build

Linting

To validate all your JavaScript code (and auto-format some easily reparable mistakes), run the linter:

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:

NODE_ENV=test PORT=4000 node server.js

In another terminal, type:

START_JEST_SERVER=false jest tests/rendering/foo/bar.js

Or whatever the testing command you use is.

The START_JEST_SERVER environment variable needs to be set to false, or else jest will try to start a server on :4000 too.