Testing with React-Intl
See original GitHub issueHi. 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:
- Created 7 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
…sorry for the delay, just realized I was pinged on this.
@iam-peekay I looked at
react-test-rendererand it doesn’t support passingcontextas @dmitriiabramov said. The reason for the example I wrote up in the docs about manually creating anIntlProviderand calling itsgetChildContext()method was to get thecontext.intlwithout adding another level to tree — which doesn’t work out for shallow renderer. Butreact-test-renderersupports rending deep trees, so it’s actually simpler. You should be able to update this code:…to this:
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.