我想编写一个单元测试,以便它以特定的格式返回日期。我通过"2020-02-25“,函数返回"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');
})当我运行测试时,测试失败。
AssertionError:预期‘2月25日,-2020年’等于‘25-2月-2020年’
发布于 2020-02-25 19:15:16
似乎单元测试是可以的,但问题是您的函数返回‘2月25日,-2020’。
https://stackoverflow.com/questions/60400947
复制相似问题