Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jest测试的模拟时刻JS时区_Javascript_Unit Testing_Jestjs_Timezone_Momentjs - Fatal编程技术网

Javascript jest测试的模拟时刻JS时区

Javascript jest测试的模拟时刻JS时区,javascript,unit-testing,jestjs,timezone,momentjs,Javascript,Unit Testing,Jestjs,Timezone,Momentjs,这是我的typescript模块,它获取格式化的时间字符串 const getFormattedTime = (time: number, showTimezone = false, isUTC = false) => { if(!time || time < 0) { return ''; } const date: moment.Moment = moment(time).local(!isUTC); console.log('DATE IS ::',

这是我的typescript模块,它获取格式化的时间字符串

const getFormattedTime = (time: number, showTimezone = false, isUTC = false) => {
  if(!time || time < 0) {
    return '';
  }

  const date: moment.Moment = moment(time).local(!isUTC);
  console.log('DATE IS ::', date);
  if(showTimezone) {
    return date.format(DATE_FORMAT_WITH_TIMEZONE)
  }
  return date.format(DATE_FORMAT);
};

export default getFormattedTime;
我试过几种嘲弄的方式,但都不管用。 这里的想法是让所有测试无论在哪个环境中运行,都在UTC时区中运行。 你知道哪里出了问题吗

import getFormattedTime from './getFormattedTime';

describe('getFormattedTime()', () => {
  // jest.mock('moment', () => {
  //   // let isUTC;
  //   const moment = jest.fn(() => {
  //     return {
  //       local: jest.fn(() => false),
  //       isUTC: jest.fn(() => true),
  //     }
  //   })
  //   // Helper for tests to set expected value
  //   // moment.local = (value: boolean) => {
  //   //   isUTC = value;
  //   //   return isUTC;
  //   // };
  //   return moment;
  // })

  const origDate = Date.now;

  beforeEach(() => {
    Date.now = jest.fn(() => Date.UTC(2020, 2, 2, 0, 0, 0));
  });

  afterEach(() => {
    Date.now = origDateNow;
  });

  it.each`
        time                                    | showTimezone | expected
        ${Date.UTC(2020, 2, 2,0, 0, 0)} | ${true}      | ${'03/02/2020, 12:00:00 AM +00:00'}
        ${Date.UTC(2020, 2, 2,0, 0, 0)} | ${false}     | ${'03/02/2020, 12:00:00 AM'}
    `('formats time into human readable format if timezone passed in = $showTimezone', ({time, showTimezone, expected}) => {
      expect(getFormattedTime(time, showTimezone, false)).toBe(expected);
  });
});