Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
next-page-tester
Advanced tools
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');
});
});
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:
getServerSideProps
, getInitialProps
or getStaticProps
) if the casenext/router
provider initialized with the expected values (to test useRouter
and withRouter
)_app
componentLink
, router.push
, router.replace
Property | Description | type | Default |
---|---|---|---|
route (mandatory) | Next route (must start with / ) | string | - |
req | Access default mocked request object ( getServerSideProps only) | res => res | - |
res | Access default mocked response object ( getServerSideProps only) | req => req | - |
router | Access default mocked Next router object | router => router | - |
useCustomApp | Use custom App component | boolean | true |
nextRoot | Absolute path to Next's root folder | string | auto detected |
req
and res
objects are mocked with node-mocks-http@types/react-dom
and @types/webpack
when using Typescript in strict
mode due to this bugNext.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-page-tester
focuses on supporting only the last major version of Next.js:
next-page-tester | next.js |
---|---|
v0.1.0 - v0.7.0 | v9.X.X |
v0.8.0 + | v10.X.X |
trailingSlash
option0.8.0
getPage
promise returns an object with a page
prop holding the expected page element (previously page element was directly returned)useCustomApp
option defaults to true
FAQs
Enable DOM integration testing on Next.js pages
The npm package next-page-tester receives a total of 3,247 weekly downloads. As such, next-page-tester popularity was classified as popular.
We found that next-page-tester demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.