Jest:测试Intl.DateTimeFormat

Mat*_*zza 6 javascript datetime-format jestjs

我想测试一个我写的过滤函数,它返回一个使用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环境中的不同行为.

Ste*_*han 11

我设法找到这个问题的唯一解决方案是安装full-icu,它似乎在测试过程中为节点提供了正确的语言环境.

该包是必需的,因为默认情况下节点只附带一组有限的语言环境,如下所述:https://nodejs.org/docs/latest-v9.x/api/intl.html

安装该软件包后,我必须采取的额外步骤是更改我要使用的测试命令:

"test": "NODE_ICU_DATA=node_modules/full-icu jest --config jest.config.js"

我在几个不同的环境中遇到了这个问题.在Mac OS上本地运行测试时,以及在CI期间在Docker容器内运行测试时.

有趣的是,在通过WebStorm的Jest集成运行测试时,我不需要使用导出.看起来Intl库的行为在节点中远非稳定.


fre*_*emn 10

如果您想更改每个测试的区域,刚刚找到了解决方案。

这里的好处是对 Intl 对象的更改最小,因为我们只是使用普通 api 来设置默认值

const timezoneMock = function(zone: string) {
    const DateTimeFormat = Intl.DateTimeFormat
    jest
    .spyOn(global.Intl, 'DateTimeFormat')
    .mockImplementation((locale, options) => new DateTimeFormat(locale, {...options, timeZone: zone}))
  }

  afterEach(() => {
    jest.restoreAllMocks();
  })
Run Code Online (Sandbox Code Playgroud)

进而

  describe('when Europe/London', () => {
    it('returns local time', () => {
      timezoneMock('Europe/London')
   //etc.... 
Run Code Online (Sandbox Code Playgroud)

  • 天才!!!就我而言,这是在测试中模拟时区的最佳方法 (2认同)