docs/tests
mc fd0ff51c33
Update rendering test - remove hardcoded links to Dependabot article (#26765)
* update the article the tests use to something a bit more stable
2022-04-11 07:29:04 +00:00
..
browser automatically start server for jest (#26206) 2022-03-18 21:46:07 +00:00
content sitedata per translation (#26496) 2022-03-30 14:51:29 +00:00
fixtures Change product landing page `introLinks` to a general map of link titles to links (#26360) 2022-04-01 11:01:37 -05:00
graphql Remove `import x` statements (#20594) 2021-07-29 20:28:30 +00:00
helpers do rendering end-to-end tests with a real server (#26169) 2022-03-18 17:06:12 -04:00
javascripts React Cleanup: Documentation (#20517) 2021-07-27 09:07:18 -07:00
linting Version actions for GHES, use reusables (#26004) 2022-04-01 09:36:17 +10:00
meta Update supported-browsers.md (#26701) 2022-04-06 16:00:12 +00:00
rendering Update rendering test - remove hardcoded links to Dependabot article (#26765) 2022-04-11 07:29:04 +00:00
routing do rendering end-to-end tests with a real server (#26169) 2022-03-18 17:06:12 -04:00
translations Prevent creation of empty source files (#23634) 2021-12-13 16:57:38 +00:00
unit don't test external early-access in unit tests (#26612) 2022-04-04 21:00:14 +00:00
README.md automatically start server for jest (#26206) 2022-03-18 21:46:07 +00:00
package.json

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 sometimes it's helpful to run tests locally before pushing your changes to GitHub.

Test are written using jest, a framework maintained by Facebook and used by many teams at GitHub. Jest is convenient in that it 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 we consider some large test-only dependencies optional (for example, puppeteer). In order 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 manually install the expected chromium version 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 also run a script that will continually watch for changes and re-run the tests any time a change is made. This command will notify you when tests change to and from a passing or failing state, and will also print out a test coverage report, so you can see what files are in need of tests.

npm run test-watch

Running individual tests

You can run specific tests in one of these 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 it's done.

You can disable that, 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.mjs

and then, in another terminal type:

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

Or whatever the testing command you use. Note the START_JEST_SERVER=false environment variable that needs to be set or else, jest will try to start a server on :4000 too.