Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing using Jasmine and TypeScript

I am trying to get a unit test written in Typescript using Jasmine to compile. With the following in my unit-test file, Resharper prompts me with a link to import types from jasmine.d.ts.

/// <reference path="sut.ts" /> /// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />  describe("Person FullName", function () {     var person;      BeforeEach(function () {         person = new Person();         person.setFirstName("Joe");         person.setLastName("Smith");     });      It("should concatenate first and last names", function () {         Expect(person.getFullName()).toBe("Joe, Smith");     }); }); 

So I click on the link and end up with the following (actually resharper only prefixed the describe function with "Jasmine.", so I manually prefixed the other Jasmine calls):

/// <reference path="sut.ts" /> /// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" /> import Jasmine = require("../../../Scripts/typings/jasmine/jasmine");  Jasmine.describe("Person FullName", function () {     var person;      Jasmine.BeforeEach(function () {         person = new Person();         person.setFirstName("Joe");         person.setLastName("Smith");     });      Jasmine.It("should concatenate first and last names", function () {         Jasmine.Expect(person.getFullName()).toBe("Joe, Smith");     }); }); 

However the import statement has a red squiggly line with error message "Unable to resolve external module ../../../scripts/typings/jasmine/jasmine. Module cannot be aliased to a non-module type"

Any idea what is causing this error? I've checked that the "Module System" option is set to AMD in my project build settings. I've also checked that the jasmine module is defined in jasmine.d.ts. I downloaded this file from DefinitelyTyped site.

declare module jasmine {     ... } 
like image 802
aw1975 Avatar asked Jun 16 '15 09:06

aw1975


People also ask

Does Jasmine support TypeScript?

jasmine : it is a testing framework for javascript code. It does not require DOM. @types/jasmine : it contains the type definition for Jasmine to be used with TypeScript.

Is Jasmine used for unit testing?

Jasmine is one of the popular JavaScript unit testing frameworks which is capable of testing synchronous and asynchronous JavaScript code. It is used in BDD (behavior-driven development) programming which focuses more on the business value than on the technical details.


1 Answers

Here's (in my opinion) the best way to test a ts-node app as of 2018:

npm install --save-dev typescript jasmine @types/jasmine ts-node 

In package.json:

{   "scripts": {     "test": "ts-node node_modules/jasmine/bin/jasmine"   } } 

In jasmine.json change file pattern to *.ts

"spec_files": ["**/*[sS]pec.ts"], 

In your spec files:

import "jasmine"; import something from "../src/something";  describe("something", () => {     it("should work", () => {         expect(something.works()).toBe(true);     }); }); 

To run the tests:

npm test 

This will use the locally installed versions of ts-node and jasmine. This is better than using globally installed versions, because with local versions, you can be sure that everyone is using the same version.

Note: if you have a web app instead of a node app, you should probably run your tests using Karma instead of the Jasmine CLI.

like image 87
AJ Richardson Avatar answered Sep 26 '22 19:09

AJ Richardson