You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest-MongoDB与TypeScript预设加载失败及全局变量报错问题

问题:无法加载@Shelf/jest-mongodb预设,global.__MONGO_URI__返回undefined

问题现象

无法加载@shelf/jest-mongodb预设,涉及配置文件jest.config.js,关键配置片段:

preset: "@shelf/jest-mongodb",

问题摘要

  • 核心问题:global.__MONGO_URI__返回undefined
  • 测试命令(package.json):"test": "jest --passWithNoTests"
  • 运行环境:Windows系统 + GitBash
  • 执行测试时已生成包含uri和dbname的globalConfig.json文件,但问题依旧存在

报错信息

error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no 

配置信息

jest-mongodb-config.js

module.exports = {
  mongodbMemoryServerOptions: {
    instance: {
      dbName: 'jest',
    },
    binary: {
      version: '4.0.3',
      skipMD5: true,
    },
    autoStart: false,
  },
};

jest.config.js

module.exports = {
  roots: ["<rootDir>/src", "<rootDir>/tests"],
  collectCoverageFrom: [
    '<rootDir>/src/**/*.ts',
  ],
  testEnvironment: 'node',
  preset: "@shelf/jest-mongodb",
  coverageDirectory: "coverage",
  testEnvironment: "node",
  transform: {
    ".+\\.ts$": "ts-jest",
  },
  moduleNameMapper: {
    '@/tests/(.*)': '<rootDir>/tests/$1',
    '@/(.*)': '<rootDir>/src/$1'
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$", 
};

代码使用示例

beforeAll(async () => {
  client = await MongoClient.connect(global.__MONGO_URI__, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  } as ConnectOptions)
  db = client.db()
})

已尝试方案

  • 重新安装@shelf/jest-mongodb
  • 按照官方文档创建新项目测试
  • 重载、关闭并重新打开VSCode
  • 尝试使用process.env.__MONGO_URL__替代global.__MONGO_URI__
  • 分别使用npm和yarn安装依赖:
    npm install --save-dev @shelf/jest-mongodb
    
    yarn add @shelf/jest-mongodb --dev
    

解决办法

1. 修复TypeScript全局类型报错

报错源于TypeScript默认不识别global.__MONGO_URI__,需要扩展全局类型:
在项目中创建类型声明文件(比如src/types/global.d.ts),添加以下内容:

declare global {
  namespace NodeJS {
    interface Global {
      __MONGO_URI__: string;
      __MONGO_DB_NAME__: string;
    }
  }
}

export {};

然后在tsconfig.json中确保类型目录被识别:

{
  "compilerOptions": {
    "typeRoots": ["./src/types", "./node_modules/@types"]
  }
}

2. 清理jest.config.js的重复配置

你的jest配置里重复设置了两次testEnvironment: 'node',而@shelf/jest-mongodb预设已经自带对应的测试环境,重复设置会导致冲突,删除其中一个即可:

module.exports = {
  roots: ["<rootDir>/src", "<rootDir>/tests"],
  collectCoverageFrom: [
    '<rootDir>/src/**/*.ts',
  ],
  preset: "@shelf/jest-mongodb",
  coverageDirectory: "coverage",
  transform: {
    ".+\\.ts$": "ts-jest",
  },
  moduleNameMapper: {
    '@/tests/(.*)': '<rootDir>/tests/$1',
    '@/(.*)': '<rootDir>/src/$1'
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$", 
};

3. 切换终端环境测试

Windows下GitBash可能存在环境变量传递问题,尝试用CMD或PowerShell执行测试命令,验证global.__MONGO_URI__是否能正常获取值。

4. 显式指定jest-mongodb配置文件路径

如果jest无法自动识别jest-mongodb-config.js,在jest.config.js里显式指定路径:

module.exports = {
  // ...其他配置
  preset: "@shelf/jest-mongodb",
  globals: {
    'jest-mongodb': {
      configPath: './jest-mongodb-config.js'
    }
  }
};

内容的提问来源于stack exchange,提问作者Sergio Pile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:45:06