相关疑难解决方法(0)

Jest:测试Intl.DateTimeFormat

我想测试一个我写的过滤函数,它返回一个使用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);
};
Run Code Online (Sandbox Code Playgroud)

这是我的测试,使用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');
    });
});
Run Code Online (Sandbox Code Playgroud)

但它失败了:

Expected value to equal:
  "11/02/2014"
Received:
  "02/11/2014"
Run Code Online (Sandbox Code Playgroud)

是否可以使用Jest测试(或模拟)Intl API?看起来问题是由于Impl API在浏览器和Node环境中的不同行为.

javascript datetime-format jestjs

6
推荐指数
2
解决办法
2251
查看次数

标签 统计

datetime-format ×1

javascript ×1

jestjs ×1