【发布时间】:2018-08-09 16:48:56
【问题描述】:
我想测试我编写的过滤器函数,它返回使用 Intl.DateTimeFormat('en-GB', options) 格式化的日期:
// module "date.js"
export default function (dateISOString) {
const options = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'UTC'
};
let d = new Date(dateISOString);
return new Intl.DateTimeFormat('en-GB', options).format(d);
};
这是我的测试,使用 Jest:
import date from '@/filters/date';
describe('date', () => {
it('should format the date into dd/mm/yyyy', () => {
expect(date('2014-02-11')).toEqual('11/02/2014');
});
});
但它失败了:
Expected value to equal:
"11/02/2014"
Received:
"02/11/2014"
是否可以使用 Jest 测试(或模拟) Intl API? 问题似乎是由于浏览器和 Node 环境中 Impl API 的行为不同所致。
【问题讨论】:
标签: javascript jestjs datetime-format