title: Testing Angular 2 service with mocha
body: <p>I am trying to implement unit tests for an Angular 2 app. But I can't get it it to work. 
  As test runner <strong>mocha</strong> is used and executed like this:  </p>
  
  <pre><code>mocha -r ts-node/register -t 10000 ./**/*.unit.ts
  </code></pre>
  
  <p>Consider the following test file where I define two test cases which basically should do the same thing, but neither one is working. </p>
  
  <p><strong>shared.service.unit.ts</strong>  </p>
  
  <pre><code>import { TestBed, async, inject } from '@angular/core/testing';
  import { SharedService } from './shared.service';
  import * as Chai from 'chai';
  import 'mocha';
  const expect = Chai.expect;
  
  describe('SharedService', () =&gt; {
  
      beforeEach(() =&gt; {
          TestBed.configureTestingModule({
              declarations: [SharedService],
              providers: [SharedService]
          });
      });
  
      it('should be an object',
          inject([SharedService], (service: SharedService) =&gt; {
              expect(service).to.be.an('object');
          })
      );
  });
  
  describe('SharedService without the TestBed', () =&gt; {
      let service: SharedService;
  
      beforeEach(() =&gt; { 
          service = new SharedService();
      });
  
      it('should be an object', () =&gt; {
          expect(service).to.be.an('object');
      });
  });
  </code></pre>
  
  <p>The first one <code>'SharedService'</code> uses the <a href="https://angular.io/docs/ts/latest/guide/testing.html#!#atu-apis" rel="nofollow noreferrer">Angular Testing Utility</a>. Running it gives:  </p>
  
  <blockquote>
    <p>ReferenceError: Zone is not defined</p>
  </blockquote>
  
  <p>The second one <code>'SharedService without TestBed'</code>does not use any Angular code (similar to <a href="https://angular.io/docs/ts/latest/guide/testing.html#!#isolated-unit-tests" rel="nofollow noreferrer">this example from Angular 2 Testing guide</a>). Running it gives:  </p>
  
  <blockquote>
    <p>TypeError: Reflect.getMetadata is not a function</p>
  </blockquote>
  
  <p>After adding these lines to the test file:  </p>
  
  <pre><code>import 'core-js/es6';
  import 'core-js/es7/reflect';
  import 'zone.js/dist/zone';
  </code></pre>
  
  <p>Both test cases give the same error (from <code>zone.js\dist\zone.js</code>):  </p>
  
  <blockquote>
    <p>TypeError: Cannot read property 'prototype' of undefined</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>