我不知道为什么这个功能测试没有通过,
export function timeStampToFormattedDate (timestamp) {
if (!timestamp) return 'N/A'
const date = new Date(timestamp)
return date.toLocaleString('en-us', { dateStyle: 'long' })
}
测试
describe('timeStampToFormattedDate function', () => {
it('returns formatted date string from last active user time-stamp', () => {
const timeStamp = 1592810798024
const formattedString = timeStampToFormattedDate(timeStamp)
expect(formattedString).toEqual(`June 22, 2020`)
})
})
我在运行测试时遇到此错误,
timeStampToFormattedDate function › returns formatted date string from last active user time-stamp
expect(received).toEqual(expected) // deep equality
Expected: "June 22, 2020"
Received: "6/22/2020, 10:26:38 AM"
8 | const timeStamp = 1592810798024
9 | const formattedString = timeStampToFormattedDate(timeStamp)
> 10 | expect(formattedString).toEqual(`June 22, 2020`)
| ^
11 | })
12 |
13 | it('returns "N/A" if last active timestamp is null', () => {
at Object.toEqual (src/utilities/dateTimeUtil.test.js:10:29)
当我通过在浏览器控制台中测试使用相同的参数调用来尝试该函数时,它按预期工作。
timeStampToFormattedDate(1592810798024)
"June 22, 2020"
那么为什么它会失败呢?
最佳答案
正如@James 在他们的评论中提到的,这仅在 Node V13.0.0 和更高版本中受支持,如下所述:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString#Browser_compatibility
测试它的一种简单方法是从命令行运行它。输入 node
并复制粘贴您的方法,然后使用时间戳调用该方法。
这是一个例子:
关于javascript - date.toLocaleString ('en-us' , { 日期样式 : 'long' }) not working in jest test but working in browser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64162833/