This article first describes the installation of Karma-jasmine. In the next (ii), we use the assertion library to explain how to Karma-jasmine unit test.
Karma Introduction
first of all, Karma That's what the authorities are giving, Karma is not a test framework, nor is it an assertion library. Karma just launches an HTTP server and generates an HTML file. So you can choose your own favorite test framework. such as jasmine,mocha,qunit and so on.
Jasmine Introduction
Jasmine That's what it says. JavaScript test the framework. It does not depend on any other JavaScript components, it has a clean syntax,theJasmine Assertion Library allows you to simply write out the test code, (assert: expressed as some boolean expression, You can assume that when the output of the program is consistent with the hypothetical result, the assertion is true, otherwise, the assertion is false) running Jasmine on the Karma can be done Automated testing of JavaScript, generate coverage reports, and more.
Karma Installation
① installation node. js
Karma is run on node. js on the. So first install node. js. Reference Address: Http://pan.baidu.com/s/1jHrCh74#list/path=%2F Download all the way Next can (warm tip: Better change the installation path)
② installation Karma
Click the Start menu in the lower left corner of the desktop to find node. js in the first item, such as:
Install the karma in your preferred location. I installed it under E:\tomjia-soft\karma (for a file directory jump with a CD under Windows)
Install Karma, enter command:npm Install Karma --save-dev
The Ok,karma has been installed successfully.
③ Installing plugins
Next, install the Karma-jasmine, Karma-chrome-launcher (Chrome browser launcher) plugin.
NPM Install karma-jasmine karma-chrome-launcher --save-dev
OK, the plugin has been installed successfully.
④ installation Karma-cli
KARMA-CLI is used to simplify karma calls, making it easy to use Karma
Input command:npm install-g karma-cli
The installation has all been completed. After the installation is complete, the following directories:
Karma use
Start Karma, enter command:Karma Start
Automatically pop-up the Chrome browser window
The command window is returned with an error because there is no configuration file. At this time do not have the tube, directly shut down the command window. The browser is also closed.
Re-jump to the Karma directory, such as: and execute the Karma init command.
Description: ① which test framework you choose: we choose Jasmine
② do you want to use Require.js? : (Requirejs is a very compact JavaScript module loading framework) We don't need it here.
③ you want that browser to automatically capture: there can be more than one choice, we only use chrome.
......
⑥ you want karma to always monitor all the files and keep testing? : We need automated testing, so we choose Yes.
Configuration file is done. Such as:
To open a profile: We need to note two places:
1,2 are set the path. Bathpath in 1 : Can be set (set to the path relative to profile karma.conf) or not set. If not set. The path in files in 2 is the path relative to the configuration file karma.conf. The following will be reflected. If Bathpath is set, the path in files in2 is relative to the path in 1 .
We create a new folder named TestFiles. At the top level of the configuration file. There are two JS files under TestFiles. The test file jasminetest.js and the test file Test.js, respectively. As shown:
Next we do not set the configuration file in 1, directly set 2 path (of course you can also set 1, then set 2). :
files: [ '.. /testfiles/jasminetest.js ', '. /testfiles/test.js ' ],
Test.js File Contents:
function TT () { return "abc";}
Jasminetest File Contents:
function () { It (function () { expect ("abc"). Toequal (TT ()); });});
Start Karma again: The results are as follows:
Click Debug:f12 Debug
Change test.js content, such as:
function TT () { return "abc123";}
Save, open a command window: observe to be able to:
Open Browser, Refresh! Refresh! , click Debug F12 Debugging observation to get:
This reflects the ability of karma to automatically test through the browser.
Karma-jasmine Installation and examples (i)