1
votes

I have a Typescript+Node+Angular2+Electron app and currently trying to run tests for node classes, written also in Typescript.

For building the application and running it within electron I use following tsconfig:

"compilerOptions": {
    "module": "system",
    "target": "es6",
    ...
  }

So as you can see, it's using systemjs and compiling TS into JS-es6. It works fine, application itself is working.

Now I need Jasmine to come on board. I installed this npm package, updated my gulp tasks to run gulp-jasmine for just 1 file:

gulp.task('jasmine', function() {
    gulp.src('./test/test.js')
        .pipe(jasmine())
});

This is how my test.js looks like:

System.register(["./models-src/app/models/pathWatch/pathWatch"], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var pathWatch_1;
    return {
        setters:[
            function (pathWatch_1_1) {
                pathWatch_1 = pathWatch_1_1;
            }],
        execute: function() {
            describe("Run Application:", () => {
                it("starts", () => {
                    var pw1 = new pathWatch_1.PathWatch();
                    expect(true).toEqual(true);
                });
            });
        }
    }
});

So, nothing special, 1 import-1test-1assert, wrapped with SystemJs stuff. When I try to run this test, I have an error: "System is not defined".

My questions are:

1) Is it possible to run jasmine tests, using systemjs loader inside?

2) If it's possible, do I need to install/configure some additional stuff?

3) I tried to compile TS using Module="commonjs" and it's working. But I don't want to compile my source differently for tests and build. Why it's working fine with commonjs without any additional manipulations?

4) Also I tried to compile TS using Module="es6". It's not working, I have an error "Unexpected reserved word". Is it possible to run jasmine tests written in js es6 without transpiling them into es5?

Thanks a lot!

1
Please limit questions on Stack Overflow to one question per question. Asking multiple ones in a single post makes it harder to vote on the question, as well as the answers. - Madara's Ghost

1 Answers

1
votes

1) Is it possible to run jasmine tests, using systemjs loader inside?

2) If it's possible, do I need to install/configure some additional stuff?

You mean, run jasmine tests in node using systemjs as a loader? I don't think jasmine supports using systemjs instead of require for loading modules. So your tests need to be in commonjs, but test code can use SystemJS to load and test application code. Something like this in test.js could work, provided that systemjs is configured properly and can find pathWatch module:

describe("Run Application:", () => {
    it("starts", (done) => {

        var system = require('systemjs');
        system.config({
           // systemjs config here
           // 
        });
        system.import('path-to-path-watch-module').then(pathWatch => {
            var pw = new pathWatch.PathWatch();
            expect(true).toEqual(true);
            done();
        });
    });
});

system.import is asynchronous, so all jasmine tests need to be async too.

3) I tried to compile TS using Module="commonjs" and it's working. But I don't want to compile my source differently for tests and build. Why it's working fine with commonjs without any additional manipulations?

Because then there is no reference to System in the compiled code - it uses module.exports like any other node module and can be loaded as is by jasmine.

4) Also I tried to compile TS using Module="es6". It's not working, I have an error "Unexpected reserved word". Is it possible to run jasmine tests written in js es6 without transpiling them into es5?

Module="es6" requires a runtime that supports es6 import and export, so it needs a transpiler and module loader before it can run on current version of node.