rsnext/test
Wyatt Johnson c6ef857d57
Subresource Integrity for App Directory (#39729)
<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we
request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

This serves to add support for [Subresource
Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
hashes for scripts added from the new app directory. This also has
support for utilizing nonce values passed from request headers (expected
to be generated per request in middleware) in the bootstrapping scripts
via the `Content-Security-Policy` header as such:

```
Content-Security-Policy: script-src 'nonce-2726c7f26c'
```

Which results in the inline scripts having a new `nonce` attribute hash
added. These features combined support for setting an aggressive Content
Security Policy on scripts loaded.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)

Co-authored-by: Steven <steven@ceriously.com>
2022-09-08 15:17:15 -07:00
..
.stats-app Add edge ssr to pr stats (#39621) 2022-08-15 13:28:31 -05:00
__mocks__ Update Edge Runtime (#38862) 2022-07-21 18:29:19 +00:00
development Update flakey tsconfig test (#40105) 2022-08-30 12:31:38 -05:00
e2e Subresource Integrity for App Directory (#39729) 2022-09-08 15:17:15 -07:00
integration Subresource Integrity for App Directory (#39729) 2022-09-08 15:17:15 -07:00
lib Subresource Integrity for App Directory (#39729) 2022-09-08 15:17:15 -07:00
production Subresource Integrity for App Directory (#39729) 2022-09-08 15:17:15 -07:00
unit Fix static info parsing when export data fetching method as variable (#40317) 2022-09-07 09:28:15 -07:00
.gitignore
jest-setup-after-env.ts Adds tests to ensure eslint-plugin-next's available rules are properly exported and recommended rules are correctly defined. (#38183) 2022-06-30 11:31:33 -05:00
jest.d.ts Adds tests to ensure eslint-plugin-next's available rules are properly exported and recommended rules are correctly defined. (#38183) 2022-06-30 11:31:33 -05:00
readme.md Add note about test-trace artifact to test readme (#38770) 2022-07-18 13:01:37 -05:00
test-file.txt Add additional file serving tests (#12479) 2020-05-04 11:58:19 -05:00

Writing tests for Next.js

Getting Started

You can set-up a new test using yarn new-test which will start from a template related to the test type.

Test Types in Next.js

  • e2e: These tests will run against next dev, next start, and deployed to Vercel
  • development: These tests only run against next dev
  • production: These tests will run against next start.
  • integration: These tests run misc checks and modes and is where tests used to be added before we added more specific folders. Ideally we don't add new test suites here as tests here are not isolated from the monorepo.
  • unit: These are very fast tests that should run without a browser or running next and should be testing a specific utility.

For the e2e, production, and development tests the createNext utility should be used and an example is available here. This creates an isolated Next.js install to ensure nothing in the monorepo is relied on accidentally causing incorrect tests.

All new test suites should be written in TypeScript either .ts (or .tsx for unit tests). This will help ensure we catch smaller issues in tests that could cause flakey or incorrect tests.

If a test suite already exists that relates closely to the item being tested (e.g. hash navigation relates to existing navigation test suites) the new checks can be added in the existing test suite.

Best Practices

  • When checking for a condition that might take time, ensure it is waited for either using the browser waitForElement or using the check util in next-test-utils.
  • When applying a fix, ensure the test fails without the fix. This makes sure the test will properly catch regressions.

Helpful environment variables

There are some test specific environment variables that can be used to help debug isolated tests better, these can be leveraged by prefixing the pnpm testheadless command.

  • When investigating failures in isolated tests you can use NEXT_TEST_SKIP_CLEANUP=1 to prevent deleting the temp folder created for the test, then you can run pnpm next while inside of the temp folder to debug the fully setup test project.
  • You can also use NEXT_SKIP_ISOLATE=1 if the test doesn't need to be installed to debug and it will run inside of the Next.js repo instead of the temp directory, this can also reduce test times locally but is not compatible with all tests.
  • The NEXT_TEST_MODE env variable allows toggling specific test modes for the e2e folder, it can be used when not using pnpm test-dev or pnpm test-start directly. Valid test modes can be seen here: aa664868c1/test/lib/e2e-utils.ts (L46)

Debugging

When tests are run in CI and a test failure occurs we attempt to capture traces of the playwright run to make debugging the failure easier. A test-trace artifact should be uploaded after the workflow completes which can be downloaded, unzipped, and then inspected with pnpm playwright show-trace ./path/to/trace