Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mount not working (RangeError: Invalid string length) #1050

Closed
alexmarles opened this issue Jul 28, 2017 · 12 comments
Closed

Mount not working (RangeError: Invalid string length) #1050

alexmarles opened this issue Jul 28, 2017 · 12 comments

Comments

@alexmarles
Copy link

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!

@alexmarles
Copy link
Author

alexmarles commented 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!

@tsneed290
Copy link

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

@alexmarles
Copy link
Author

@tsneed290 Not me, it works perfectly. Make sure you are using one of the serializations after importing the library, either using the toJson method or adding the serializer to expect

@DhawalRank-zz
Copy link

I am still having it. I can see the wrapper in console with debug() getting mounted perfectly but when I use toMatchSnapshot(), it fails with invalid string length error. I tried using enzyme-to-json but still it gives the same error. render() works fine but as I want to use find() and simulate(), mount() is the only option.

@schtauffen
Copy link

schtauffen commented Mar 13, 2018

I am also seeing this:

RangeError: Invalid string length
at printListItems (node_modules/pretty-format/build/collections.js:174:256)
at printComplexValue (node_modules/pretty-format/build/index.js:214:37)
at printer (node_modules/pretty-format/build/index.js:334:10)
at printObjectProperties (node_modules/pretty-format/build/collections.js:179:21)
at printComplexValue (node_modules/pretty-format/build/index.js:253:42)
at printer (node_modules/pretty-format/build/index.js:334:10)
at printListItems (node_modules/pretty-format/build/collections.js:174:258)
at printComplexValue (node_modules/pretty-format/build/index.js:214:37)
at printer (node_modules/pretty-format/build/index.js:334:10)
at printObjectProperties (node_modules/pretty-format/build/collections.js:179:21)
at printComplexValue (node_modules/pretty-format/build/index.js:253:42)
at printer (node_modules/pretty-format/build/index.js:334:10)
at printListItems (node_modules/pretty-format/build/collections.js:174:258)
at printComplexValue (node_modules/pretty-format/build/index.js:214:37)
[sic]

versions:

    "enzyme": "3.3.0",
    "enzyme-adapter-react-16": "1.1.1",
    "enzyme-to-json": "3.3.1",
    "jest": "22.1.4",

@ljharb
Copy link
Member

ljharb commented Mar 14, 2018

@schtauffen what is "pretty-format"? It seems like it's not an enzyme bug.

@schtauffen
Copy link

@ljharb it is a jest dependency.
There is some issue with the contract between enzyme/enzyme-to-json/jest but unfortunately I don't really know more at the moment to help determine where exactly the issue lies.

I've just written tests that don't use toMatchSnapshot in the cases it was failing, for now. I will try to contribute more information in the applicable repo if I find anything which leans one way or the other.

@ljharb
Copy link
Member

ljharb commented Mar 14, 2018

enzyme has no snapshot support; you probably should file this on enzyme-to-json.

@dijs
Copy link

dijs commented Apr 6, 2018

I am also having this issue...

@likethemammal
Copy link
Contributor

Adding the below code to my "jest" setting in my package.json fixed my issue

    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ]

@anosikeosifo
Copy link

Thanks @likethemammal that solved the issue for me too!

@MaxGuitet
Copy link

MaxGuitet commented May 29, 2018

I was having the same issue even with "enzyme-to-json/serializer", but only with many tests. It seems the issue arises when the snapshots is too big and creates a memory issue (hence the Invalid string length). Splitting the tests into two different files solved the issue for me.

github-merge-queue bot pushed a commit to powerhome/playbook that referenced this issue Jun 3, 2024
**What does this PR do?** A clear and concise description with your
runway ticket url.

Runway https://nitro.powerhrg.com/runway/backlog_items/PLAY-1351

Upgrades highcharts from version 9 to 10

I added the boiler plate for tests for the graphs because they had
nothing. I would have liked to add snapshot testing but was having
issues because highcharts generates random ids for different parts of
the graphs

your suppose to be able to do things like expect.any(String)
https://jestjs.io/docs/snapshot-testing#property-matchers But you need
to download other library(s) to get it to work. I bailed because its out
of scope

other people have this issue
enzymejs/enzyme#1050

```
test('bargraph has right html structure', () => {
  const { container } = render(
    <BarGraph
      data={{ testid: testId }}
    />
  );
  
  expect(container).toMatchSnapshot({ id: expect.any(String) });
});
```

![Screenshot 2024-05-24 at 9 43
20 AM](https://github.com/powerhome/playbook/assets/38965626/cd3c7730-3f9a-4e2e-9b26-b0c8709d0b3c)

**How to test?** Steps to confirm the desired behavior:
 Everything should be the same, just a version bump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants