Skip to content Skip to sidebar Skip to footer

Testing Typescript Generated Classes With Node.js, Mocha And Mocha-jsdom?

I would like to run unit tests from bash to test my Typescript generated Javascript classes using mocha and mocha-jsdom. I'm using mocha-jsdom because I want to emulate the dom in

Solution 1:

So let's start from executing the test. For executing the tests you have several options:

  • Use karma. Karma is a JavaScript test runner. Actually you specify inside a karma.config.js file all the tests you want to execute, and it will execute all your tests. You have also a watch functionality which allows you to execute the test every time you make changes to your code
  • Use node.js. Inside the script property of your package.json file, you specify the command test and you associate to that script the command in order to execute mocha against all your tests. Then you can run the test just typing npm test
  • If you want to use a bash, my suggestion is to use it in order to trigger the npm test command or the karma command. That's the easiest way you can use for starting tests through bash. You can also setup a .bat file on Win which runs your tests always through the npm/karma commands
  • Use a task runner as Gulp, Grunt etc... You can specify a task where you trigger your tests and then run the task providing for example the command gulp mytask

My suggestion is to use node.js directly at the moment. It makes things easier.

About TypeScript, you can directly use mocha with TypeScrit, no need of JavaScript. I suggest you to integrate mocha with chai, chai-as-promised and sinon. Chai is an assertion library. Chai-as-promised allows you to test promises and sinon to test functions and methods.

This is an example of package.json with the test command I was saying above:

"scripts":{"test":"find ./src -name '*spec.ts' | xargs mocha -r ts-node/register"}

With this line, you will execute all the files which end for *spec.ts. The ts-node is a really useful node plugin which allows you to run ts directly through the command ts-node myfile.ts

About the dependencies to include in your package.json here a useful list:

"devDependencies": {
        "@types/chai": "^3.5.0",
        "@types/chai-as-promised": "^0.0.30",
        "@types/mocha": "^2.2.40",
        "@types/node": "^7.0.12",
        "@types/sinon": "^2.1.2",
        "chai": "^3.5.0",
        "chai-as-promised": "^6.0.0",
        "husky": "^0.13.3",
        "mocha": "^3.2.0",
        "sinon": "^2.1.0",
        "ts-node": "^3.0.2",
        "typings": "^2.1.1"
}

About how to integrate mocha inside your tests, here a declaration of a spec file that you can use as patter:

// imports of testing libraries import * as mocha from'mocha';
import * as chai from'chai';
import * as chaiAsPromised from'chai-as-promised';
import sinon = require('sinon');
// import of the source files to be testedimportMainfrom'./Main'; // an example// testing inits
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
const should = chai.should();

You can try to take a look to this repo. There is a really simple project with TypeScript and all the testing libraries I said above. It's still in progress and I didn't have time to finish it yet, so also the main README.md is not updated. But if you clone the project or download the source, you can have a really panoramic of all the things I was saying above and an illustration of all the methods you can use inside your tests:

https://github.com/quirimmo/testing-typescript-mocha-chai-sinon

Hope this help! for any query let me know!

Post a Comment for "Testing Typescript Generated Classes With Node.js, Mocha And Mocha-jsdom?"