用笑话嘲笑mongoose模型
我试图用jest
来嘲笑mongoose模型,但越来越Cannot create property 'constructor' on number '1'
错误的Cannot create property 'constructor' on number '1'
。 我能够通过创build具有以下2个文件的项目来重现该问题。 有没有办法用jest
模仿mongoose模型?
./model.js
const mongoose = require('mongoose') const Schema = mongoose.Schema const schema = new Schema({ name: String }) module.exports = mongoose.model('Test', schema)
./model.test.js
jest.mock('./model') const Test = require('./model') // Test.findOne.mockImplementation = () => { // ... // }
错误:
FAIL ./model.test.js ● Test suite failed to run TypeError: Cannot create property 'constructor' on number '1' at ModuleMockerClass._generateMock (../../jitta/sandbox/rest_api/node_modules/jest-mock/build/index.js:458:34) at Array.forEach (native) at Array.forEach (native) at Array.forEach (native)
更新:
似乎是一个开玩笑的bug。 https://github.com/facebook/jest/issues/3073
好吧,我有同样的问题,所以我创作这个包来解决这个问题: https : //www.npmjs.com/package/mockingoose
这是你如何使用它,让我们说这是你的模型:
import mongoose from 'mongoose'; const { Schema } = mongoose; const schema = Schema({ name: String, email: String, created: { type: Date, default: Date.now } }) export default mongoose.model('User', schema);
这是你的testing:
it('should find', () => { mockingoose.User.toReturn({ name: 2 }); return User .find() .where('name') .in([1]) .then(result => { expect(result).toEqual({ name: 2 }); }) });
结帐testing文件夹更多的例子: https : //github.com/alonronin/mockingoose/blob/master/ tests /index.test.js
没有连接到数据库!
- babel-jest ES2015import报表
- reactJs Jest:`jsdom 4.x以前只能用于io.js,而不是Node.jsT:
- Jest:找不到模块内部需要testing的模块(相对path)
- 让Jest在testing时忽略无用的导入
- EADDRINUSE 127.0.0.1:5858在jesttesting期间debugging
- Jesttesting简单的香草JavaScript – 不能读取null的属性'addEventListener'
- 用Jesttesting文件系统?
- 在CircleCI上运行Jest时出错:“npmtesting意外死亡”
- 笑npm脚本的正则expression式