Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbol is not a function react enzyme i18n error

When i test my react project with jest and enzyme, i encounter this problem, `TypeError: symbol is not a function

  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)

Now this is my test

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)
});

In <LoggedSearch/> the component is exported with the named export and imported in my test file as import {LoggedSearch} from "../components/LoggedSearch"; So the component should be imported pure without decoration like i18n right? . I don't understand why the test fails with this error TypeError: symbol is not a function i think it is a i18n problem because when i use shallow instead of mount it works but i can't test function with shallow. Thanks

like image 361
Mario Avatar asked Jan 28 '23 23:01

Mario


1 Answers

I had the same problem, it was due to images imported through webpack. I solved this by creating a fileMock.js with:

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

Then in jest configuration:

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

More details here: https://jestjs.io/docs/en/webpack

like image 186
Lucas Binhardi Branisso Avatar answered Feb 11 '23 09:02

Lucas Binhardi Branisso