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

TypeError: Cannot read property 'version' of undefined #735

Closed
adamransom opened this issue Jul 6, 2020 · 24 comments
Closed

TypeError: Cannot read property 'version' of undefined #735

adamransom opened this issue Jul 6, 2020 · 24 comments

Comments

@adamransom
Copy link

adamransom commented Jul 6, 2020

  • @testing-library/react version: 10.4.4
  • Testing Framework and version: Jest v24
  • DOM Environment: jsdom-sixteen
  • React version: 16.13.1

What you did:

Running any test that uses @testing-library/react fails with TypeError: Cannot read property 'version' of undefined that previously used to run fine on 10.4.3.

I imagine it's due to the version check in the latest version, but I can't figure out if it is something that I need to change about my config to get it working again? I have a very standard setup I believe and use the library as such:

// spec/support/react-testing-library.js

import React from "react";
import "@testing-library/jest-dom/extend-expect";
export * from "@testing-library/react";
@MatanBobi
Copy link
Member

Hi @adamransom!
Thanks for this.
Whats the React version you're running?

@adamransom
Copy link
Author

I believe 16.13.1!

@MatanBobi
Copy link
Member

MatanBobi commented Jul 6, 2020

@adamransom I tried to reproduce this one with CRA (I upgraded to jest-environment-jsdom-sixteen), using RTL version 10.4.4 and it wasn't reproduced.
Any chance to get a reproduction of this one in codesandbox?

@adamransom
Copy link
Author

Interesting, thanks for trying to reproduce. I will attempt a reproduction in codesandbox, but it sounds like it may be something a bit more subtle in our config/package dependencies!

@MatanBobi
Copy link
Member

Maybe I'll be able to understand this one through the package.json if you want to attach it.

@adamransom
Copy link
Author

Maybe I'll be able to understand this one through the package.json if you want to attach it.

Good idea, here it is: https://pastebin.com/raw/75Tu9Jfz

@MatanBobi
Copy link
Member

Two questions:

  1. Any reason why testing-library/react is a dependency and not a devDependency?
  2. Would you mind adding: <rootDir>/node_modules/@testing-library/react and <rootDir>/node_modules/react to the transformIgnorePatterns and run the tests please?

@adamransom
Copy link
Author

adamransom commented Jul 6, 2020

  1. No reason, it was probably a mistake. Have moved it now.
  2. Unfortunately this didn't seem to make any difference :(

I also can't create a repro on codesandbox, so I guess it is something very fiddly with my config.

@adamransom
Copy link
Author

Full error, in case it is helpful:

  4     TypeError: Cannot read property 'version' of undefined                                                                                                                                       
  5                                                                                                                                                                                                         
  6       1 | import React from "react";                                                                                                                                                                    
  7       2 | import "@testing-library/jest-dom/extend-expect";                                                                                                                                            
  8     > 3 | import { fireEvent, render } from "@testing-library/react";                                                                                                                                
  9         | ^                                                                                                                                                                                          
 10       4 | import userEvent from "@testing-library/user-event";                                                                                                                                     
 11       5 | import { Router } from "react-router-dom";                                                                                                                                               
 12       6 | import { createMemoryHistory } from "history";                                                                                                                                   
 13                                                                                                                                                                                            
 14       at Object.<anonymous> (node_modules/@testing-library/react/dist/flush-microtasks.js:28:82)                                                                                                 
 15       at Object.<anonymous> (node_modules/@testing-library/react/dist/pure.js:52:47)                                                                                                             
 16       at Object.<anonymous> (node_modules/@testing-library/react/dist/index.js:7:13)                                                                                                           
 17       at Object.<anonymous> (spec/javascript/support/react-testing-library.js:3:1) 

@MatanBobi
Copy link
Member

Are you mocking React somewhere? Any chance to see the jest setup file also?
Thanks!

@adamransom
Copy link
Author

I don't think we are mocking React (knowingly). Our setup file is just:

import "jest-prop-type-error";
import ReactModal from "react-modal";

ReactModal.setAppElement(document.body);
window.alert = () => {};

@morhekil
Copy link

morhekil commented Jul 8, 2020

we just ran into the same issue when upgrading from 9.1.4 to 10.4.4

@kentcdodds
Copy link
Member

Could someone make a reproduction here? It's much easier to determine the problem with a minimal reproduction. https://kcd.im/rtl-help

@adamransom
Copy link
Author

adamransom commented Jul 8, 2020

Yeah, I did try to make a minimal reproduction quickly but couldn't unfortunately 😞 I imagine it will involve taking my current config and rebuilding it piece by piece until the error occurs. I'll try to do this when I have the time, but since we are using this in production we are content with simply holding off bumping the version for now.

Thanks for all the time you've put in so far @MatanBobi! I'll comment again when I've got a reproduction.

@urlaubsbaron
Copy link

same for us, breaks after upgrading

@MatanBobi
Copy link
Member

@urlaubsbaron, @adamransom can one of you please attach your Webpack config?

@adamransom
Copy link
Author

Here you go: https://pastebin.com/2s7t6Kup

We use rails/webpacker so the config is auto-generated and might look a bit funky, not sure!

@michael5891
Copy link

@MatanBobi
v10.4.4 was update to verify the React version:
@testing-library/react/dist/flush-microtasks.js
const isModernScheduleCallbackSupported = satisfies(React.version, '>16.8.6', { includePrerelease: true, })
transpiled code would be :
const isModernScheduleCallbackSupported = Scheduler && (0, _satisfies.default)(_react.default.version, '>16.8.6', { includePrerelease: true });
_react.default is undefined.
reverting to v10.4.3 worked for me.

@MatanBobi
Copy link
Member

MatanBobi commented Jul 9, 2020

thanks @michael5891, If you'll be able to provide a reproduction repo it would really help (https://kcd.im/rtl-help).

@adamransom - I saw that in your jest configs you have:

"moduleDirectories": [
      "node_modules",
      "app/javascript",
      ""
]

This is a long-shot but I was able to reproduce this on my local env and when I removed the empty string at the bottom this one was fixed.
I think it's a long-shot because I don't really understand why it matters, but maybe worth a try :)

@adamransom
Copy link
Author

It does indeed fix it! For some reason, we used "" instead of "<rootDir>", which achieves exactly what we want. Who knows if this is a bug in Jest, RTL or even if an empty string there should be supported!

Anyways, problem resolved for us, so thank you for helping!

@morhekil
Copy link

looks like we had a similar problem, just in our case it was "./" entry in moduleDirectories that caused this issue:

    "moduleDirectories": [
      "./",
      "node_modules",
      "bower_components"
    ],

changing this to "<rootDir>" as suggested above got things working again:

    "moduleDirectories": [
      "<rootDir>",
      "node_modules",
      "bower_components"
    ],

Thank you everyone!

@MatanBobi
Copy link
Member

@urlaubsbaron, @michael5891 can you please have a look if you have the same jest config by any chance so we'll be able to close this one?

@urlaubsbaron
Copy link

urlaubsbaron commented Jul 12, 2020

Same applies for us:

moduleDirectories: ['node_modules', './'],

I will replace it with

moduleDirectories: ['node_modules', '<rootDir>'],

thanks for clarification

@MatanBobi
Copy link
Member

This should be resolved with #744 which was published a few hours ago.
If it's not fixed in the next version, please comment here.

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

6 participants