karansys
karansys

Reputation: 2719

How to unit test a date returned in particular format

I want to write a unit test such that it returns date in a particular format. I pass "2020-02-25" and the function returns "25-Feb-2020".

file.js

module.exports = { 
   convertDate
};
function convertDate (date) {
  console.log(date);
    let  date1 = new Date(date);
    console.log(date1);
    let formattedDate = date1.toLocaleDateString('en-GB', {
    day: 'numeric', month: 'short', year: 'numeric'
    }).replace(/ /g, '-');
    console.log( formattedDate);
    return formattedDate;
}    

file.spec.js

const expect = require('chai').expect;
const filejs = require('./file.js');

     it.only('should return the date in 25-Feb-2020 format when I pass date in 2020-02-25' ,function () {
       let fdate = filejs.convertDate("2020-02-25");
       expect(fdate).to.equal('25-Feb-2020');

     })

When I run the test, test fails

AssertionError: expected 'Feb-25,-2020' to equal '25-Feb-2020'

Upvotes: 0

Views: 796

Answers (1)

user1039663
user1039663

Reputation: 1280

Seems that the unit test is ok, but the problem is that your function returns 'Feb-25,-2020'.

Upvotes: 1

Related Questions