![](/img/trans.png)
[英]jest.mock(): How to mock ES6 class default import using factory parameter
[英]How to mock es6 class using Jest
我试图用jest模拟一个Mailer
类,我无法弄清楚如何去做。 文档没有提供很多关于它如何工作的例子。 过程是我将触发一个节点事件password-reset
,当该事件被触发时,我想使用Mailer.send(to, subject, body)
发送电子邮件。 这是我的目录结构:
project_root
-- __test__
---- server
------ services
-------- emails
---------- mailer.test.js
-- server
---- services
------ emails
-------- mailer.js
-------- __mocks__
---------- mailer.js
这是我的模拟文件__mocks__/mailer.js
:
const Mailer = jest.genMockFromModule('Mailer');
function send(to, subject, body) {
return { to, subject, body };
}
module.exports = Mailer;
和我的mailer.test.js
const EventEmitter = require('events');
const Mailer = jest.mock('../../../../server/services/emails/mailer');
test('sends an email when the password-reset event is fired', () => {
const send = Mailer.send();
const event = new EventEmitter();
event.emit('password-reset');
expect(send).toHaveBeenCalled();
});
最后我的mailer.js
类:
class Mailer {
constructor() {
this.mailgun = require('mailgun-js')({
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
});
}
send(to, subject, body) {
return new Promise((reject, resolve) => {
this.mailgun.messages().send({
from: 'Securely App <friendly-robot@securelyapp.com>',
to,
subject: subject,
html: body,
}, (error, body) => {
if (error) {
return reject(error);
}
return resolve('The email was sent successfully!');
});
});
}
}
module.exports = new Mailer();
那么,如何使用Jest成功模拟和测试这个类呢? 非常感谢您的帮助!
您不必模拟邮件程序类,而是mailgun-js
模块。 所以mailgun是一个函数,它返回返回函数send
的函数messages
。 所以模拟看起来像这样。
为了快乐的道路
const happyPath = () => ({
messages: () => ({
send: (args, callback) => callback()
})
})
对于错误情况
const errorCase = () => ({
messages: () => ({
send: (args, callback) => callback('someError')
})
})
因为你有这2个案例,所以在你的测试中模拟模块是有意义的。 首先,您必须使用一个简单的间谍来模拟它,我们稍后可以为我们的案例设置实现,然后我们必须导入模块。
jest.mock('mailgun-js', jest.fn())
import mailgun from 'mailgun-js'
import Mailer from '../../../../server/services/emails/mailer'
由于您的模块使用promises,我们有2个选项要么从测试中返回promise,要么使用async/await
。 我使用后面的更多信息看看这里 。
test('test the happy path', async() => {
//mock the mailgun so it returns our happy path mock
mailgun.mockImplementation(() => happyPath)
//we need to use async/awit here to let jest recognize the promise
const send = await Mailer.send();
expect(send).toBe('The email was sent successfully!')
});
如果你想测试使用正确的参数调用mailgun send
方法,你需要调整mock如下:
const send = jest.fn((args, callback) => callback())
const happyPath = () => ({
messages: () => ({
send: send
})
})
现在您可以检查发送的第一个参数是否正确:
expect(send.mock.calls[0][0]).toMatchSnapshot()
仅供Google员工和未来的访问者使用,以下是我为ES6课程设置开玩笑的方法。 我在github上也有一个工作示例 ,使用babel-jest来转换ES模块语法,以便jest可以正确地模拟它们。
__mocks __ / MockedClass.js
const stub = {
someMethod: jest.fn(),
someAttribute: true
}
module.exports = () => stub;
您的代码可以使用new调用它,在测试中,您可以调用该函数并覆盖任何默认实现。
example.spec.js
const mockedClass = require("path/to/MockedClass")();
const AnotherClass = require("path/to/AnotherClass");
let anotherClass;
jest.mock("path/to/MockedClass");
describe("AnotherClass", () => {
beforeEach(() => {
mockedClass.someMethod.mockImplementation(() => {
return { "foo": "bar" };
});
anotherClass = new AnotherClass();
});
describe("on init", () => {
beforeEach(() => {
anotherClass.init();
});
it("uses a mock", () => {
expect(mockedClass.someMethod.toHaveBeenCalled();
expect(anotherClass.settings)
.toEqual(expect.objectContaining({ "foo": "bar" }));
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.