James Monger
James Monger

Reputation: 10665

Testing compiled TypeScript with Karma and Jasmine from a JS test file

I have a project with the following structure:

build
- out.js
spec
- foo.spec.js
- bar.spec.js
src
- foo.ts
- bar.ts

The two .ts files are built down into out.js. In my .spec.js files, I want to be able to reference out.js easily.

When using Karma/Jasmine in pure JS projects, I simply use RequireJS, and wrap my test files in a define(['a'], function(a) { ... }); file. Then, in a.js, the contents would be wrapped like this:

define([], function() {
    Foo.prototype. .......
    Foo.prototype. .......

    return Foo; 
});

However, because out.js will be build by the TypeScript compiler, and therefore cannot be wrapped in the define(...) block, I am unable to correctly import it with RequireJS.


To clarify:

With handwritten JS, such as below, I can import it into my test files fine:

define([], function() {
    function Bla() { }
    Bla.prototype.hi = function () {
        return "hey";
    };
    return Bla;
});

With similar JS, but built with the TypeScript compiler and therefore missing the define([]) wrapper, I simply get undefined when I import it into my tests.

var Bla = (function () {
    function Bla() { }
    Bla.prototype.hi = function () {
        return "hey";
    };
    return Bla;
})();

How can I either configure the TypeScript compiler to wrap my compiled classes in the define() wrapper, or how can I configure my tests to import the class without it being wrapped in a define() wrapper?

Upvotes: 1

Views: 956

Answers (1)

Rajab Shakirov
Rajab Shakirov

Reputation: 8005

You should to use "--module amd" parameter

in visual studio 2015: Project/"your project" properties/Typescript Building/module system/amd

and next:

bla.ts:

function Bla() {
    
}
Bla.prototype.hi = function () {
    return "hey";
};
export = Bla;

bla.js:

define(["require", "exports"], function (require, exports) {
    function Bla() {
    }
    Bla.prototype.hi = function () {
        return "hey";
    };
    return Bla;
});

Upvotes: 1

Related Questions