简体   繁体   English

用 moment.js 模拟茉莉花日期

[英]Jasmine date mocking with moment.js

I'm using moment.js for date/time in my application, but it seems like it doesn't play well with Jasmine's mocking capabilities.我在我的应用程序中使用 moment.js 作为日期/时间,但它似乎不能很好地与 Jasmine 的模拟功能配合使用。 I've put together a test suite below that shows my issue:我在下面整理了一个测试套件,显示了我的问题:

jasmine.clock().mockDate doesn't seem to work for moment, while it works fine for Date . jasmine.clock().mockDate似乎jasmine.clock().mockDate不起作用,而它适用于Date

describe('Jasmine tests', function () {
    beforeEach(function() {
        jasmine.clock().install();
    });

    afterEach(function() {
        jasmine.clock().uninstall();
    });

    // Pass
    it('uses the mocked time with Date', function() {
        var today = new Date('2015-10-19');
        jasmine.clock().mockDate(today);
        expect(new Date().valueOf()).toEqual(today.valueOf());
    });


    // Fail
    it('uses the mocked time with moment', function() {
        var today = moment('2015-10-19');
        jasmine.clock().mockDate(today);

        expect(moment().valueOf()).toEqual(today.valueOf());
    });
});

Why does Date work as expected while moment does not?为什么Date可以按预期工作,而moment却没有? Isn't moment using Date under the hood?是不是moment使用Date引擎盖下?

What is the right way to mock moment using Jasmine?使用 Jasmine 模拟moment的正确方法是什么?

jasmine.clock().mockDate expects Date as input. jasmine.clock().mockDate需要Date作为输入。 Date and moment are not fully compatible. Datemoment不完全兼容。 If you provide the to-be-mocked date in the spec itself you could simply use Date there instead.如果您在规范本身中提供了待模拟的日期,则可以简单地在那里使用Date

If your code generates a moment you want to mock, or you'd rather use the moment API, take a look atmoment.toDate() .如果您的代码生成了您想要模拟的时刻,或者您更愿意使用时刻 API,请查看moment.toDate() This method returns the Date object backing a moment.此方法返回支持片刻的Date对象。

it('uses the mocked time with moment', function() {
    var today = moment('2015-10-19').toDate();
    jasmine.clock().mockDate(today);
    expect(moment().valueOf()).toEqual(today.valueOf());
});

在他们自己的测试套件中查看 moment mock 的日期: https : //github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/now.js#L15

I was trying to find an alternative to jasmine or even other mock frameworks to avoid dependencies .我试图找到jasmine甚至其他模拟框架的替代方案来避免依赖

 const currentToday = moment().toDate(); console.log(`currentToday:`, currentToday) const newToday = moment('1980-01-01').toDate(); console.log(`newToday :`, newToday); Date.now = () => { return newToday }; const fakedToday = moment().toDate(); console.log(`fakedToday :`, fakedToday)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

currentToday: 2019-09-17T15:26:12.763Z
newToday    : 1980-01-01T00:00:00.000Z
fakedToday  : 1980-01-01T00:00:00.001Z
  1. Import moment to spec file将时刻导入规范文件

    import * as moment from 'moment';从“时刻”导入 * 作为时刻;

  2. expect your test case with moment期待您的测试用例时刻

    const stringDate = moment.utc(date).format(stringFormat); const stringDate = moment.utc(date).format(stringFormat); expect(stringDate).toEqual('2021-05-23T08:00:00');期望(stringDate).toEqual('2021-05-23T08:00:00');

  3. The final spec file look like this最终的规范文件看起来像这样

 import { TestBed } from '@angular/core/testing'; import { convertDateToString, getDateDifferenceInHours, getDateIsoStringWithoutTimezone, getDateIsoStringWithTimezone, getDateWithoutTimezone } from './DateHelper'; import * as moment from 'moment'; import * as DateHelper from '@shared/helper/DateHelper'; describe('DateHelper', () => { beforeEach(async () => { await TestBed.configureTestingModule({ }); }); it('should convert date to string YYYY-MM-DD', () => { expect(convertDateToString(new Date('05-23-2021 12:00'), 'YYYY-MM-DD')).toEqual('2021-05-23'); }); it('should convert date to iso string without timezone', () => { expect(getDateIsoStringWithoutTimezone(new Date('05-23-2021 12:00'))).toEqual('2021-05-23T12:00:00.000Z'); }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM