Reputation: 33139
I use AngularJS with TypeScript in a Web application in Visual Studio 2013. I can run tests in JavaScript just fine, but the tests written in TypeScript are not run. In fact, the ReSharper Test Runner displays a "Test Not Run" status for every test written in TypeScript, and I have no idea why. When I read blogs about this topic, they describe exactly what I do.
My code for such a unit test is:
/// <reference path="../typings/jasmine/jasmine.d.ts" />
/// <reference path="../AngularApp/Models/Member.ts" />
/// <reference path="../AngularApp/Models/Contract.ts" />
describe("Member", () => {
"use strict";
var contracts = new Array<App.Controllers.Contract>();
var sut = new App.Controllers.Member("Elvis", "Presley", "350108-11.2222",
new Date("1935-01-08"), new Date("1977-08-16"), "M", "Home", "", "90210", "Graceland",
contracts);
it("must not be null", () => {
expect(sut).not.toBeNull();
});
it("must contain zero Contracts", () => {
expect(sut.contracts).not.toBeNull();
expect(sut.contracts.length).toBe(0);
});
});
What can cause this problem?
Upvotes: 2
Views: 2355
Reputation: 159
Jasmine is a framework for testing JavaScript. As such, it is incompatible with TypeScript. You can use TypeScript and test it, you have to transpile it first into JavaScript, using Grunt, Gulp or Webpack.
install Typescript package, and then you can use tsc in command line to turn all .ts files into .js files - then it should work.
I was stuck on the same problem :)
Upvotes: 0
Reputation: 2206
You could try wrapping your test setup code in a beforeEach method:
/// <reference path="../typings/jasmine/jasmine.d.ts" />
/// <reference path="../AngularApp/Models/Member.ts" />
/// <reference path="../AngularApp/Models/Contract.ts" />
describe("Member", () => {
"use strict";
var contracts : any;
var sut : any;
beforeEach(() => {
var contracts = new Array<App.Controllers.Contract>();
var sut = new App.Controllers.Member("Elvis", "Presley", "350108-11.2222",
new Date("1935-01-08"), new Date("1977-08-16"), "M", "Home", "", "90210", "Graceland",
contracts);
});
it("must not be null", () => {
expect(sut).not.toBeNull();
});
it("must contain zero Contracts", () => {
expect(sut.contracts).not.toBeNull();
expect(sut.contracts.length).toBe(0);
});
});
Upvotes: 0
Reputation: 204
You cannot directly run tests written in typescript, they need to be transpiled into js first. Since your using visual studio i suppose your ts files are compile on save, so you need to check test config file ( example Karma.conf.js) to see if it looking for tests in the right directory. If you want to debug your ts test file, your test runner in visual studio must support source-maps. Have a look at this test runner for Vs2013 : https://github.com/MortenHoustonLudvigsen/KarmaTestAdapter. It supports source-maps.
Upvotes: 1