.flat is not a function only with jest
Asked Answered
D

4

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'
  }
}
Dawes answered 20/7, 2019 at 2:6 Comment(0)
P
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"]
Polarize answered 11/12, 2019 at 12:3 Comment(2)
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.Wendt
Did you need to install the dependency or did it work within CRAInaudible
A
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).

Anzio answered 20/11, 2019 at 9:54 Comment(0)
D
4

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

Dicho answered 12/3, 2021 at 13:34 Comment(0)
D
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.

Dawes answered 21/7, 2019 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.