Unit Test, Karma, Jasmine

My current job content is developing and maintaining a web application which written in PHP. It requires same Javascript for front-end, but our team current development process based on others teams requirements:

When they ask, we developβ€¦πŸ™„

No time line, no strictly requirement, all work check and test done by visual result in browser.

After working on current project for over 6 months, I realized I have no any efficiency process to test my Javascript code (no any test for PHP side either). I wanna try on Karma and Jasmine on our PHP MVC framework, only do unit test for my Javascript part, no plan for PHP code so far.

Task break down
  1. set up environment
  2. write unit test case for current exist Javascript file
  3. run and debug
  4. repeat step 2, step 3
Set up test environment in current project
  1. Initiated a Node.js project under the current project directory
1
npm init
  1. Install Karma, Jasmine, minimatch and others dependencies base on installation result
1
2
3
4
npm install karma --save-dev
npm install karma-cli -g
npm install karma-jasmine --save-dev
npm install minimatch --save-dev
  1. Configure karma.conf.js, see configuration instruction here. Select Jasmine as frameworks.
  2. Specify file paths in karma.conf.js, add current project dependencies such as jQuery, Bootstrap and other Javascript libraries if necessary.
  3. Finally specify the unit test file and source file path in karma.conf.js, for example:
1
2
3
4
files: [
'www/js/*.js', // source file path
'test/www/js/*Spec.js' // unit test file path
]

using minimatch to have more control on file selection.

Write unit test in *Spec.js
β‡…
Debug and write more unit test