gruntjs - Typescript tests, with Karma, Jasmine and Grunt -


i've been trying use karma+jasmine run tests written in typescript.

searching online, seems quite few have managed such feat, not find complete guide how this. environment:

  • typescript using es6 modules
  • karma
  • jasmine
  • grunt

how 1 set whole thing?

see github repository full source.

package.json

let's start devdependencies:

"devdependencies": {     "grunt": "^0.4.5",     "grunt-contrib-watch": "^0.6.1",     "grunt-karma": "^0.12.1",     "grunt-ts": "^5.1.0",     "jasmine-core": "^2.3.4",     "karma": "^0.13.15",     "karma-jasmine": "^0.3.6",     "karma-phantomjs-launcher": "^0.2.1",     "karma-requirejs": "^0.2.3",     "load-grunt-tasks": "^3.3.0",     "phantomjs": "^1.9.18",     "requirejs": "^2.1.22" } 

gruntfile.js

watch

we watch .ts files, , upon change first transpile them, run tests:

watch: {     typescript: {         // watching typescript files (specs , units)         files: [ 'source/**/*.ts' ],         // first transpile, run tests         tasks: [ 'ts:default', 'karma:continuous:run' ],         options: {             spawn: false         }     } }, 

typescript compilation

we transpile .ts files using amd module. note need typings of jasmine:

ts: {     default: {         src: [             // note add typings jasmine here             'typings/jasmine.d.ts',             // compile .ts files (specs , units)             'source/**/*.ts'         ],         options: {             // need convert es6 es5 , use amd works             // requirejs             module: 'amd',             fast: 'never'         },         // output files temp folder         outdir: 'transpiled'     } }, 

karma

one .ts files in transpiled folder, can run karma tests:

karma: {     options: {         configfile: 'karma.conf.js'     },      continuous: {         loglevel:  'info',         singlerun: false,     } } 

karma.conf.js

the trickiest part:

  • we add requirejs framework.
  • we load transpiled files, include flag set false.
  • a file called test-main.js scan these files , dynamically load .spec files tests. test-main.js generated karma, see this guide more info.

like so:

module.exports = function(config) {     config.set({         // note use `requirejs`         frameworks: [ 'jasmine', 'requirejs' ],          files: [             // add transpiled files (specs , unit) included set             // false.             { pattern: 'transpiled/**/*.js', included: false },              // test main search files specs , load them             // dynamically.             'test-main.js'         ],          reporters: [ 'progress' ],          browsers: [ 'phantomjs' ],          autowatch: false,         singlerun: true     }) } 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -