Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-page-tester

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-page-tester

Enable DOM integration testing on Next.js pages

  • 0.8.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.1K
decreased by-6.12%
Maintainers
1
Weekly downloads
 
Created
Source

Next page tester

Build status Test coverage report Npm version

The missing DOM integration testing tool for Next.js.

Given a Next.js route, this library will return an instance of the matching page component instantiated with the properties derived by Next.js' routing system and server side data fetching.

import { render, screen, fireEvent } from '@testing-library/react';
import { getPage } from 'next-page-tester';

describe('Blog page', () => {
  it('renders blog page', async () => {
    const { page } = await getPage({
      route: '/blog/1',
    });

    render(page);
    expect(screen.getByText('Blog')).toBeInTheDocument();

    fireEvent.click(screen.getByText('Link'));
    await screen.findByText('Linked page');
  });
});

What

The idea behind this library is to enable DOM integration tests on Next.js pages along with server side data fetching and routing.

The testing approach suggested here consists of manually mocking external API's dependencies and get the component instance matching a given route.

Next page tester will take care of:

  • resolving provided routes into matching page component
  • calling Next.js data fetching methods (getServerSideProps, getInitialProps or getStaticProps) if the case
  • set up a mocked next/router provider initialized with the expected values (to test useRouter and withRouter)
  • wrapping page with custom _app component
  • instantiating page component with expected page props
  • Emulate client side navigation via Link, router.push, router.replace

Options

PropertyDescriptiontypeDefault
route (mandatory)Next route (must start with /)string-
reqAccess default mocked request object
(getServerSideProps only)
res => res-
resAccess default mocked response object
(getServerSideProps only)
req => req-
routerAccess default mocked Next router objectrouter => router-
useCustomAppUse custom App componentbooleantrue
nextRootAbsolute path to Next's root folderstringauto detected

Notes

  • Data fetching methods' context req and res objects are mocked with node-mocks-http
  • Next page tester can be used with any testing framework/library (not tied to Testing library)
  • It might be necessary to install @types/react-dom and @types/webpack when using Typescript in strict mode due to this bug

Error: Not implemented: window.scrollTo

Next.js Link components invoke window.scrollTo on click which is not implemented in JSDOM environment. In order to fix the error you should provide your own window.scrollTo mock.

Next.js versions support

next-page-tester focuses on supporting only the last major version of Next.js:

next-page-testernext.js
v0.1.0 - v0.7.0v9.X.X
v0.8.0 +v10.X.X

Todo's

  • Consider adding custom Document support
  • Consider reusing Next.js code parts (not only types)
  • Consider supporting Next.js trailingSlash option

Keywords

FAQs

Package last updated on 25 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc