【问题标题】:Jest error: Mongoose: looks like you're trying to test a Mongoose app with Jest's default jsdom test environmentJest 错误:Mongoose:看起来您正在尝试使用 Jest 的默认 jsdom 测试环境测试 Mongoose 应用程序
【发布时间】:2020-06-04 18:38:44
【问题描述】:
使用 Mongoose 中间件使用 ExpressJS 的 Jest 端点进行测试我遇到了以下错误:
Mongoose: looks like you're trying to test a Mongoose app with Jest's default jsdom test environment. Please make sure you read Mongoose's docs on configuring Jest to test Node.js apps: http://mongoosejs.com/docs/jest.html
有什么问题?
【问题讨论】:
标签:
express
mongoose
jestjs
【解决方案1】:
"scripts": {
"test": "jest --testEnvironment=node --verbose --forceExit --watchAll --maxWorkers=1"
}
- 如上所示,将 --testEnvironment=node 添加到 package.json。
- 然后重新运行“npm test”
问题已解决!
【解决方案2】:
根据Mongoose官方documentation的说法,你需要通过创建jest.config.js来解决这个问题,内容如下:
module.exports = {
testEnvironment: 'node'
};
你猜怎么着?它非常轻松地解决了我的问题:-)