title: How do I test angular2 services that use moment?
body: <p>I am trying to test an Angular (angular2) service I wrote that uses momentJS, but since moment lives in the browser I keep getting reference errors. Is there anyway to tell karma/jasmine how to use moment in a unit test? Here is the error I get:</p>
  
  <pre><code>ReferenceError: Can't find variable: moment in karma-test-shim.js (line 33814)
  </code></pre>
  
  <p>Here are my tests:</p>
  
  <pre><code>describe('isDateBeforeToday', () =&gt; {
          it('should return true if date is before today', () =&gt; {
              let past = new Date(2016, 1, 1);
              expect(UnitService.isDateBeforeToday(past)).toEqual(true);
  
              past = new Date();
              past.setDate(past.getDate() - 1);
              past.setHours(23);
              expect(UnitService.isDateBeforeToday(past)).toEqual(true);
          });
  
          it('should return false if date is today', () =&gt; {
              let today = new Date();
              expect(UnitService.isDateBeforeToday(today)).toEqual(false);
          });
  
          it('should return false if date is in future', () =&gt; {
              let future = new Date();
              future.setDate(future.getDate() + 1);
              expect(UnitService.isDateBeforeToday(future)).toEqual(false);
          });
      });
  </code></pre>
  
  <p>Here is the function I'm trying to test:</p>
  
  <pre><code>static isDateBeforeToday(date: Date) {
          let momentDate = moment(date);
          let now = moment();
          return (momentDate.diff(now, 'days') &lt; 0);
      }
  </code></pre>
  
  <p>karma-test-shim.js (based on angular examples):</p>
  
  <pre><code>Error.stackTraceLimit = Infinity;
  
  require('core-js/es6');
  require('core-js/es7/reflect');
  
  require('zone.js/dist/zone');
  require('zone.js/dist/long-stack-trace-zone');
  require('zone.js/dist/proxy');
  require('zone.js/dist/sync-test');
  require('zone.js/dist/jasmine-patch');
  require('zone.js/dist/async-test');
  require('zone.js/dist/fake-async-test');
  
  require('moment');
  
  var appContext = require.context('../__tests__', true, /\.spec\.ts/);
  
  appContext.keys().forEach(appContext);
  
  var testing = require('@angular/core/testing');
  var browser = require('@angular/platform-browser-dynamic/testing');
  
  testing.TestBed.initTestEnvironment(browser.BrowserDynamicTestingModule, browser.platformBrowserDynamicTesting());
  </code></pre>
  
  <p>karma.conf.js:</p>
  
  <pre><code>var webpackConfig = require('./webpack.test');
  
  module.exports = function (config) {
    var _config = {
      basePath: '',
      browserNoActivityTimeout: 20000, // had to increase this for component unit tests to work
  
      frameworks: ['jasmine'],
  
      files: [
        {pattern: './karma-test-shim.js', watched: false}
      ],
  
      preprocessors: {
        './karma-test-shim.js': ['webpack', 'sourcemap']
      },
  
      webpack: webpackConfig,
  
      webpackMiddleware: {
        stats: 'errors-only'
      },
  
      webpackServer: {
        noInfo: true
      },
  
      reporters: ['progress','kjhtml'], //leave coverage reporter in for now, even though coverage not working
  
      coverageReporter: {
          type: 'html',
          dir: '../coverage/'
      },
  
      browsers: ['phantomJS_without_security'],
      customLaunchers: {
          chrome_without_security: {
              base: 'Chrome',
              flags: ['--disable-web-security']
          },
          phantomJS_without_security: {
              base:'PhantomJS',
              options: {
                  windowName: 'Portal-tests',
                  settings: {
                      webSecurityEnabled: false
                  }
              }
          }
      },
  
      phantomjsLauncher: {
          // Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
          exitOnResourceError: true
      },
  
      client: {
          captureConsole: true
      },
  
      port: 9876,
      colors: true,
      logLevel: config.LOG_INFO,
      autoWatch: false,
      singleRun: true
    };
  
    config.set(_config);
  };
  </code></pre>