Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

测试时提示:formatMessage not initialized yet, you should use it after react app mounted #2156

Closed
LingzhiLiu opened this issue Mar 22, 2019 · 8 comments

Comments

@LingzhiLiu
Copy link

在单元测试中,使用enzyme初始化含有formatMessage方法的组件时,无论使用mount或者shallow:
mount(<Component {...props} />
都会在umi-plugin-locale出现warning:
image
有没有解决办法在测试中解决这个warning?

@imhele
Copy link
Contributor

imhele commented Mar 22, 2019

测试时组件顶层没有国际化 context 的 provider ,目前比较简单的方法是 mock 掉 formatMessage

@imhele imhele closed this as completed Mar 23, 2019
@sorrycc
Copy link
Member

sorrycc commented Mar 25, 2019

@imhele 有空可以写下如何 mock 掉 formatMessage,很多人应该都不会。

@LingzhiLiu
Copy link
Author

我解决了,新建了一个intlHelper.js文件,在往component注入intl的同时,调用umi locale的_setIntlObject来初始化umi的intl。这样就不会在mount的过程中报错说formatMessage没有初始化了。但是这种做法可能不被支持,毕竟_setIntlObject是umi内部函数。
代码如下:

import { IntlProvider, intlShape } from 'react-intl'
import { mount } from 'enzyme';
import React from 'react';
import { _setIntlObject } from 'umi/locale'
import messages from '../locales/en-US'

const intlProvider = new IntlProvider({ locale: 'en-US', messages }, {})
const { intl } = intlProvider.getChildContext()

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

export function mountWithIntl(node, { context, childContextTypes } = {}) {
  return mount(
    nodeWithIntlProp(node),
    {
      context: Object.assign({}, context, { intl }),
      childContextTypes: Object.assign({}, { intl: intlShape }, childContextTypes),
    }
  )
}

在渲染component时直接调用即可
const enzymeWrapper = mountWithIntl(<MyComponent {...props} />)

参考了:
https://gist.github.com/mirague/c05f4da0d781a9b339b501f1d5d33c37/
https://github.com/umijs/umi/blob/master/packages/umi-plugin-locale/test/locale.test.js

@imhele imhele self-assigned this Mar 25, 2019
@imhele
Copy link
Contributor

imhele commented Mar 25, 2019

或许可以在 umi-plugin-locale 中添加 <TestIntl /><MockIntl config={{ formatMessage: 'Mock text' }} /> 导出,需要测试时在组件外包裹一层

@stale
Copy link

stale bot commented May 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label May 24, 2019
@stale
Copy link

stale bot commented Jul 23, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 23, 2019
@stale stale bot closed this as completed Jul 30, 2019
@paradite
Copy link

paradite commented Dec 18, 2019

现在可用的mock formatMessage 的方案:

import React from 'react';
import { mount } from 'enzyme';
import { formatMessage } from 'umi-plugin-react/locale';
import { MyComponent } from '../../src/component';

import messages from '../../src/locale/en-US';

jest.mock('umi-plugin-react/locale');

describe('component/MyComponent.test.js', () => {
  it('MyComponent', async () => {
    formatMessage.mockImplementation(({ id }) => {
      return <span>{messages[id]}</span>;
    });
    const wrapper = mount(<MyComponent />);
    const html = wrapper.html();
    expect(html).toMatch(/My translation/);
  });
});

@JinhuW
Copy link

JinhuW commented Jan 4, 2020

mountWithIntl
Enzyme expects an adapter to be configured, but found none.
To configure an adapter, you should call Enzyme.configure({ adapter: new Adapter() })
before using any of Enzyme's top level APIs, where Adapter is the adapter
corresponding to the library currently being tested. For example:
我在使用你的方法的时候出现这个报错

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

Successfully merging a pull request may close this issue.

5 participants