MomentJs in protractor test

854 views
Skip to first unread message

Johan Dindaine

unread,
Jun 13, 2014, 4:18:24 AM6/13/14
to ang...@googlegroups.com
I'm trying to run an end-to-end test that is using momentJs for date manipulation.
And I have added it as a dependency in my package.json file and installed it so it available.
Then at the very top of my end-to-end test file I include it as a module with the require directive (require('moment')).
But when I run this test I get an exception: ReferenceError: moment is not defined

It should be loaded at that point, shouldn't it?

Bailey Malone

unread,
May 19, 2016, 4:28:43 PM5/19/16
to AngularJS
Hey @Johan,

I know this is totally late, but just in case you or someone else runs into this again I wanted to post an answer. 

I did something very similar to this recently for using Protractor to test a partially Angular application. What I did that worked was add a variable name to the 'require()' statement at the very top of my test file. So my test file looks like this:

var moment = require('moment');

describe('Screen A - Mobile', function() {

        ......
        it('Example Jasmine IT block test', function() { 

                var m = moment();
                console.log('moment date = ' + moment().format('dddd'));    // print day of week
        });

});


I think the only part you were missing was assigning a handle ("moment" in my example) to the require of the moment library.

Hope that helps!
Reply all
Reply to author
Forward
0 new messages