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.4react version: N/Anode version: 6.xnpm (or yarn) version: 6.xSolution
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.
For people who can't upgrade: install and import the babel-polyfill in your test files.
import babel-polyfill
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.
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.
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.
Considering Node 6 is on maintenance LTS, I don't care to support it anymore. We should update our engines config in package.json.
:tada: This issue has been resolved in version 3.1.7 :tada:
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot :package::rocket:
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
Hmmmm.... Could you create a simple reproduction repository?
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
Hi @kentcdodds I'm getting this issue

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
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.
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
My guess is that you have an .nvm file in your project that's saying what version to run 馃槈
Naa, the library had some issues as enlisted here https://github.com/typicode/husky/issues/77
https://github.com/typicode/husky/issues/247
Anyway thanks :)
Most helpful comment
Considering Node 6 is on maintenance LTS, I don't care to support it anymore. We should update our engines config in package.json.