【发布时间】:2019-04-24 12:34:34
【问题描述】:
我有一些使用nodemailer 模块的代码。
在路由器(router.js)中,我有
const transporter = nodeMailer.createTransport(emailArgs);
然后在路线内 (/login) 我有:
...
return transporter.sendMail(mailOptions);
我正在尝试使用jest 测试框架测试这条路线。我在模拟对sendMail 的调用时遇到了一些麻烦。我阅读了 this nice blogpost 关于如何使用 jest mocking 的信息,但我收到了这个错误:
TypeError: 无法读取未定义的属性“sendMail”
确实,当我检查 transporter 的值时,它是未定义的。
这是我的测试代码(不起作用):
import request from "supertest";
import router from "./router";
jest.mock("nodemailer");
describe("", () => {
...
test("", async () => {
// 1 - 200 status code; 2 - check email was sent
expect.assertions(2);
const response = await request(router)
.post("/login")
// global variable
.send({ "email": email })
.set("Accept", "application/json")
.expect("Content-Type", /json/);
// should complete successfully
expect(response.status).toBe(200);
// TODO not sure how to express the expect statement here
});
});
所以我的问题是如何模拟模块返回的类实例的方法?
【问题讨论】:
标签: node.js unit-testing mocking jestjs nodemailer