'MongoDb can't use types in jest - declares ... locally, but it is not exported

I use the mongodb library in TypeScript for a few functions (e.g. lambda functions but that should not matter).

The imports look like that:

import { ObjectId, Db, InsertOneResult } from "mongodb";

When I import the function in Jest and execute it I always get the error:

Module '"../../node_modules/mongodb/mongodb.ts34"' declares 'ObjectId' locally, but it is not exported.

I think something is wrong with my jest logic but I am not sure what. The function itself is working.

My jest config looks like that:

const tsPreset = require("ts-jest/jest-preset");

module.exports = {
  ...tsPreset,
  testEnvironment: "node",
  roots: ["<rootDir>/test"],
  testMatch: ["**/*.test.ts"],
  moduleNameMapper: {
    "^@/types/(.*)$": "<rootDir>/types/$1",
    "^@/lib/(.*)$": "<rootDir>/lib/$1",
    "^@/functions/(.*)$": "<rootDir>/functions/$1",
  },
};



Solution 1:[1]

This appears to be related to this ticket. The current versions of the mongodb node driver are no longer compatible with typescript 3.9.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Graham Lee