in Education by
I have this cypress test where Im checking for a correct billing date. Our website has monthly subscriptions and it works as follows: If you start your subscription on January 31st, your next billing date will automatically be on the 1st of March since February has only 28 days. Same if you start your subscription on the 31st of March, then your next billing date will be on the first of 1st of May since there is no 31st in April and it automatically changes to the first day of the next month. Starting on other normal dates like the 15th will always be the same date (15th) of the next month etc.. My issue is when testing this with cypress, i always get the last day of the next month. For example if i test that Im gonna start my subscription on the 31st of March, my test will have 30th of April as an expected result, which is not correct since i want the expected result of my test to be 1st of May. I am using this function but i cant seem to make it work properly since there are many differences in the months. export const getBillingDate = (todayDate: string, months: number) => { const date1 = new Date(todayDate) const date2 = new Date(todayDate) date1.setDate(1) const daysInNextMonth = getDaysInMonth(addMonths(date1, months)) date2.setDate(Math.min(date2.getDate(), daysInNextMonth)) return format(addMonths(date2, months), 'MMMM do, yyyy') } I would really appreciate anyone's help with this since i am new to Cypress and testing in general. (Sorry english is not my first language) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Both dayjs and javascript new Date() fail to add all the dates exactly as you want. But you can use dayjs().daysInMonth() to get results exactly as per your description, const getBilling = (startDate) => { const [year, month, day] = startDate.split('/') const sd = dayjs(startDate) const firstOfNextMonth = sd.month(sd.month() + 1).date(1) const daysInNextMonth = dayjs(firstOfNextMonth).daysInMonth() let end; if (daysInNextMonth < day) { end = `${year}/${+month+2}/${1}` // always bump to 1st day of month + 2 } else { end = `${year}/${+month+1}/${day}` } return dayjs(end, 'YYYY/MM/DD').format('YYYY/MM/DD') } it('gets billing date, accounting for short months', () => { //Jan expect(getBilling('2022/01/15')).to.eq('2022/02/15') expect(getBilling('2022/01/31')).to.eq('2022/03/01') //Feb expect(getBilling('2022/02/15')).to.eq('2022/03/15') expect(getBilling('2022/02/28')).to.eq('2022/03/28') //Mar expect(getBilling('2022/03/15')).to.eq('2022/04/15') expect(getBilling('2022/03/31')).to.eq('2022/05/01') })

Related questions

0 votes
    How will you add 1 month to current date using local datetime api of java8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Find the probability that the birth days of six different persons will fall in exactly two calendar months. A. 1 6 B ... 126 D. 341 125 Select the correct answer from above options...
asked Nov 15, 2021 in Education by JackTerrance
0 votes
0 votes
    I mean this Datepicker: https://uxsolutions.github.io/bootstrap-datepicker/ HTML: Fecha de inicio Fecha de ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    Period of natural degradation for cloth bags is (a) 2-3 weeks (b) 1 month (c) 5 months ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Period of natural degradation for wood is (a) 1 month (b) 5 months (c) 10-15 years ( ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    I am trying to convert a string into date format in Google Sheet. I've tried different formulas but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    What do you do if any concrete cube fails in 28 days compressive strength test?...
asked Jan 20, 2021 in Technology by JackTerrance
0 votes
    How will you add 10 years to current date using local datetime api of java8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you add 1 year to current date using local datetime api of java8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you add 1 week to current date using local datetime api of java8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Which of the following input control represents a date consisting of a year and a month encoded according to ISO 8601 in Web ... - datetime B - datetime-local C - date D - month...
asked Dec 1, 2020 in Technology by JackTerrance
0 votes
    The different variant of Date() constructor to create date object is/are ___________ i. new Date(date) ii ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...