使用 jest 模拟 moment() 和 moment().format

Zin*_*vaa 25 javascript unit-testing momentjs reactjs jestjs

我无法模拟moment()moment().format运行。我有状态在哪里,currentDateMoment并且currentDateFormatted设置如下。

currentDateMoment: moment() //2019-04-23T17:45:26.339Z
currentDateFormatted: moment().format('MM-DD-YYYY').valueOf() //"04-23-2019"
Run Code Online (Sandbox Code Playgroud)

试图在我的快照测试中模拟moment()和模拟moment().format以返回特定日期,但无法。下面试过。

jest.mock('moment', () => () => '2018–01–30T12:34:56+00:00');

jest.mock('moment', () => ({
  constructor: () => '2018–01–30T12:34:56+00:00'
})); 

jest.mock('moment', () => () => ({ format: () => '01–30-2018' }));
Run Code Online (Sandbox Code Playgroud)

gia*_*ant 27

以模拟时刻最简单的方法()和它使用的任何功能(即.day().format())是改变Date的是moment()引擎盖下应用

在您的测试文件中添加以下代码段

Date.now = jest.fn(() => new Date("2020-05-13T12:33:37.000Z"));

这使得moment()在您的测试中随时调用,moment()认为今天是 2020 年 5 月 13 日,星期三

  • 整洁,在 Typescript 中记得添加: `Date.now = jest.fn(() => new Date('2020-11-01T12:33:37.000Z').getTime());` (6认同)
  • 是的,我也喜欢这个,设置很简单。我个人喜欢这样使用它: ```Date.now = jest.fn().mockReturnValue(new Date('2020-05-13T12:33:37.000Z'));``` (5认同)
  • 我成功使用了 `jest.spyOn(Date, 'now').mockImplementation(...)` (2认同)

fel*_*omi 22

您可以模拟 Moment 以返回特定日期,然后format不必进行模拟。

jest.mock('moment', () => {
  return () => jest.requireActual('moment')('2020-01-01T00:00:00.000Z');
});
Run Code Online (Sandbox Code Playgroud)

通过这样做,任何调用都Moment()将始终返回一个日期设置为2020-01-01 00:00:00的时刻对象

这是一个带有返回明天日期的函数的示例以及此函数的测试。

const moment = require('moment');
const tomorrow = () => {
  const now = moment();
  return now.add(1, 'days');
};

describe('tomorrow', () => {
  it('should return the next day in a specific format', () => {
    const date = tomorrow().format('YYYY-MM-DD');
    expect(date).toEqual('2020-01-02');
  });
});
Run Code Online (Sandbox Code Playgroud)


sli*_*wp2 5

这是解决方案:

index.ts

import moment from 'moment';

export function main() {
  return {
    currentDateMoment: moment().format(),
    currentDateFormatted: moment()
      .format('MM-DD-YYYY')
      .valueOf()
  };
}
Run Code Online (Sandbox Code Playgroud)

index.spec.ts

import { main } from './';
import moment from 'moment';

jest.mock('moment', () => {
  const mMoment = {
    format: jest.fn().mockReturnThis(),
    valueOf: jest.fn()
  };
  return jest.fn(() => mMoment);
});

describe('main', () => {
  test('should mock moment() and moment().format() correctly ', () => {
    (moment().format as jest.MockedFunction<any>)
      .mockReturnValueOnce('2018–01–30T12:34:56+00:00')
      .mockReturnValueOnce('01–30-2018');
    expect(jest.isMockFunction(moment)).toBeTruthy();
    expect(jest.isMockFunction(moment().format)).toBeTruthy();
    const actualValue = main();
    expect(actualValue).toEqual({ currentDateMoment: '2018–01–30T12:34:56+00:00', currentDateFormatted: '01–30-2018' });
  });
});
Run Code Online (Sandbox Code Playgroud)

100% 覆盖率的单元测试结果:

 PASS  src/stackoverflow/55838798/index.spec.ts
  main
    ? should mock moment() and moment().format() correctly  (7ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.795s, estimated 8s
Run Code Online (Sandbox Code Playgroud)

源代码:https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/55838798


Пав*_*рин 5

模拟日期对我有用

import mockDate from "mockdate";


test('Should add some', () => {
    mockDate.set(new Date('2/20/2020'));

    const action = addSome();

    expect(action).toEqual({
        createdAt: moment()
    });

    mockDate.reset();
})
Run Code Online (Sandbox Code Playgroud)