New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Testing guide to documentation #10967
Comments
In addition to testing the react components, a guide for testing the API routes would be helpful too. |
I would be very interested in a guide to run Cypress tests against PR preview deployments. I'm working on setting that up now |
If possible I would like to have a github action run Cypress against the vercel preview url, but I don't know how to get that information from the Zeit GH integration |
@cmbirk we are currently utilizing github actions + cypress to run what I am now deeming a smoke test against the Vercel build preview...this just makes sure basic functionality is working...here is the github actions yml it's pretty simple to get up and running on each PR... name: Deploy & Smoke Test
on: [pull_request]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: amondnet/vercel-action@v19.0.1+1
with:
vercel-token: ${{ secrets.VERCEL_TOKEN }} # Required
github-token: ${{ secrets.GITHUB_TOKEN }} #Optional
vercel-org-id: ${{ secrets.VERCEL_TEAM_ID }} #Required
vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} #Required
scope: ${{ secrets.VERCEL_SCOPE }}
id: build_vercel
- name: Cypress Test
uses: cypress-io/github-action@v1
with:
config: baseUrl=${{ steps.build_vercel.outputs.preview-url }} Reason I stumbled upon this open issue is what we'd really like to get is a full integration-suite of cypress tests that runs against a test build (production build, with test env vars) of our nextjs application. Getting this to work to my knowledge is not very straight forward considering we leverage Vercel for our deployments with a Interested in hearing how anyone else has approached this problem. |
The thing with puppeteer and Next.JS is that it's pretty difficult to tell when the route changes have ended, because the normal "waitForNavigation" method doesn't seem to work well with Next.JS routing |
I'm shocked that there isn't a word about testing in the Next docs, or on their homepage or anything. This feels like a big oversight. When I evaluate a framework or library like this I look for the testing documentation and for where testing fits into the story of the library and the values of the maintainers. See for example: https://www.gatsbyjs.org/docs/testing/ A notable exception to good testing docs is Svelte, but at least they offer a scrap of an explanation: |
When I started to use Next I was also concerned about heavily relying on e2e tests to test parts of the application that could be covered with cheaper/quicker DOM nodes tests running in Node.js (like Testing Library). From my point if view, the main issue about testing Next pages is that the framework provides a lot of glue that you need to somehow replicate in order to not write tests for every single piece of your pages ( As a proof of concept I put together this library which tries to fill these gaps and provide a smoother testing experience without spinning up the actual Next server. The idea consists of providing a Next path (like The library is supposed to take care of:
The library is available on NPM as |
I have been using this (typescript) with some success today. It basically waits for the URL to contain a path and throws an errors if you aren't sent there within an interval. export const waitForPath = async (
path: string,
page: Page,
timeInMS = 1000
) => {
const interval = 50
const iterations = timeInMS / interval
let foundPath = false
for (let index = 0; index < iterations; index++) {
const currentPath = page.url().split(/\.com|\.dev/)[1]
if (currentPath === path) {
foundPath = true
break
}
await sleep(interval)
}
if (!foundPath)
throw new Error(`Page did not navigate to ${path} within ${timeInMS}ms`)
} |
Maybe this will help a few people, here is an example of E2E testing with Cypress and Next.js. https://github.com/UnlyEd/next-right-now/tree/v2-mst-aptd-at-lcz-sty/cypress Tests are run by GitHub Action automatically (CI/CD). The repo also contains other testing examples. (unit/integration) using Jest/Enzyme/etc. |
There is now a "Testing" documentation : https://nextjs.org/docs/testing |
Thanks for posting @divlo! If you would like to see additions to the documentation, please post back on the Discussion linked above. |
Feature request
Is your feature request related to a problem? Please describe.
I am a fan of testing, and now that we go with Next.js at my company, I started thinking about how we should test pages. I immediately wanted to go with react-testing-library because of its simplicity, and that it is mainly framework agnostic, although I hit a problem. I am talking about pages implementing one of the
GS(S)P
methods. In addition, _app, api routes, etc. maybe also should be taken into consideration here. All these makes testing pages a bit trickier than simply usingreact-testing-library
.Describe the solution you'd like
I would like the Next.js team - if they have an opinion - to consider putting their thoughts around testing of Next.js apps into the docs, so we get a good starting point.
Describe alternatives you've considered
I considered creating
nextjs-testing-library
(a fork ofreact-testing-library
), that would make writing tests for Next.js pages as easy, as using react-testing-library. But as someone pointed out, it would mean, I should re-implement Next.js in a way, which is not optimal.https://spectrum.chat/testing-library/help-react/creating-nextjs-testing-library-for-next-js-help-feedback-needed~cd940022-aaa2-46fb-8c37-59af9ea77990
I was suggested:
by @eps1lon
And if the Next.js team also thinks this is the way to go, or they have a better alternative in mind, a few words around it in the documentation would be great! 🤗
The text was updated successfully, but these errors were encountered: