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 using react-testing-library
.
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.
I considered creating nextjs-testing-library
(a fork of react-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:
You're probably better of running next dev and using cypress or puppeteer or any other e2e testing framework of your choice.
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! 馃
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/[email protected]+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 serverless
target...we'd have to run a server
target and handle that in order to run the integration tests in a ci environment...which would also mean we're testing a totally different setup that we're deploying to production.
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/
https://angular.io/guide/testing
https://guides.emberjs.com/release/testing/
https://vue-test-utils.vuejs.org/
A notable exception to good testing docs is Svelte, but at least they offer a scrap of an explanation:
https://svelte.dev/faq#how-do-i-test-svelte-apps
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 (getServerSideProps
, getStaticProps
, dynamic API, dynamic routing, etc..)
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 /post/33
) and receiving back a the corresponding page element ready to be rendered with any DOM testing library.
The library is supposed to take care of:
getServerSideProps
or getStaticProps
) if the caseuseRouter
and withRouter
)The library is available on NPM as next-page-tester
. Any feedback or contribution to validate (or not) this approach is appreciated.
Most helpful comment
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/
https://angular.io/guide/testing
https://guides.emberjs.com/release/testing/
https://vue-test-utils.vuejs.org/
A notable exception to good testing docs is Svelte, but at least they offer a scrap of an explanation:
https://svelte.dev/faq#how-do-i-test-svelte-apps