I have some code using the Object.groupBy
method. This is in codebase using TypeScript 5.5 and everything is working well.
Lately I’ve tried to add some tests and I get the error Property 'groupBy' does not exist on type 'ObjectConstructor'.
Jest is set up to use ts-jest
to transform the TypeScript code:
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\.(ts|tsx|js)$": [
"ts-jest",
{
"tsconfig": "<rootDir>/tsconfig.json",
"isolatedModules": true
}
]
}
and the tsconfig.json
is:
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"strict": true,
"declaration": false,
"esModuleInterop": true,
"noImplicitOverride": true,
"lib": [
"ESNext",
]
},
"include": [
"src"
]
}
As as far as I understand my test code should be transpiled using this config into code valid for ES2018 which should be fully supported by Node 20.
What generates a failure and how can I fix it?
1
According to MDN, Object.groupBy
was only introduced in Node.js 21.0.0.
This is also referenced in the Node.js 21 release notes (see “Array Grouping” in the “Notable Changes” section).
To make a long story short – you need Node.js 21 to use Object.groupBy
, not Node.js 20.
3
I had to add "ESNext"
to compilerOptions.lib
in tsconfig:
"compilerOptions": {
"lib": ["DOM", "ESNext"],