enzyme: Unknown token type: undefined for class selectors

Current behavior

In .find, for all the places that I use a class selector (.find('.class')), I am getting this error: Unknown token type: undefined. Looking at the enzyme code, I see this is happening in selectors.js file, in nodeMatchesToken (line 214) here (looks like rst-selector-parser itself is giving token.body as an array of arrays for some reason)

If I print the token there, it is an array, with just one item, & using array[0] as the token fixes the issue for me: if(!token.type && token.length) { token = token[0]; } Not sure what I’m doing wrong.

(PS: I have recently moved from react 15->16, and ezyme 2->3, and did everything according to the docs. Everything worked fine for a while, but this issue popped up randomly)

Expected behavior

.find('.class') should work fine

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.8.0
react 16.6.3
react-dom 16.6.3
react-test-renderer 16.6.3
adapter (below) 16

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

I finally figured out what was causing our problem. I could not reproduce this outside of Jest, but it happened every-time within our applications jest tests. The issue is because we had the following entry in moduleNameMapper jest config: ‘lodash.flatten’: ‘lodash/flatten’,

Since these are regex patterns, it caused lodash.flattendeep to use lodash.flatten instead. rst-selector-parser uses flattendeep in their grammar.js file to flatten those arrays.

Hope this helps someone else.

Struggling to reproduce it in an isolated app. Will keep on trying tomorrow.

So, it’s definitely an issue with my yarn.lock file. We must have some dependencies that are out of sync somehow. Deleting the yarn.lock file and doing a yarn to regenerate the file fixed my issue.

Sorry - it used to work in enzyme 3. Today I checked out my enzyme 3 upgrade branch where it had worked, maybe about a month ago. I reinstalled the same modules, and I started experiencing this error.