title: Angular 2 Karma testing (PhantomJS) - SyntaxError: Use of reserved word 'import'
body: <p>I'm trying to setup Angular 2 Karma testing using PhantomJS, due to jenkins purpose. <strong>Dummy tests work fine</strong>, the problem came when I tried to import a service from angular itself.</p>
  
  <h1>The problem</h1>
  
  <p>Running <code>npm test</code></p>
  
  <pre><code>PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
    SyntaxError: Use of reserved word 'import'
    at test/components/talks.spec.ts:1
  PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.114 secs / 0 secs)
  npm ERR! Test failed.  See above for more details.
  </code></pre>
  
  <h1>The cause</h1>
  
  <p>File <code>talks.spec.ts</code></p>
  
  <pre><code>import {TalkService} from "../../app/services/talk.service"; &lt;- Error here
  
      describe('Integration testing for talks', function() {
          let service : TalkService = new TalkService();
  
      it('should be able to run a method', function() {
          service.something();
      });
  });
  </code></pre>
  
  <p>For some reason, I'm unable to import when using PhantomJS. I've tried a large number of solutions, and after flooding my project with dependencies trying to find a solution, I understood I wasn't being able to solve this.</p>
  
  <h1>Files</h1>
  
  <p>karma.conf.js</p>
  
  <pre><code>module.exports = function(config) {
      config.set({
          basePath: '.',
          frameworks: ['jasmine'],
          plugins : [
              'karma-jasmine',
              'karma-html-reporter',
              'karma-phantomjs-launcher',
              'karma-junit-reporter'
          ],
          files: [
              './test/components/dashboard.spec.js',
              './test/components/talks.spec.ts'
          ],
          exclude: [],
          preprocessors: {},
          reporters: ['progress', 'junit', 'html'],
          htmlReporter: {
              outputDir: 'test', // where to put the reports
              templatePath: null, // set if you moved jasmine_template.html
              focusOnFailures: true, // reports show failures on start
              namedFiles: false, // name files instead of creating sub-directories
              pageTitle: null, // page title for reports; browser info by default
              urlFriendlyName: false, // simply replaces spaces with _ for files/dirs
              reportName: 'html', // report summary filename; browser info by default
  
              // experimental
              preserveDescribeNesting: false, // folded suites stay folded
              foldAll: false // reports start folded (only with preserveDescribeNesting)
          },
          junitReporter: {
              outputDir: 'test/xml-reports/'
          },
          port: 9876,
          colors: true,
          logLevel: config.LOG_INFO,
          autoWatch: false,
          browsers: ['PhantomJS'],
          singleRun: true,
          concurrency: Infinity
      })
  };
  </code></pre>
  
  <p>karma-test-shim.js</p>
  
  <pre><code>// Tun on full stack traces in errors to help debugging
  Error.stackTraceLimit = Infinity;
  
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
  
  // // Cancel Karma's synchronous start,
  // // we will call `__karma__.start()` later, once all the specs are loaded.
  __karma__.loaded = function() {};
  
  System.config({
      packages: {
          'base/dist': {
              defaultExtension: false,
              format: 'cjs',
              map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
          }
      }
  });
  
  System.import('@angular/core/testing')
      .then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
      .then(function() { return Promise.all(resolveTestFiles()); })
      .then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });
  
  function createPathRecords(pathsMapping, appPath) {
      // creates local module name mapping to global path with karma's fingerprint in path, e.g.:
      // './vg-player/vg-player':
      // '/base/dist/vg-player/vg-player.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
      var pathParts = appPath.split('/');
      var moduleName = './' + pathParts.slice(Math.max(pathParts.length - 2, 1)).join('/');
      moduleName = moduleName.replace(/\.js$/, '');
      pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
      return pathsMapping;
  }
  
  function onlyAppFiles(filePath) {
      return /\/base\/dist\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
  }
  
  function onlySpecFiles(path) {
      return /\.spec\.js$/.test(path);
  }
  
  function resolveTestFiles() {
      return Object.keys(window.__karma__.files)  // All files served by Karma.
          .filter(onlySpecFiles)
          .map(function(moduleName) {
              // loads all spec files via their global module names (e.g.
              // 'base/dist/vg-player/vg-player.spec')
              return System.import(moduleName);
          });
  }
  </code></pre>