[英]Why is my Vue unit test not recognizing a function?
I'm currently writing simple unit tests (using Jest) for my component dateFormat.js that contains the function formatDateGlobal.我目前正在为包含函数 formatDateGlobal 的组件 dateFormat.js 编写简单的单元测试(使用 Jest)。 Here's a snippet of the test:
这是测试的一个片段:
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('');
});
At the bottom of dateFormat, I'm exporting the formatDateGlobal function as so:在 dateFormat 的底部,我将 formatDateGlobal 函数导出为:
export default formatDateGlobal;
The tests are built as expected, but I'm getting the error测试按预期构建,但我收到错误
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 | });
Not really sure why this is happening, but I'm thinking it has to do with the way I'm exporting the function.不太确定为什么会发生这种情况,但我认为这与我导出函数的方式有关。
You are importing formatDateGlobal
into DateFormat
variable.您正在将
formatDateGlobal
导入DateFormat
变量。 So you can use const returnedDate = DateFormat(date1);
所以你可以使用
const returnedDate = DateFormat(date1);
currently you're trying to achieve equivalent of this: const returnedDate = formatDateGlobal.formatDateGlobal(date1);
目前您正在尝试实现与此等效的:
const returnedDate = formatDateGlobal.formatDateGlobal(date1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.