【问题标题】:Symbol is not a function react enzyme i18n error符号不是函数反应酶 i18n 错误
【发布时间】:2018-11-16 11:28:47
【问题描述】:

当我用 jest 和酵素测试我的反应项目时,我遇到了这个问题, `TypeError: 符号不是函数

  44 | test('PROVA', () => {
> 46 |     const wrapper = mount(<I18nextProvider i18n={i18n}><MemoryRouter><LoggedSearch t={(k) => 'translate hardcoded'} userData={Artist}/></MemoryRouter></I18nextProvider>,{context});
     |                     ^
  47 |     console.log(wrapper.debug());
  48 |     wrapper.find('#ButtonSearch').simulate('click', { preventDefault() {} });
  49 |     expect(true).toBe(true)

  at setValueForProperty (../node_modules/react-dom/cjs/react-dom.development.js:2909:46)

现在这是我的测试

test('PROVA', () => {
    //const wrapper = mount(<LoggedSearch t={key=>key} userData={Artist}/>);
    const wrapper = mount(<I18nextProvider i18n={i18n}><MemoryRouter><LoggedSearch t={(k) => 'translate hardcoded'} userData={Artist}/></MemoryRouter></I18nextProvider>,{context});
    console.log(wrapper.debug());
    wrapper.find('#ButtonSearch').simulate('click', { preventDefault() {} });
    expect(true).toBe(true)
});

&lt;LoggedSearch/&gt; 中,组件使用命名导出导出并在我的测试文件中导入为import {LoggedSearch} from "../components/LoggedSearch"; 所以组件应该像 i18n 那样纯进口没有装饰对吗? .我不明白为什么测试失败并出现此错误TypeError: symbol is not a function 我认为这是一个 i18n 问题,因为当我使用浅层而不是挂载时它可以工作,但我无法用浅层测试函数。谢谢

【问题讨论】:

    标签: reactjs testing internationalization jestjs enzyme


    【解决方案1】:

    我遇到了同样的问题,这是由于通过 webpack 导入的图像。我通过创建一个 fileMock.js 解决了这个问题:

    module.exports = 'test-file-stub';
    

    然后在开玩笑的配置中:

    "moduleNameMapper": {
        "\\.(css|scss)$": "identity-obj-proxy",
        "\\.(png|svg|pdf|jpg|jpeg)$": "<rootDir>/__mocks__/fileMock.js"
    }
    

    更多细节在这里: https://jestjs.io/docs/en/webpack

    【讨论】:

      【解决方案2】:

      我不确定这是否是您的确切问题,但这可能是由于您通过 webpack 导入图像的方式,导入。它可能类似于this 问题。

      【讨论】:

        猜你喜欢
        • 2021-10-22
        • 1970-01-01
        • 2021-11-30
        • 2017-03-16
        • 2019-02-27
        • 2018-06-15
        • 2021-04-28
        • 2021-08-20
        • 1970-01-01
        相关资源
        最近更新 更多