New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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

[![Build status][ci-badge]][ci] [![Test coverage report][coveralls-badge]][coveralls] [![Npm version][npm-badge]][npm]

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.9K
increased by3.25%
Maintainers
1
Weekly downloads
 
Created
Source

Next page tester

Build status Test coverage report Npm version

The missing 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 } 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',
      pagesDirectory: process.cwd() + '/src/pages',
    });

    render(Page);
    expect(screen.getByText('Blog')).toBeInTheDocument();
  });
});

What

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

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

Next page tester will take care of:

  • resolving provided routes into the matching page component
  • calling Next.js data fetching methods (getServerSideProps or getStaticProps) if the case
  • set up a mocked next/router provider initialized with the expected values (to test useRouter and withRouter)
  • instantiating the page component with the expected props

Options

PropertyDescriptiontypeDefault
routeNext route (must start with /)--
pagesDirectoryAbsolute path of Next's /pages folder--
reqOverride default mocked request object
(getServerSideProps only)
res => res-
resOverride default mocked response object
(getServerSideProps only)
req => req-
routerOverride default mocked Next router objectrouter => router-

Notes

req and res objects are mocked with node-mocks-http.

Next page tester can be used with any testing framework/library.

Todo's

  • Make available dynamic api routes under /pages/api
  • Consider adding custom App and Document
  • Switch to Typescript
  • Consider adding a getPage factory

Keywords

FAQs

Package last updated on 16 Sep 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