Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'addListener' of undefined react-testing-library

I'm trying to test my antd application with react testing library, but I keep getting this error:

TypeError: Cannot read property 'addListener' of undefined

Im using a custom render but the error seems to be coming from the 'render' method.

const customRender = (ui, options) => render(ui, { wrapper: TestingWrapper, ...options }) ^

I´m even using the same versions of react and react-dom (which seems to be a common issue with rtl).

"react": "17.0.1", "react-dom": "17.0.1",

The problematic component seems to be this:

import React, {
  lazy,
  Suspense
} from 'react';

import List from 'antd/lib/list';
  
const Stories = (props) => {
    return(
  <div className="stories-container">

    <div>
      <h1 className="StoriesTitle">Stories</h1>
    </div>

    <div className="StoryListContainer">
     <Suspense fallback={<Spin />}>
        <List
          itemLayout="vertical"
          size="default"
          pagination={pagination}
          dataSource={stories}
          renderItem={item =>
            <StoryItem
              item={item}
              deleteFn={onDelete}
              loggedIn={loggedIn}
              stories={stories}
            />
          }
        />
      </Suspense>
    </div>

  </div>
    );
}

It seems to error out in the module 'antd/lib/_util/responsiveObserve' which is a part of antd's List component. Taking that component out makes the test work (though obviously I don't want to remove it from my application).

like image 724
Demian Avatar asked Nov 12 '20 23:11

Demian


People also ask

What does cannot read property mean in JavaScript?

Cannot read property means the code was trying to read a property. This is a good clue! There are only a few ways to read properties in JavaScript. The most common is probably the . operator. As in user.name, to access the name property of the user object.

Why is my renderhook not working on react 18?

You should instead try the new renderHook API from react-testing-library ( see here for details) I suspect the issue here is that you have not updated react-test-renderer to react 18 as well (the stack trace points to that as the detected renderer, but you are posting react-dom versions).

Why can't I read function “map” of undefined?

You might wonder why the error isn’t more specific, like “Cannot read function `map` of undefined” – but remember, the JS interpreter has no idea what we meant that type to be. It doesn’t know it was supposed to be an array, or that map is a function. It didn’t get that far, because items is undefined.


1 Answers

Are you perhaps using defining window.matchMedia in your jest setupTest.js file? What fixed it for me is to move it from window.matchMedia to global.matchMedia like how they did it in this repo.

global.matchMedia = global.matchMedia || function () {
  return {
    addListener: jest.fn(),
    removeListener: jest.fn(),
  };
};
like image 170
Albert Manuel Avatar answered Sep 30 '22 17:09

Albert Manuel