Skip to content

Object.entries is not a function when importing react-testing-library #110

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

Closed
snekse opened this issue Jun 9, 2018 · 14 comments
Closed

Comments

@snekse
Copy link

snekse commented Jun 9, 2018

Reporting this as an issue along with the fix in hopes that it helps others find the answer quicker.

Problem
When importing react-testing-library and running my tests, they failed with the following error

Object.entries is not a function

Version Information

  • react-testing-library version: 3.1.4
  • react version: N/A
  • node version: 6.x
  • npm (or yarn) version: 6.x

Solution

Per @kentcdodds suggestion in a glamorous ticket:

Make sure you're on the latest stable node (>=8) and npm (>= 5). Sorry about that!

Upgrading to the latest version of node did indeed resolve my issue.

@eliassotodo
Copy link
Contributor

eliassotodo commented Jun 9, 2018

For people who can't upgrade: install and import the babel-polyfill in your test files.

import babel-polyfill

@gnapse
Copy link
Member

gnapse commented Jun 9, 2018

I guess it wouldn't harm to use Object.keys instead. I think is supported in more environments than Object.entries, and can be used to achieve the same goals.

@snekse
Copy link
Author

snekse commented Jun 9, 2018

To be clear, I'm not using Object.keys or Object.entries in my testing code. Simply adding
import * as rtlib from 'react-testing-library'
causes this. I'm assuming it's because the library is using these features. In my mind it's not a big deal to update node which I should have done a long time ago.

@gnapse
Copy link
Member

gnapse commented Jun 10, 2018

Yes I know. What I suggested was regarding the code in this library, that maybe that code could with to use Object.keys, which I believe is more widely supported.

@kentcdodds
Copy link
Member

Considering Node 6 is on maintenance LTS, I don't care to support it anymore. We should update our engines config in package.json.

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 3.1.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@muyaszed
Copy link

Hi guys, sorry if i'm not doing this right. I happen to encounter this problem again. All my node, npm and react-testing-library is all up to date. The solution I am using right now is importing babel-polyfill

@kentcdodds
Copy link
Member

Hmmmm.... Could you create a simple reproduction repository?

@muyaszed
Copy link

Amazingly now I can't replicate the error even after totally removing the babel-polyfil, ahhaha. Can't really say anything much right now, just sorry for the noise. Most probably it is because I accidentally installed jest-cli over CRA but then later I decide to remove, install and update everything. Cheers

@nitish24p
Copy link

nitish24p commented Oct 8, 2018

Hi @kentcdodds I'm getting this issue
screen shot 2018-10-09 at 1 51 47 am

import React from 'react';
import { render, cleanup, fireEvent } from 'react-testing-library';
import Button from '../';

afterEach(cleanup);

test('renders the button component', () => {
  const { container } = render(<Button>CLICK ME</Button>);
  expect(container).toMatchSnapshot();
});

test('Handle Click', () => {
  const handleClick = jest.fn();
  const { getByText } = render(<Button onClick={handleClick}>SUBMIT</Button>);
  fireEvent.click(getByText('SUBMIT'));
  expect(handleClick).toHaveBeenCalledTimes(1);
});

I am running these tests as part of a precommit hook. My package.json is as shown

{
  ... Other things
   "scripts": {
    "test": "node scripts/custom-config test --env=jsdom",
    "test:staged": "cross-env CI=true node scripts/custom-config test --env=jsdom --findRelatedTests",
    "precommit": "lint-staged",
  },
   "lint-staged": {
    "**/src/**/*.js": [
      "prettier --write",
      "eslint --fix",
      "npm run test:staged",
      "git add"
    ]
  },
}

I am using node 8.1.2 and react-testing-library is on 5.0.1

@kentcdodds
Copy link
Member

That shouldn't be happening.

In your test do this:

console.log(process.version)
console.log(Object.entries)

My guess is that your system is on a recent version of node, but somehow your tests are being run with an older version.

@nitish24p
Copy link

nitish24p commented Oct 8, 2018

Did some digging.. Turns out husky was running the precommit script with some random old node version (6.9.4), not the same as my system, solved it by doing the following

nvm alias default 8.12.0

@kentcdodds
Copy link
Member

My guess is that you have an .nvm file in your project that's saying what version to run 😉

@nitish24p
Copy link

Naa, the library had some issues as enlisted here typicode/husky#77
typicode/husky#247
Anyway thanks :)

ovidiuch added a commit to react-cosmos/react-cosmos that referenced this issue Nov 1, 2018
julienw pushed a commit to julienw/react-testing-library that referenced this issue Dec 20, 2018
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