我正在使用jest
和enzyme
进行单元测试。下面是我的index.js
文件。我需要测试文件的openNotification
和uploadErrorNotification
函数。但是,只有uploadErrorNotification
函数被导出。那么,我如何测试这两个函数呢?
此外,除了jest
和enzyme
之外,我不想使用任何其他库。
//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
});
2条答案
按热度按时间eufgjt7s1#
你不得不嘲笑外部依赖:
首先模拟
antd
,使notification.open
成为间谍然后将模块导入到测试中
知道你可以这样使用它:
6l7fqoea2#
如果你想在不覆盖其他antd组件的情况下测试通知,你可以添加jest.requireActual('antd ')。