martinkr / next-export-i18n

Internationalize (18n) next.js with true support for next export
MIT License
247 stars 37 forks source link

Testing with Jest #59

Closed leobelen closed 11 months ago

leobelen commented 1 year ago

Whenever testing components tax with Jest, it fails because it cannot use the useRouter hook... is there a proper way to mock it or configure the package properly with jest?

So far, assuming that TestComponent does contain only a Component that shows a translated text, what should I do

import { render } from '@testing-library/react';

import TestComponent from '.';

describe('<TestComponent />', () => {
  it('should render the heading', () => {
    const { container } = render(<TestComponent />);
    expect(container).toBeDefined();
  });
});

As of now it fails with the following error:

Error: Uncaught [Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted]

Thanks in advance.

martinkr commented 11 months ago

Thank you for your contribution.

I can not reproduce this error. Please try the latest version.