我目前正在为包含函数 formatDateGlobal 的组件 dateFormat.js 编写简单的单元测试(使用 Jest)。这是测试的片段:
import DateFormat from '../dateFormat';
describe('dateFormat.js', () => {
let date1;
beforeEach(() => {
date1 = {
date: '',
};
});
it('Then it should return an empty string', () => {
// Act
const returnedDate = DateFormat.formatDateGlobal(date1);
// Assert
expect(returnedDate).toBe('');
});
在 dateFormat 的底部,我将 formatDateGlobal 函数导出为:
export default formatDateGlobal;
测试按预期构建,但我收到错误
TypeError: _dateFormat.default.formatDateGlobal is not a function
27 | it('Then it should return an empty string', () => {
28 | // Act
> 29 | const returnedDate = DateFormat.formatDateGlobal(date1);
| ^
30 | // Assert
31 | expect(returnedDate).toBe('');
32 | });
不太确定为什么会发生这种情况,但我认为这与我导出函数的方式有关。
最佳答案
您正在将 formatDateGlobal
导入到 DateFormat
变量中。所以你可以使用
const returnedDate = DateFormat(date1);
目前您正在尝试实现与此相同的目标:
const returnedDate = formatDateGlobal.formatDateGlobal(date1);
关于javascript - 为什么我的 Vue 单元测试无法识别函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60726727/