Ryan Castner
Ryan Castner

Reputation: 1054

TypeError: Object.values is not a function -- How to correctly polyfill with babel-preset-env in jest?

I get the following error after upgrading to Jest v20 where they removed the automatic babel-polyfill due to memory leaks:

TypeError: Object.values is not a function

I realize I need to polyfill this on my own now, I am using babel-preset-env and have the following .babelrc file:

  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    },
    "test": {
      "presets": [
        "react",
        "stage-3",
        ["env", {
          "targets": {
            "browsers": [
              "firefox >= 36",
              "chrome >= 38",
              "opera >= 25",
              "safari >= 9",
              "ios >= 9"
            ],
            "node": "6.11.4"
          },
          "useBuiltIns": "usage",
          "include": ["es7.object.values"],
          "debug": true
        }],
        "jest"
      ],
      "plugins": [
        "transform-class-properties"
      ],
    }
  }

I can see that es7.object.values is being polyfilled in the debug output:

Using polyfills:
  ...
  es7.object.values {"chrome":"38","firefox":"36","ios":"9","safari":"9","node":"6.11.4"}

But I am still getting the error message, help!

Upvotes: 6

Views: 4142

Answers (2)

Behnam Azimi
Behnam Azimi

Reputation: 2488

In my case, the Node version was the reason. I just updated the node 6 to node 7 and it fixed.

Upvotes: 0

Wiktor Czajkowski
Wiktor Czajkowski

Reputation: 1762

Some of the options are:

  1. bump node version to the one supporting Object.values (which seems to be 7.0 judging from this answer),
  2. polyfill it using babel-polyfill (via import 'babel-polyfill' in setupTests.js file).

Upvotes: 2

Related Questions