javascript - 匹配器错误:接收到的值必须是模拟或间谍函数 - 值未定义
问题描述
错误就在身边getSourceURL
,并且能够模拟它的响应,因为它是在内部调用中调用的logger
:
import { logger, getSourceURL } from '@server/utils/logger';
jest.mock('@server/utils/logger', () => ({
getSourceURL: jest.fn()
}));
getSourceURL.mockReturnValue('https://localhost:3000/women')
jest.js
describe('Has Error Response', () => {
it('Should log an error message', () => {
expect(logger).toHaveBeenCalledWith(
`Response Error - Time: NaNms - response status: ${status}`
);
});
});
/utiles/logger
const getSourceURL = (req, path = '') => {
const protocol = getProtocol(req);
const urlPath = path === '' && req && typeof req?.reqPath !== 'undefined' ? req?.reqPath : path;
return printSourceURL(`${protocol}://${req.headers.host}${urlPath}`);
};
const logger = (message, severity = 'I') => {
const output = `${getDateTime()} ${process.pid} ${severity} ${message}\n`;
console.log(output);
};
const logAccess = (req, res, timeTaken) => {
const output = 'something'
logger(output, 'A');
return output;
};
开玩笑的错误:
Error: expect(received).toHaveBeenCalledWith(...expected)
Matcher error: received value must be a mock or spy function
Received has value: undefined
解决方案
推荐阅读
- java - 为冒泡排序创建计数器
- css - 用css放两个圆形梯形,
- python - Python 编码重复相同的字符串 10 次,每个字符串都有计数
- python - 在Robot Framework Selenium中没有唯一ID的情况下如何单击元素
- r - 如何在文档标题前插入徽标?
- reactjs - React/Redux - 将 this.props.history 传递给 thunk 是一种反模式吗?
- laravel-5 - 为什么图片值没有从 laravel 表单上传到数据库?
- python-3.x - pandas:csv文件,为什么连接后有缺失值?
- python - 如何在 Python 中将多个值从一个函数返回到另一个函数?
- java - android.view.InflateException 和 NavigationView 错误导致的错误