简体   繁体   English

TS-JEST Mongoose/MongoDB 与 ESM 不兼容

[英]TS-JEST Mongoose/MongoDB incompatibility with ESM

I am trying to get Node Express/Typescript app that uses Mongoose (which drags in Mongodb) to work with Jest.我正在尝试让 Node Express/Typescript 应用程序使用 Mongoose(在 Mongodb 中拖动)与 Jest 一起使用。 I know what the error is, I just can't find the correct configuration to get jest and mongodb to play nicely.我知道错误是什么,我只是找不到正确的配置来开玩笑和 mongodb 玩得很好。

No matter what I try I either get:无论我尝试什么,我都会得到:

chart-review-server2.0/node_modules/mongodb/src/bson.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import type {
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

or if I try to exclude it from the config using transformIgnorePatterns in my jest.config.js configuration like this: transformIgnorePatterns: ['node_modules/(?,(mongodb))'], I get:或者如果我尝试在我的 jest.config.js 配置中使用 transformIgnorePatterns 从配置中排除它:transformIgnorePatterns: ['node_modules/(?,(mongodb))'],我得到:

RangeError: Maximum call stack size exceeded

      at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)
      at Object.get [as ObjectId] (node_modules/mongodb/src/bson.ts:38:3)...

My jest.config.js file looks like this:我的 jest.config.js 文件如下所示:

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  transformIgnorePatterns: [`node_modules/(?!(mongodb))`],
  transform: {
    '^.+\\.ts?$': 'ts-jest'
  },
  moduleDirectories: [ "node_modules", "src", "test"],
  verbose: true,
  testMatch: ["<rootDir>/test/*(*.)+(test).+(ts)"],
  setupFiles: [
    'dotenv/config'
  ],
};

my tsconfig.json looks like this:我的 tsconfig.json 看起来像这样:

{
  "compilerOptions": {
    "lib": [
      "es6"
    ],
    "module": "commonjs",
    "target": "es5",
    "rootDirs": [
      "./src"
    ],
    "outDir": "./dist",
    "baseUrl": "./src",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "allowJs": true,
    "downlevelIteration": true,
    "isolatedModules": false,
    "noEmit": true

  },
  "include": [
    "src/**/*.ts",
    "test/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "types": [
    "node"
  ],
  "typeRoots": [
    "node_modules/@types"
  ]
}

Thanks in advance for any replies, and for reading this.提前感谢您的任何回复,并阅读本文。

Commenting out moduleDirectories on jest.config.js solved the issue for me.注释掉moduleDirectories上的 moduleDirectories 为我解决了这个问题。 All credits to @jimmythecode, see his answer . @jimmythecode 的所有功劳,请参阅他的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM