Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jestjs

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!

like image 614
Ryan Castner Avatar asked Oct 12 '17 15:10

Ryan Castner


1 Answers

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).
like image 122
Wiktor Czajkowski Avatar answered Nov 16 '22 02:11

Wiktor Czajkowski