Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Typescript tests for Jasmine\Karma through a TFS Build Process

I have a web application using Angular 1.5 with bower/npm/gulp coded in Typescript to do our build. Our back end is a c# .net WebApi2. Both are built and deployed on TFS2015. My c# nUnit tests are easy to integrate as part of the build process. The Typescript jasmine unit tests however are more difficult to integrate. How do I get my Typescript jasmine unit tests to run as part of the TFS build and if they fail, fail the build? We have them running through a Jasmine Spec runner and also Karma but not integrated.

I have read the many posts on StackOverflow integrating Javascript unit tests and each avenue took me through an overly complex solution that didn't work. These include Powershell scripts, Chutzpah amoungst others.

like image 651
sarin Avatar asked Aug 15 '16 08:08

sarin


1 Answers

Rather than try to recreate the Specrunner via Chutzpah on the build server, which I found difficult to configure and get working. The aim was to get karma to output the running tests in the 'trx' test format that TFS recognises and then publish them to the build. Please note I am using PhantomJs to run my tests through Karma but won't cover that here as it is well covered elsewhere.

1) install the karma-trx-reporter plugin via npm into your web project (or similar plugin)

2) Configure the Karma.config to include the trx reporter

reporters: ['dots', 'trx'],

trxReporter: { outputFile: 'test-results.trx' },

// notify karma of the available plugins
plugins: [
  'karma-jasmine',
  'karma-phantomjs-launcher',
  'karma-trx-reporter',
],

3) Create a Gulp (or grunt) task to run the karma tests if you don't already have one. Run the task locally and check it creates the 'test-results.trx' specified above. (It doesn't matter where the file is created on the build server):

gulp.task('test', function () {
    return gulp.src(['tests/*.js']).pipe(karma({
        configFile: __dirname + '/Testing/karma.config.js',
        singleRun: true
    }));
});

4) Add a Gulp (or Grunt) TFS build task to run the karma tests created in the previous step and output the trx file. enter image description here

5) Add a Gulp (or Grunt) TFS build task to Publish the test results and merge them into the build. Note that the "Test Result Files" path is a wild card **/*.trx to find any trx files in the build path (i.e. finds our previously created file). "Merge Test results" is checked to Merge both our Jasmine test run and our c# test run into the same session. "Continue on error" is unticked to ensure any jasmine test failures break the build.

enter image description here

You will notice two sets of tests that have been run and included as part of the build!

enter image description here

like image 86
sarin Avatar answered Sep 30 '22 20:09

sarin