[英]Jest toMatchObject with MongoDB confusing
I have a test on my mongoose model and while one models' tests are running completly fine, another one which is basically a copy - does not work.我对我的 mongoose model 进行了测试,虽然一个模型的测试运行完全正常,但另一个基本上是副本的测试不起作用。 And honestly I don't understand the problem.
老实说,我不明白这个问题。 I tried to remove some of the properties or add hardcoded values to really match 100% exactly - but somehow i always get a similar error.
我试图删除一些属性或添加硬编码值以真正匹配 100%——但不知何故我总是得到类似的错误。
The error.错误。 What drives me crazy is that I tried to remove/add the "_id" and the "createdOn" field but at least the "_id" always appear in the error.
让我发疯的是我试图删除/添加“_id”和“createdOn”字段,但至少“_id”总是出现在错误中。 As said above in another model the test does not complain about the "_id" because I do not validate it there...I just don't get it.
正如上面在另一个 model 中所说,测试不会抱怨“_id”,因为我没有在那里验证它......我只是不明白。
insert › Should save a channel
expect(received).toMatchObject(expected)
- Expected - 1
+ Received + 2
@@ -1,8 +1,9 @@
Object {
+ "_id": "5e962f1dc133d8b92891ddaf",
"createdBy": "5e962f1dc133d8b92891ddae",
- "createdOn": Anything,
+ "createdOn": 2020-04-14T21:46:05.907Z,
"hasAcceptedLegal": false,
"isActive": true,
"isPublic": false,
"members": Array [
"5e962f1dc133d8b92891ddae",
48 | expect(spy).toHaveBeenCalled();
49 |
> 50 | expect(channel).toMatchObject({
| ^
51 | name: mockName,
52 | createdBy: mockCreatedById,
53 | createdOn: expect.anything(),
at Object.it (test/model/channel.model.test.ts:50:21)
The respective model各自model
import mongoose, { Schema, Document } from "mongoose";
import { IUser } from "./user.model";
export interface IChannel extends Document {
name: string;
createdBy: IUser["id"];
createdOn: Date;
isActive: boolean;
isPublic: boolean;
hasAcceptedLegal: boolean;
members: [IUser["id"]];
}
const ChannelSchema: Schema = new Schema({
name: { type: String, required: true },
createdBy: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
createdOn: { type: Date },
isActive: { type: Boolean, default: true },
isPublic: { type: Boolean, default: false },
hasAcceptedLegal: { type: Boolean, default: false },
members: [
{
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
],
});
export default mongoose.model<IChannel>("Channel", ChannelSchema);
The test:考试:
it("Should save a channel", async () => {
expect.assertions(2);
let mockName = Faker.company.companyName();
let mockCreatedById = Types.ObjectId();
let mockCreatedOn = Date.now();
const channel: IChannel = new Channel({
name: mockName,
createdBy: mockCreatedById,
createdOn: mockCreatedOn,
members: [mockCreatedById],
});
const spy = jest.spyOn(channel, "save");
channel.save();
expect(spy).toHaveBeenCalled();
expect(channel).toMatchObject({
name: mockName,
createdBy: mockCreatedById,
createdOn: expect.anything(),
isActive: true,
isPublic: false,
hasAcceptedLegal: false,
members: [mockCreatedById],
});
});
just for reference, I found sth.仅供参考,我找到了某事。 that the returned mongoose object should be converted with ".toJSON()" and then it works, but even then it was having a problem with the "createdOn" field as it seems to be a formatted Date or sth.
返回的 mongoose object 应该用“.toJSON()”转换,然后它就可以工作,但即便如此,“createdOn”字段也有问题,因为它似乎是格式化的日期或某物。 like this (no string, parenthesis missing).
像这样(没有字符串,缺少括号)。 What I finally did now was the following and now it works:
我现在最终做的是以下内容,现在它可以工作了:
it("Should save a channel (required+defaults)", async () => {
expect.assertions(2);
let mockName = Faker.company.companyName();
let mockCreatedById = Types.ObjectId();
let mockCreatedOn = new Date();
const channel: IChannel = new Channel({
name: mockName,
createdBy: mockCreatedById,
createdOn: mockCreatedOn,
members: [mockCreatedById],
});
const spy = jest.spyOn(channel, "save");
channel.save();
expect(spy).toHaveBeenCalled();
expect(channel.toJSON()).toMatchObject({
_id: expect.anything(),
name: mockName,
createdBy: mockCreatedById,
createdOn: expect.anything(),
isActive: true,
isPublic: false,
hasAcceptedLegal: false,
members: expect.arrayContaining([mockCreatedById]),
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.