【问题标题】:I want to write test cases for function with parameter using jest我想使用 jest 为带有参数的函数编写测试用例
【发布时间】:2021-08-06 11:46:00
【问题描述】:

我有一个函数接受一个 yyyy-mm-dd 格式的参数。我正在获取今天的日期并将该日期与参数中收到的日期进行比较。

我想用 jest 写测试用例,首先它应该检查传入参数的日期是 yyyy-mm-dd 格式。

所以我的功能大致是这样的:

function dateFunction(parameter in yyyy-mm-dd format){

today = new Date() // it is taking todays date.
convertedDate = converting parameter date into date format.
 
if (today === convertedDate){
// then do something
}
else {
   // do something
  }
}

【问题讨论】:

    标签: javascript node.js unit-testing jestjs


    【解决方案1】:
    test("The function parameter should be in yyyy-mm-dd format", () => {
        const dateToBeChecked = '2021-12-17';
        const dateFormat = (/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/.test(dateToBeChecked));
        expect(dateFunction(dateToBeChecked)).toBe(dateFormat)
    })
    

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 2012-08-04
      • 2016-08-22
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多