Write Typescript UT with Karma & Jasmine

What is Karma


Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应,比Cassandra这种名字更让人猜不透!

Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuous integration)工具,也可和其他代码编辑器一起使用。这个测试工具的一个强大特性就是,它可以监控(Watch)文件的变化,然后自行执行,通过console.log显示测试结果。

Install Karma


Using below commands to install Karma with npm

Karma Config


karma.conf.js

  • basePath
  • frameworks
  • files
  • excludes
  • port
  • browsers

Example:

test-main.js


test-main.js is the entrance of the test run.

Example:

Write Typescript UT


Step
1. Create module

Create module with $provide

2. Register filter/controller to module

3. Apply module

4. Inject the filter/scope/controller or other resources

or use new keyword to create an instance, this can skip the step 1,2 and 3

5. Write Cases

Run UT


At the root folder of the source code, for example,

Run the below command:

You will see the test result:
result

Debug


Click the “Debug” button on the page to enter debug mode

debug

Tips


  • It will run UT automatically when it detected file changes
  • Terminates it press Crtl + C

References


Karma Introduction
Karma和Jasmine自动化单元测试

打赏

Leave a Reply

Your email address will not be published. Required fields are marked *