I read the two articles by Philip Walton:
http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/
http://philipwalton.com/articles/why-i-test-private-functions-in-javascript/
http://philipwalton.com/articles/why-i-test-private-functions-in-javascript/
My idea was inspired from these two articles.
It seems like whether and how to test the private function in Javascript is still a controversial topic.
For me, I choose to do it. There are two reasons:
First of all, some of the code are not written by me. I'd like to play around with my test code rather than changing others' work. And we haven't decided whether it's OK to make those code public.
Second of all, there are many private functions, I don't think it's good to skip all of them.
A brief instruction of the app I am working on
The app is built with gulp. We set the task
gulp test
for unit test.
In the app code, we have the following structure :
I will test the updateMemberBenefitTable() and loadMemberBenefitTalbe().
In the app code, we have the following structure :
angular.module('MainModule').controller('MainCtrl',MainController);
mainController.$injct = ['dep1','dep2']
function MainController(dep1,dep2){
var vm = this;
var name = '';
var isMember = false;
vm.somePubFuncs = function(){
// call those private functions from public function
}
function updateMemberBenefitTable(){
// dosomething here
}
function loadMemberBenefitTable(){
// dosomething here
}
}
My basic idea of testing the private functions
Adding some public interfaces for the private function at the beginning of test, and strip them at the end of the test.I use gulp-insert-lines for adding and gulp-strip-code for stripping.
I added the comments pair
i.e./*test-code-insert-here*/ /*end-test-code-insert-here*/
angular.module('MainModule').controller('MainCtrl',MainController); mainController.$injct = ['dep1','dep2'] function MainController(dep1,dep2){ var vm = this; var name = ''; var isMember = false; vm.somePubFuncs = function(){ // call those private functions from public function } /*test-code-insert-here*/ /*end-test-code-insert-here*/ function updateMember--BenefitTable(){ // dosomething here } function loadMemberBenefitTable(){ // dosomething here } }
The gulp task for unit test is:
var insertLines = require('gulp-insert-lines'); var stripCode = require('gulp-strip-code'); gulp.task('test',function(){// This section is to add the interfaces of the private function into the source file var str = '/*--test-only*/'+'\n'+ 'vm._updateMemberBenefitTable = updateMemberBenefitTable;'+'\n'+ 'vm._loadMemberBenefitTable= loadMemberBenefitTable;'+'\n'+ '/*--end-test-only*/'; gulp.src('path/of/MainCtrl') .pipe(insertLines({ 'after':/\/\*test-code-insert-here\*\/$/, 'lineAfter':str })) .pipe(gulp.dest('dest'));
//END This section is to add the interfaces of the private function into the source file
return gulp.src('testfile.js') .pipe($.karma({ configFile: 'karma.conf.js', action: 'run' })).on('end',function(){// This section is to strip off the interfaces of the private function
//at the end of the unit test gulp.src('path/of.MainCtrl') .pipe(stripCode({ start_comment:'--test-only', end_comment:'--end-test-only' })) .pipe(gulp.dest('dest'));
//END This section is to strip off the interfaces of the private function
//at the end of the unit test
}) .on('error',function(err){ throw err; })
Please keep in mind that the /*--test-only*/ /*--end-test-only*/ comments pair is very important. Because later on, when gulp-strip-code is stripping the interfaces, the comments pair will be stripped as well. If you use /*test-code-insert-here*/ /*end-test-code-insert-here*/ pair for stripping, you will end up with being not able to find them at the next run of gulp test.
Nice blog. Very informative. Thanks for sharing. Keep updating.
ReplyDeleteAngularjs online training
Angularjs online course
Angularjs online training in Hyderabad
Angularjs online course in kurnool
Angularjs online training in kurnool