Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Forked from novascreen/README.md
Created August 29, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robksawyer/bd7cd0db458ad82d91cc7ef92677bbff to your computer and use it in GitHub Desktop.
Save robksawyer/bd7cd0db458ad82d91cc7ef92677bbff to your computer and use it in GitHub Desktop.
How to mock next/router in Storybook

If you use Storybook with Next.js and have components using next/link you'll have to mock next/router the same you would for testing with Jest or others. Simply create a file with the mock router as shown below and import it in your Storybook config.

This is based on some information from an issue on Next.js:

vercel/next.js#1827

import { configure } from '@storybook/react';
import './mockNextRouter';
/* ... */
import Router from 'next/router';
Router.router = {
push: () => {},
prefetch: () => {},
};
@kasir-barati
Copy link

Any suggestion on how to mock and modify the current address in the URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment