[英]I want to write test cases for function with parameter using jest
我有一個 function,它接受一個 yyyy-mm-dd 格式的參數。 我正在獲取今天的日期並將該日期與參數中收到的日期進行比較。
我想用 jest 編寫測試用例,首先它應該檢查傳入參數的日期是否為 yyyy-mm-dd 格式。
所以我的 function 大致是這樣的:
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
}
}
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)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.