node.js - MongoDb 在 jest 中无法使用类型 - 声明为本地的,但未导出
我在 TypeScript 中使用 mongodb 库来实现一些函数(例如 lambda 函数,但那应该无关紧要)。
导入看起来是这样的:
import { ObjectId, Db, InsertOneResult } from "mongodb";
当我在 Jest 中导入该函数并执行它时,我总是收到以下错误:
Module '"../../node_modules/mongodb/mongodb.ts34"' declares 'ObjectId' locally, but it is not exported.
我觉得我的 jest 逻辑有问题,但我不确定是什么。函数本身是可以工作的。
我的 Jest 配置看起来是这样的:
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",
},
};
答案#1
这似乎与此工单有关。当前版本的 MongoDB 节点驱动程序不再与 TypeScript 3.9 兼容。