我很难找到关于如何使用jest.fn()
来模拟TypeScript类和类上的方法(例如express的Request
,Response
和NextFunction
以及mongoose模型上的save()
方法)的好资料。
举个例子,假设我有以下模型和控制器:
models/Foo.ts:
import * as mongoose from "mongoose"
export type FooModel = mongoose.Document & {
owner: mongoose.Schema.Types.ObjectId,
bars: string[]
}
const fooSchema = new mongoose.Schema({
owner: { type: mongoose.Schema.Types.ObjectId, ref: "User", index: true },
bars: [String]
}
export const Foo = mongoose.model<FooModel>("Foo", fooSchema)
controllers/foo.ts:
import { Request, Response, NextFunction } from "express";
import { Foo, FooModel } from "../models/Foo";
export let createFoo = async (req: Request, res: Response, next: NextFunction) => {
try {
const foo = new Foo({
owner: req.user._id,
bars: req.body.bars
});
await foo.save();
res.status(200).json(foo);
} catch (err) {
next(err)
}
}
我想添加一些单元测试:
import { Request, Response, NextFunction } from "express";
import { Foo } from "../../src/models/Foo";
import * as fooController from "../../src/controllers/foo";
import {} from "jest";
describe("createFoo", async () => {
let req: Request;
let res: Response;
let next: NextFunction;
const bars = ["baz", "qux", "quux"];
beforeEach(() => {
// Mock req, res and next
// Set req.body.bars equal to bars
// Stub out Foo.save() so that we don't actually save to the db
});
it("should add bars to the foo", async () => {
await fooController.createFoo(req, res, next);
responseData = JSON.parse(res.json)
expect(responseData.bars).toEqual(bars);
});
it("should save the foo", async () => {
await fooController.createFoo(req, res, next);
expect(Foo.prototype.save).toHaveBeenCalled();
}
it("should call next on error", async () => {
const err = new Error();
// Set up Foo.save() to throw err
await fooController.createFoo(req, res, next);
expect(next).toHaveBeenCalledWith(err);
}
});
我遇到的主要问题是关于被注释掉的部分:我还没有想出如何真正地模拟 req
、res
和 next
,或者如何存根 Foo.save()
或使其抛出错误。我该如何做到这一点?