15

When running my tests with jest, I had the above error;

Error: Uncaught [TypeError: array.map(...).flat is not a function]

Following the solution from that issue, https://github.com/kulshekhar/ts-jest/issues/828

I've installed core-js on dependencies and putting this in jest.config.js

setupFiles: ['core-js'],

I'd receive that another error;

Error: Uncaught [Error: Not supported]

And this is occurring only with jest, I'm using babel and webpack on my application and storybook without errors on flat.

My jest.config.js

const PATH = require('./path')

module.exports = {
  setupFilesAfterEnv: ['./rtl.setup.js'],
  moduleFileExtensions: ['js'],
  verbose: true,
  moduleNameMapper: {
    '@components(.*)$': `${PATH.source}/components/$1`
  },
  transform: {
    '^.+\\.js$': 'babel-jest'
  }
}
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Guilherme Bayer
  • 161
  • 1
  • 1
  • 4

4 Answers4

17

per https://github.com/kulshekhar/ts-jest/issues/828#issuecomment-433976880

this problem can be solved by running

npm install --save-dev core-js

and adding core-js to your jest config's setupFiles

"setupFiles": ["core-js"]
Ulad Kasach
  • 11,558
  • 11
  • 61
  • 87
  • 1
    For my create-react-app project I was able to fix this by adding `import 'core-js/stable';` to the top of my `setupTests.ts` file. – Kris Dover Nov 10 '20 at 20:29
  • Did you need to install the dependency or did it work within CRA – Sole Apr 06 '22 at 15:44
5

I installed core-js (@3.4.1) and it worked for me. Also had to add import 'core-js'; at the top of my test file's imports.

Edit: Using it in a create-react-app project that is not ejected, so I don't access webpack files and I don't have a custom config for jest (nor jest-ts).

gkri
  • 1,627
  • 3
  • 18
  • 27
4

This error happens if your node version is old. Try to update it to the latest version.

OlegKusov
  • 43
  • 3
0

Searching more about core-js, since it's substituting @babel/polyfill, that config has worked to me.

[
  "@babel/preset-env",
  {
    "targets": {
      "node": 4
    },
    "useBuiltIns": "usage",
    "corejs": 3
  }
]

And core-js@3 being installed as a dependency.

Guilherme Bayer
  • 161
  • 1
  • 1
  • 4