如何使用 Jest 模拟和测试时刻日期格式化程序

How to mock and test moment date formatter with Jest

我正在使用 dateFormatter.js 文件中的以下函数将日期格式从 DD-MM-YYYY 更改为 ISO(时间-日期)格式:

import moment from 'moment';

export const getFormattedDate = (date) => moment(date, 'DD-MM-YYYY');

测试:

import {getFormattedDate} from '../dateFormatter';

describe('Date formatter test', () => {
    describe('WHEN providing sample date with "DD-MM-YYYY" format', () => {
        test('THEN the function should return sample date in ISO (date-time) format ', () => {
            expect(getFormattedDate('27-02-2021')).toEqual('2021-02-26T23:00:00.000Z');
        });
    });
});

函数返回 formattedDate = "2021-02-26T23:00:00.000Z"

但是尝试开玩笑地测试它会返回整个 moment 对象并使我的测试失败。实际结果如下所示:

{
  "_d": {},
  "_f": "DD-MM-YYYY",
  "_i": "27-02-2021",
  "_isAMomentObject": true,
  "_isUTC": false,
  "_isValid": true,
  "_locale": {
    "_abbr": "en",
    "_calendar": {
      "lastDay": "[Yesterday at] LT",
      "lastWeek": "[Last] dddd [at] LT",
      "nextDay": "[Tomorrow at] LT",
      "nextWeek": "dddd [at] LT",
      "sameDay": "[Today at] LT",
      "sameElse": "L"
    },
    "_config": {
      "abbr": "en",
      "calendar": "[Circular reference found] Truncated by IDE",
      "dayOfMonthOrdinalParse": {},
      "eras": [
        {
          "abbr": "AD",
          "name": "Anno Domini",
          "narrow": "AD",
          "offset": 1,
          "since": "0001-01-01",
          "until": null
        },
        {
          "abbr": "BC",
          "name": "Before Christ",
          "narrow": "BC",
          "offset": 1,
          "since": "0000-12-31",
          "until": null
        }
      ],
      "invalidDate": "Invalid date",
      "longDateFormat": {
        "L": "MM/DD/YYYY",
        "LL": "MMMM D, YYYY",
        "LLL": "MMMM D, YYYY h:mm A",
        "LLLL": "dddd, MMMM D, YYYY h:mm A",
        "LT": "h:mm A",
        "LTS": "h:mm:ss A"
      },
      "meridiemParse": {},
      "months": [
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
      ],
      "monthsShort": [
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
      ],
      "ordinal": {},
      "relativeTime": {
        "M": "a month",
        "MM": "%d months",
        "d": "a day",
        "dd": "%d days",
        "future": "in %s",
        "h": "an hour",
        "hh": "%d hours",
        "m": "a minute",
        "mm": "%d minutes",
        "past": "%s ago",
        "s": "a few seconds",
        "ss": "%d seconds",
        "w": "a week",
        "ww": "%d weeks",
        "y": "a year",
        "yy": "%d years"
      },
      "week": {
        "dow": 0,
        "doy": 6
      },
      "weekdays": [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday"
      ],
      "weekdaysMin": [
        "Su",
        "Mo",
        "Tu",
        "We",
        "Th",
        "Fr",
        "Sa"
      ],
      "weekdaysShort": [
        "Sun",
        "Mon",
        "Tue",
        "Wed",
        "Thu",
        "Fri",
        "Sat"
      ]
    },
    "_dayOfMonthOrdinalParse": {},
    "_dayOfMonthOrdinalParseLenient": {},
    "_eras": "[Circular reference found] Truncated by IDE",
    "_invalidDate": "Invalid date",
    "_longDateFormat": "[Circular reference found] Truncated by IDE",
    "_meridiemParse": {},
    "_months": "[Circular reference found] Truncated by IDE",
    "_monthsShort": "[Circular reference found] Truncated by IDE",
    "_relativeTime": "[Circular reference found] Truncated by IDE",
    "_week": "[Circular reference found] Truncated by IDE",
    "_weekdays": "[Circular reference found] Truncated by IDE",
    "_weekdaysMin": "[Circular reference found] Truncated by IDE",
    "_weekdaysShort": "[Circular reference found] Truncated by IDE",
    "ordinal": "[Circular reference found] Truncated by IDE"
  },
  "_pf": {
    "charsLeftOver": 0,
    "empty": false,
    "era": null,
    "invalidEra": null,
    "invalidFormat": false,
    "invalidMonth": null,
    "iso": false,
    "nullInput": false,
    "overflow": -1,
    "parsedDateParts": [
      2021,
      1,
      27
    ],
    "rfc2822": false,
    "unusedInput": [],
    "unusedTokens": [],
    "userInvalidated": false,
    "weekdayMismatch": false
  }
}

知道我如何模拟它以获得预期的开玩笑结果吗?

当您调用 moment() 时,它 returns 一个新的时刻实例。如果你想以某种方式格式化日期,你应该调用 moment("27-02-2021", "DD-MM-YYYY").toISOString().

你也可以在里面提供 format() 它应该被格式化,默认情况下(没有参数)它会是这样的:2021-01-26T00:00:00+01:00