Jest: TypeError: Object.values is not a function

Created on 30 May 2017  路  12Comments  路  Source: facebook/jest

My tests works fine on Jest 19.0.0, but it breaks when I upgrade to Jest 20.0.4 with the following error:

TypeError: Object.values is not a function

this is my .babelrc:

{
  "presets": ["flow", "es2015", "stage-0"]
}

this is the code that breaks:

Object.values(USERS_ROLES)

Most helpful comment

For those reluctant to install a dependency just for this, you can also add this to your setupFile:

Object.values = (obj) => Object.keys(obj).map(key => obj[key])

All 12 comments

We don't require babel-polyfill automatically any more because it lead to memory leaks. Please call it yourself in setupFiles.

@cpojer Is this documented somewhere?

I've tried import 'babel-polyfill in a standalone test setup file and added to setupFiles, but I still get this error. If I run jest --no-cache, it works. If I run jest --findRelatedTests immediately after which runs the same tests, it does not work. 馃槙

I'm running into the same issue. Found that it fails in some versions of node. Tests passed using 7.10.0 but not 6.10.3.

I also got it to work with the older version of node by adding to package.json

    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/babel-polyfill/",
      ...
    ]

and then in my test file adding:

import 'babel-polyfill'

If need to dig into the issue more, look at changes from https://github.com/facebook/jest/pull/2755

This is also causing test failures for us, running node v6.10.3. @lvela's solution (and the one originally posted by @cpojer) worked without needing to unmock babel-polyfill.

This should probably be updated in the docs if this is an expectation for developers.

Why do you need to document "if you call functions in an environment which does not support it, you have to polyfill it"? It makes perfect sense, and it's the same thing you would do if you use Object.values in the browser today.

@SimenB I agree, but the polyfills were called on by Jest before. Now they're not. It's in the changelog as a breaking change, that satisfied me once I was pointed to it. :) Kind of a subtle line tho... could have been bolder ;)

Just stumbled upon this thread with the same issue, if it helps anyone here's how I fixed it:
Installed npm package object.values: https://www.npmjs.com/package/object.values
Here's my setupFiles:

    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],

In polyfills.js I add the following line:

Object.values = require('object.values');

What this does I believe is tell Jest that when Object.values is called, use the implementation in the npm package that should mirror the behavior of the real Object.values

For those reluctant to install a dependency just for this, you can also add this to your setupFile:

Object.values = (obj) => Object.keys(obj).map(key => obj[key])

I put a simple require in a other wise empty setup file, and it worked fine, had the babel polyfill used for older browsers anyway

aurelia-jest-bl-bootstrap.js

// https://github.com/facebook/jest/issues/3687 require('babel-polyfill');

jest-bl-cli-version.json

{ ... "setupFiles": [ "../test/aurelia-jest-bl-bootstrap.js" ], "modulePaths": [ "../src", "../node_modules" ], "moduleFileExtensions": [ "js", "json" ], "moduleNameMapper": { "aurelia-(.*)": "../node_modules/$1", "(.*)/ft-(.*)": "../src/$1/ft-$2" } }

I got the same error message, when running tests in Travis, which passed locally but failed in CI and landed here. I found a different solution I'd just like to share so I or anyone else can find it later.

In my case, I was testing with node 6.10.something and Stack Overflow pointed out that Object.values is not a function in node 6, but it is in 7. So I switched to 7, but yarn doesn't work with 7, so I switched to Node 8, which works.

In .travis.yml

node_js:
  - "7"

If anyone runs into this and can't figure out why, look to see if you are running an older version of Node

Was this page helpful?
0 / 5 - 0 ratings