Testing with React-Intl

See original GitHub issue

Hi. I’m trying to use snapshot testing for components that use React-Intl (https://github.com/yahoo/react-intl). I tried following a similar approach to how we’d set it up using Enzyme (https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl#enzyme):

import React from 'react';
jest.mock('react-dom');
import renderer from 'react-test-renderer';
import { IntlProvider } from 'react-intl';

const messages = require('../../build/lang/en-US.json');
const intlProvider = new IntlProvider({ locale: 'en', messages }, {});
const { intl } = intlProvider.getChildContext();

function nodeWithIntlProp(node) {
  return React.cloneElement(node, { intl });
}

const rendererWithIntl = (node) => {
  return renderer.create(nodeWithIntlProp(node), { context: { intl } });
};


export {
  rendererWithIntl,
};

and then my tests:

import React from 'react';
jest.mock('react-dom');
import { rendererWithIntl } from 'utils/tests';
import Item from './Item';

describe('<Item /> - snapshot', () => {
  it('renders the default Item', () => {
    const component = rendererWithIntl(
      <Item
        items={ [{ id: '1', text: '123' }] }
        selectItem={ () => {} }
      />
    );
    const tree = component.toJSON();
    expect(tree).toMatchSnapshot();
  });
});

it keeps throwing an error saying Invariant Violation: [React Intl] Could not find requiredintlobject. <IntlProvider> needs to exist in the component ancestry.. So it looks like I’m not defining the intl context properly using renderer.create. Wondering if you have any tips for how to create the intl context using react-test-renderer’s renderer?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ericfcommented, Aug 24, 2016

…sorry for the delay, just realized I was pinged on this.

@iam-peekay I looked at react-test-renderer and it doesn’t support passing context as @dmitriiabramov said. The reason for the example I wrote up in the docs about manually creating an IntlProvider and calling its getChildContext() method was to get the context.intl without adding another level to tree — which doesn’t work out for shallow renderer. But react-test-renderer supports rending deep trees, so it’s actually simpler. You should be able to update this code:

const rendererWithIntl = (node) => {
  return renderer.create(nodeWithIntlProp(node), { context: { intl } });
};

…to this:

const rendererWithIntl = (node) => {
  return renderer.create(<IntlProvider locale='en' messages={messages}>{node}</IntlProvider>);
};
0reactions
github-actions[bot]commented, May 14, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Intl | Testing Library
Configuring React-Intl Polyfills / Locales​. If you're using React-Intl in your project, and you need to load a locale, You have two options ......
Read more >
Testing with formatjs | Format.JS
Testing with formatjs. Intl APIs requirements​. React Intl uses the built-in Intl APIs in JavaScript. Make sure your environment satisfy the requirements ...
Read more >
React-Intl and React-testing-library - CodeSandbox
This should help to understand how to use IntlProvider on test environment, along with react-testing-library. react-testing-library. react. react-intl. locale.
Read more >
How to use react-intl with jest - Stack Overflow
I'm working with React Testing Library an I need to get the translation for a text, we mocked in our setupTest file the...
Read more >
Testing React-Intl components with Enzyme's mount ... - GitHub
Testing React-Intl components with Enzyme's mount() and shallow() methods. This is a helper function which wraps the `intl` context around your component ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found