Different dateTime depending on current environment using moment.js


Hi guys, I’m just wondering how to deal with moment.js when developing/testing.

The situation: My app shows some products based on the current local time delivered by moment.js. Products that were already published and upcoming products that will be released in the future. Therefore the product data is delivered by a server and Ember consumes and displays the products. Product data:

    id: 1,
    title: "Product 1",
    releaseDateTime: "2017-02-10 10:00:00"
    id: 2,
    title: "Product 2",
    releaseDateTime: "2017-02-12 11:00:00"

By assuming today is 2017-02-11 my app shows products for yesterday and tomorrow based on their releaseDateTime:

PAST          | UPCOMING
------------- | -------------
Product 1     | Product 2

In all my app logic I’m using moment() to get the current dateTime delivered by moment.js. Now I’m writing acceptance tests where I check the count of products that get rendered in their columns. Today the tests would be successful, but tomorrow they’ll fail because then both products were in the PAST column!

PAST          | UPCOMING
------------- | -------------
Product 1     |
Product 2     |

So I think I need to set a fixed dateTime when developing and testing and use the standard moment() function when the app runs in production environment.

What I try to achieve is a kind of initializer that checks the current environment and when it’s not production then moment() = moment('2017-02-11 10:00:00') which would be a static dateTime to use in development and testing otherwise use the standard behaviour to get the actual dateTime by calling only moment()

I can achieve this already by a service, but I don’t want to inject it everywhere I use it and also I have to define a variable for it.

I cannot believe that I’m the only one facing this problem. Hopefully you can follow my explanation! :smiley:


I’ve done this in Node using a library called MockDate: https://github.com/boblauer/MockDate

I haven’t tried this in Ember though. I’m not sure if overriding the Date object would mess up the run loop. If you try it, report back and let us know! :slight_smile:



Be sure you are using the latest version of ember-cli-moment-shim if you go this route.


Thank you guys,

I’ll try it this way and will report :sunglasses: