Search code examples
angularjasminekarma-runnerkarma-jasminesystemjs

How to combine SystemJs relative path and test using karma/jasmine


I am currently using systemJs relative path in my component with this syntax in my .ts files:

declare var __moduleName:string;

@Component({
  moduleId: __moduleName,
  ...
})
...

All works fine untill I want to import this component in a test. I got this following non-verbose error from karma :

Chrome 52.0.2743 (Linux 0.0.0) ERROR
{
  "originalErr": {}
}

If I erase the line :

moduleId: __moduleName,

from the component transcrypted .js file, all my test works well.

If someone has an idea how to combine this systemjs relative-paths syntax and karma / jasmine tools, it would be welcome.


Solution

  • At the moment, Angular2's best practice is to use commonjs for your moduleId entry and compile your app using commonjs.

    tsconfig.json:

    {
      "compilerOptions": {
        "module": "commonjs",
        ...
        ...
      }
    }
    

    Using commonjs compilation will work fine with karma/jasmine and with angular2 aswell.

    You'll simply have to replace

    @Component({
      moduleId: __moduleName,
      ...
    })
    ...
    

    with

    @Component({
      moduleId: module.id,
      ...
    })
    ...
    

    and remove

    declare var __moduleName:string;