我正在使用jest
和enzyme
进行单元测试。以下是我的index.js
文件。我需要测试文件的openNotification
和uploadErrorNotification
功能。但是,仅导出uploadErrorNotification
函数。因此,我该如何测试这两个功能。
此外,除了jest
和enzyme
之外,我不想使用任何其他libray。
//index.js
import {
notification
} from 'antd';
const openNotification = (message, description, className) => {
notification.open({
key: 'upload-template',
message,
description,
placement: "bottomRight",
duration: null,
});
};
const uploadErrorNotification = (uploadFailedText, errorMsg) => {
openNotification(uploadFailedText, errorMsg, 'error');
};
export {
uploadErrorNotification
}
这是我的测试文件:
//test.js
import { uploadErrorNotification } from '../index.js
jest.mock('notification', () => ({ open: () => jest.fn() })); // was trying this but I couldn't understand how it will work
describe('Notification validation functions testing', () => {
uploadErrorNotification('Upload failed', 'Something went wrong.');
expect("openNotification").toHaveBeenCalledTimes(1); // want to do something like this
});
答案 0 :(得分:0)
您必须模拟外部依赖关系:
首先模拟my_object.prop.static_method(my_object)
,以使antd
是间谍
notification.open
然后将模块导入测试中
jest.mock('antd', () => ({notification: open: {jest.fn()}}))
知道您可以像这样使用它:
import { notification } from 'antd';