Unit testing using Jasmine and TypeScript

jasmine typescript
typescript unit testing jest
jasmine testing
typescript unit testing karma
jasmine tutorial
jasmine-ts no specs found
mocha typescript
jasmine vs jest

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 {
    ...
}

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

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

In package.json:

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

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.

Unit testing using Jasmine and TypeScript, Here's (in my opinion) the best way to test a ts-node app as of 2018: npm install --​save-dev jasmine @types/jasmine ts-node. In package.json : Debugging unit tests using Node 8+ currently only works for JavaScript test files, TypeScript test files will fail to hit breakpoints. As a workaround use the debugger keyword.

Put this at the top of your typescript spec file:

/// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
let Jasmine = require('jasmine');

You must install the following Jasmine modules for that to work:

$ npm install jasmine-core jasmine @types/jasmine jasmine-ts --save-dev

Once you do that, the IDE (such as WebStorm) will recognize Jasmine and its functions such as describe(), it(), and expect().. So you don't need to prefix them with "Jasmine." Also, you can run your spec files from the command line using the jasmine-ts module. Install these command line tools globally:

$ npm install -g jasmine jasmine-ts

Then configure the "jasmine" command line module so that Jasmine can find its configuration file. Then you should be able to run jasmine-ts and your spec file should run fine from the command line:

./node_modules/.bin/jasmine-ts src/something.spec.ts

.. and, you can configure your IDE to run it like that as well, and debug runs that way should also work (works for me).

Writing your tests this way, you can run a Jasmine test spec on the server side without Karma, or run it in a web browser using Karma. Same typescript code.

Typescript: writing tests with jasmine, Testing Angular 2+ Apps with Jasmine and Karma | Mosh. Programming with Unit testing Duration: 7:30 Posted: Nov 11, 2018 We'll be testing an Angular application using Jasmine, a fantastic framework for writing solid unit tests. TypeScript will transport us into the future of JavaScript, transforming our authoring experience, and allowing us to use current and proposed ECMAScript language enhancements.

For me I did the following:

Install Typings

npm install typings --global

Then add the typings in for jasmine

typings install dt~jasmine --save --global

Jasmine & Karma • Angular, We'll be testing an Angular application using Jasmine, a fantastic framework for writing solid unit tests. TypeScript will transport us into the future of JavaScript, transforming our authoring experience, and allowing us to use current and proposed ECMAScript language enhancements. Introduction. This is a quick tip on how to write unit tests for Typescript using Jasmine. Background. I have used Jasmine with vanilla Javascript in the past however testing Typescript was bit different and took me few hours to set up the environment correctly.

Include this to your jasmine html file,...

<script type="text/javascript" src="jasmine/lib/jasmine-2.0.0/jasmine.js"></script>

...or install the npm jasmine package:

npm install --save-dev jasmine

when you are using the second way (jasmine as module) you have to import it:

var jasmine = require('jasmine');

or

import jasmine from 'jasmine';

then change the other code:

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");
    });
});

Personally i would prefer the first way without using the jasmine npm module. (I didn't test the module yet)

Jasmine Documentation, Setup Jasmine testing framework with Node and TypeScript. My typescript project setup is described in this article. To be able to write tests in  GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up Sample of Unit Testing using Karma, Jasmine and Typescript.

You could try a side-effect only import which brings in the @types/jasmine declaration and places the jasmine functions into the global scope so you don't need to prefix each call with jasmine. allowing a quick port from existing unit tests and still plays nice with webpack.

// tslint:disable-next-line:no-import-side-effect
import "jasmine";

describe("My Unit Test", () => { /* ... */ } );

Of course you still need to install jasmine and the typings:

$ npm i jasmine @types/jasmine --save-dev

But no need for specialized jasmine loaders for ts or node. Just run jasmine against the compiled js files:

$ node ./node_modules/jasmine/bin/jasmine.js --config=test/support/jasmine.json

Assuming your typescript files are within a "test" subdirectory compiling to bin/test and you have a test/support/jasmine.json with something like this:

{
    "spec_dir": "bin/test",
    "spec_files": [
      "**/*[sS]pec.js"
    ],
    "stopSpecOnExpectationFailure": false,
    "random": false
}

P.S. all of the above works on Windows too

Testing JavaScript with Jasmine and TypeScript, Unit testing JavaScript and TypeScript in Visual Studio Mocha (mochajs.org); Jasmine (Jasmine.github.io); Tape (github.com/substack/tape)  Jasmine is the most popular JavaScript testing framework in the Angular community. This testing framework supports a software development practice which is called Behavior Driven Development or BDD. This is one the main features of Test Driven Development (TDD).

Testing TypeScript Node app with Jasmine – Bisaga Blog, Jasmine also works with JavaScript-based languages like TypeScript Basic white-box unit tests; REST API integration tests with frisby; Web  The unit test. Next, each unit test is a piece of code that validates the unit by: Calling it with specific inputs, and, Checking the output against the expected value. A unit test must follow the conventions of the test framework you’re using. In this case, let’s use Jasmine. Save this code to test/normalize_tests.js:

Unit testing JavaScript and TypeScript, In this article, we will use Jasmine and Karma to perform unit tests in After installing Node.js, we need to install TypeScript for AngularJS. Writing unit tests in TypeScript. In this story, we would be using TypeScript for unit testing along with popular frameworks: Mocha/Chai, Jasmine or Jest. You have decided the framework and want to write unit tests in TypeScript, Great! We would walk through changes required to support unit tests in TypeScript.

JavaScript Testing with Jasmine, Photo by Fotis Fotopoulos on Unsplash. I will describe the way to configure TypeScript unit testing with test coverage. I used TypeScript, Mocha, Chai and Istanbul for setting those up.

Comments
  • es6: import Jasmine from 'path/here';. es5: var Jasmine = require('path/here');. Use beforeEach, it and expect instead of BeforeEach, It and Expect.
  • Thanks for your response. I thought in Typescript the following is valid? import Jasmine = require("..."). Though perhaps not as it's giving so many compilation errors...
  • When I try this I get the same problem as when I follow any other tutorial on how to do this: "ReferenceError: define is not defined". I think it's either because I'm using webpack or the fact that it's a vss sdk web extension.
  • This method is meant for plain ts-node tests (without webpack). If you want webpack, you should probably use karma.
  • In case you need to provide arguments to jasmine: "test": "ts-node --project ./tsconfig-server.json node_modules/jasmine/bin/jasmine --config=jasmine.json",
  • The problem with jasmine-ts is that it is based on Jasmine 2, so you cannot use the latest and greatest Jasmine 3 features. I hope they get it sorted. There is an open ticket for it on GitHub: github.com/svi3c/jasmine-ts/issues/27
  • @BennyNeugebauer "they" = us, since it's the open source community. So anyone who needs it can work on improving it, and I encourage you, because yes that would certainly be a helpful improvement!
  • You really shouldn't be using typings anymore. Much better to use the type definitions in the @types/* repos in NPM.