简体   繁体   English

为什么我的 Vue 单元测试无法识别函数?

[英]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.

相关问题 为什么Drupal无法识别我的JavaScript函数? - Why is Drupal not recognizing my javascript function? 当对我的 firebase auth mock 进行多次调用时,为什么在我的 vue.js 单元测试中需要额外等待? - Why is an extra await required in my vue.js unit test when more than one call is made to my firebase auth mock? 为什么我的函数无法识别我的全局变量? (JavaScript)的 - Why is my function not recognizing my global variables? (Javascript) 为什么 Visual Studio Code 将我的 function 识别为构造函数 function? - Why is Visual Studio Code recognizing my function as a constructor function? 为什么我的反应单元测试导入不起作用? - Why is my react unit test import not working? 为什么我的茉莉花单元测试不等待“完成”? - Why is my jasmine unit test not waiting for 'done'? 无法使用 Jest 在我的单元测试中呈现 Vue 组件 - Cannot render Vue component in my unit test with Jest Vue.js + Nuxt.js-在单元测试head()方法时,为什么我的计算属性未定义? - Vue.js + Nuxt.js - Why is my computed property undefined when I unit test a head() method? 在我的情况下如何对功能进行单元测试 - How to unit test a function in my case 运行单元测试时,在 onMounted 中未定义 Vue Pinia 函数 - Vue Pinia function is undefined in onMounted when unit test is ran
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM