在不返回的情况下测试嵌套函数



我有一个notfications.js文件,我想为它编写笑话测试。有人能告诉我如何处理这个测试用例吗。

import { store } from 'react-notifications-component';
/**
* Helper function to add a notification anywhere.
* @param {*} title string
* @param {*} message string
* @param {*} type  string of success, danger, info, default, warning
*/
const addNotification = (title, message, type) => {
const options = {
title,
message,
type,
insert: 'top',
container: 'top-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
};
store.addNotification({
...options,
dismiss: {
duration: 5000,
onScreen: true,
pauseOnHover: true,
},
});
};
export default addNotification;

我读了一些笑话文档,也读了一些mock函数,但没能弄清楚太多。第一次尝试编写单元测试。

解决方案:我能够通过使用jest-spyOn为商店创建一个间谍来进行测试。我的测试是:

const params = {
title: 'test',
type: 'success',
message: 'this is a test notification!',
};
describe('addNotification', () => {
beforeAll(() => {
jest.spyOn(store, 'addNotification');
});
afterAll(() => {
jest.restoreAllMocks();
});
test('calls store.addNotification()', () => {
addNotification(params.title, params.message, params.type);
expect(store.addNotification).toHaveBeenCalled();
});
});

注意请参阅@slidep2提供的答案这是一个更好的解决方案

您需要使用jest.mock(moduleName,factory,options(来模拟react-notifications-component包。我添加了{ virtual: true }选项,因为我没有安装这个软件包,只是为了演示。如果已安装此软件包,则可以删除此选项。

例如

notifications.js:

import { store } from 'react-notifications-component';
const addNotification = (title, message, type) => {
const options = {
title,
message,
type,
insert: 'top',
container: 'top-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
};
store.addNotification({
...options,
dismiss: {
duration: 5000,
onScreen: true,
pauseOnHover: true,
},
});
};
export default addNotification;

notifications.test.js:

import addNotification from './notifications';
import { store } from 'react-notifications-component';
jest.mock(
'react-notifications-component',
() => {
const mStore = {
addNotification: jest.fn(),
};
return { store: mStore };
},
{ virtual: true },
);
describe('61461299', () => {
it('should pass', () => {
const title = 'jest';
const message = 'unit testing';
const type = 'ok';
addNotification(title, message, type);
expect(store.addNotification).toBeCalledWith({
title,
message,
type,
insert: 'top',
container: 'top-right',
animationIn: ['animated', 'fadeIn'],
animationOut: ['animated', 'fadeOut'],
dismiss: {
duration: 5000,
onScreen: true,
pauseOnHover: true,
},
});
});
});

100%覆盖率的单元测试结果:

PASS  stackoverflow/61461299/notifications.test.js (11.362s)
61461299
✓ should pass (4ms)
------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |     100 |      100 |     100 |     100 |                   
notifications.js |     100 |      100 |     100 |     100 |                   
------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        13.33s

源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61461299

最新更新