Trying to create Enzyme Test with Translate
See original GitHub issueI’m currently trying to get Translate to work with Enzyme Testing but i get a response of **
'undefined is not an object (evaluating ‘_this.il8n.options’) Translate(Test)@webpack:///~/react-i18next/dist/commonjs/translate.js:66:0 <- tests/test-bundler.js:168038:32’
**
! I’m very new to Enzyme Testing and React !
import React, { Component } from 'react' import { translate } from 'react-i18next' class Test extends Component { render () { return ( <div> TEST </div> ) } }
I’m using Translate HOC
export default translate(['common'])(Test)
ENZYME TESTING I tried to bypass the HOC by creating my own
import React from 'react'
import { shallow, mount } from 'enzyme'
import Test from 'routes/components/test/test'
import { I18nextProvider } from 'react-i18next'
import il8n from 'i18next'
// i18next
describe('TEST ', () => {
let _props, _spies, _wrapper
beforeEach(() => {
_spies = {}
let il8nObj = il8n.init()
il8nObj.options.wait = false
_props = {
il8n : il8nObj,
i18nLoadedAt: null
}
_wrapper = shallow(
<Test {..._props} />
)
})
it('Is it a <div>', () => {
expect(_wrapper.is('div')).to.equal(true)
})
})
i also tried to use the I18nextProvider as the parent element of Test
_wrapper = shallow( <I18nextProvider> <Test {..._props} /> </I18nextProvider> ).dive()
but i get the same result when i dive() to get the Test component
Thanks
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (6 by maintainers)

Top Related StackOverflow Question
do you want to test your component or the hoc? best option is to export your pure component without the wrapping it with the hoc for tests https://github.com/i18next/react-i18next/issues/233#issuecomment-282683522
if you like to test with hoc have a look at our tests: https://github.com/i18next/react-i18next/blob/master/test/translate.render.spec.js
@jamuhl I 💯 understand. From my experience, it’s really hard to cover the edge cases. Let’s hope we can find someone in the community with spare time to improve the situation 😃. Keep the good work ✊ .