question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to create Enzyme Test with Translate

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jamuhlcommented, May 18, 2017

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

1reaction
oliviertassinaricommented, Jan 19, 2018

@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 ✊ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest and Enzyme with React: Mock method with Translate
Note that I usually pass t for translation, which I am attempting to do in the test but it's working: import React, {...
Read more >
Unit Testing in React: Full Guide on Jest and Enzyme Testing
Discover how to start and proceed with the testing of React components with Enzyme and Jest if you do not have enough experience...
Read more >
How to write tests using Jest and Enzyme: React JS Testing ...
Facebook uses Jest and Airbnb uses Enzyme as testing utilities. ... In order to create a React project, run this command: npx ...
Read more >
A Practical Guide To Testing React Applications With Jest
In this article, you will learn everything you need to create a solid test for your React components and application.
Read more >
Testing Components in React Using Jest and Enzyme - Code
If you want to test the behavior of a component with its children, ... Let's create a beforeEach() function that fills in mock...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found