Mount not working (RangeError: Invalid string length)

See original GitHub issue

Hi everyone,

maybe I am missing something essential, but I am trying to use mount for some testing and it is just not working. These are my Jest and Enzyme versions:

"enzyme": "^2.9.1",
"jest": "^20.0.4",

I have tried in different types of components and even in the simplest one I came up to, it still not works. This is the component code:

import React, { Component } from 'react'

class Simple extends Component {
  render () {
    return (
      <div>
        <p>This is simple</p>
      </div>
    )
  }
}

export default Simple

And this is the test I wrote for it:

import React from 'react'
import { mount } from 'enzyme'

import Simple from '../../src/js/components/Simple'

test('Simple component renders', () => {
  let wrapper = mount(<Simple />)
  expect(wrapper).toMatchSnapshot()
})

And the output I get is this:

FAIL  __test__/components/Simple.test.js (26.302s)
  ● Simple component renders
    RangeError: Invalid string length
      
      at Object.<anonymous> (__test__/components/Simple.test.js:8:19)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at process._tickCallback (internal/process/next_tick.js:109:7)

I googled this error and the only thing I found is that it might be some out of memory problem, and that is why I tried it with a really simple component, but still it doesn’t work. I have tested this in Ubuntu and Mac OS X, both with the same results.

Thanks in advance for those who can help me with it!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

25reactions
tsneed290commented, Aug 9, 2017

Is anyone still seeing this issue even after leveraging enzyme-to-json?

10reactions
alexmarlescommented, Jul 28, 2017

Ok, my bad.

What is really not working in the example is mounting a component with Enzyme and trying to test it with the Jest’s Snapshot Testing right away. Components must be converted to Json for them to be stored and compared as snapshots.

Best way to do it would be using enzyme-to-json to transform mounted components to JSON:

import toJson from 'enzyme-to-json'

...

expect(toJson(wrapper)).toMatchSnapshot()

If you don’t want to write toJson every time, you can import its serializer and add it to expect:

import serializer from 'enzyme-to-json/serializer';

expect.addSnapshotSerializer(serializer)

...

expect(wrapper).toMatchSnapshot()

You can also keep using Jest’s own renderer, but you’ll be missing all the Enzyme’s features related to the component management.

Closing this, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught RangeError: Invalid string length in JavaScript
The issue is that on each iteration you add the text two times, but you assign the result to text again. iteration 1:...
Read more >
Mount not working (RangeError: Invalid string length)
I googled this error and the only thing I found is that it might be some out of memory problem, and that is...
Read more >
HTML : Uncaught RangeError: Invalid string length ... - YouTube
HTML : Uncaught RangeError : Invalid string length when appending to a string in JavaScript [ Beautify Your Computer ...
Read more >
enzymejs/enzyme - Gitter
createElement: type is invalid -- expected a string (for built-in components) or a ... but I have case when need to test component...
Read more >
Invalid child in posBefore (CodeMirror6)
Uncaught RangeError: Invalid child in posBefore ... I can still move the caret with the keys with no problem (though when I go...
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