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.

configure custom wrappingComponent and use shallow method cause RangeError: Maximum call stack size exceeded

See original GitHub issue

Jest setup file:

import React from 'react';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

function CustomWrapper({ children }) {
  return <div>{children}</div>;
}

configure({
  adapter: new Adapter(),
  wrappingComponent: CustomWrapper,
});

Test case:

import React from 'react';
import { shallow } from 'enzyme';

function Button() {
  return <button>My Button</button>;
}

it('should work', function () {
  const el = shallow(<Button />); // throw RangeError: Maximum call stack size exceeded
  expect(el).toMatchSnapshot();
});

I found it seems to be this line: ShallowWrapper.js#L355

  • enzyme: 3.10.0
  • enzyme-adapter-react-16: 1.15.1
  • jest: 24.9.0
  • react: 16.12.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
xuyuanxiangcommented, Dec 25, 2019

I found a way to avoid this issue:

import React from 'react';
import { configure } from 'enzyme';
import { createRenderWrapper } from 'enzyme-adapter-utils';
import Adapter from 'enzyme-adapter-react-16';

function CustomWrapper({ children }) {
  return <div>{children}</div>;
}

class CustomAdapter extends Adapter {
  createStringRenderer(options) {
    return {
      render(el, context) {
        if (options.context && (el.type.contextTypes || options.childContextTypes)) {
          const childContextTypes = {
            ...(el.type.contextTypes || {}),
            ...options.childContextTypes,
          };
          const ContextWrapper = createRenderWrapper(el, context, childContextTypes);
          return ReactDOMServer.renderToStaticMarkup(
            React.createElement(CustomWrapper, null, React.createElement(ContextWrapper)),
          );
        }
        return ReactDOMServer.renderToStaticMarkup(React.createElement(CustomWrapper, null, el));
      },
    };
  }
  createMountRenderer(options) {
    Object.assign(options, { wrappingComponent: CustomWrapper });
    return super.createMountRenderer(options);
  }
}

configure({
  adapter: new CustomAdapter(),
});
0reactions
xuyuanxiangcommented, Dec 26, 2019

I mean:

shallow(<Button />, { wrappingComponent: CustomWrapper })

The makeShallowOptions function will merge configure options and shallow passedOptions.

Either way:

import { configure, shallow } from 'enzyme';

configure({ wrappingComponent: CustomWrapper });

shallow(<Button />, { wrappingComponent: CustomWrapper }); // Throw Range Error...

This error is raised whenever wrappingComponent exists.

Sorry, I actually execute the code only configure wrappingComponent at the adapter level will raise this error, pass it in shallow was ok.

Test case:

import React from 'react';
import { shallow, configure } from 'enzyme';

function CustomWrapper({ children }) {
  return <div>{children}</div>;
}

function Button() {
  return <button>My Button</button>;
}

it('should work ok', () => {
  shallow(<Button />, { wrappingComponent: CustomerWrapper });
});

it('should raise RangeError', () => {
  configure({ wrappingComponent: CustomerWrapper });
  expect(() => shallow(<Button />)).toThrow('Maximum call stack size exceeded');
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

enzymejs/enzyme - Gitter
It crashing ONLY when im trying to use mount, when use shallow everything works fine. ... RangeError: Maximum call stack size exceeded at...
Read more >
JavaScript RangeError: Maximum Call Stack Size Exceeded
The JavaScript RangeError: Maximum call stack size exceeded is an error that occurs when there are too many function calls, or if a...
Read more >
RangeError: Maximum call stack size exceeded Reactjs?
This method in index.js will cause an infinite loop when you call this method on your onChange event at Question component ❌
Read more >
Shallow Rendering API - Enzyme - GitHub Pages
As of Enzyme v3, the shallow API does call React lifecycle methods such as componentDidMount and componentDidUpdate . You can read more about...
Read more >
Error RangeError: Maximum call stack size exceeded
4. User changes date using "Calendar" control in top left corner of the scheduler. This is the calendar control that comes by default...
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 Hashnode Post

No results found