Step 1 : go to Jasmine release page https://github.com/jasmine/jasmine/releases download the standalone zip file.
Step2: create a project folder in your computer where you like to put it, unzip the jasmine standalone zip file inside your project folder, then we could delete the sample files inside src and spec folder since we won’t using it, but keep the folder, so we could still using it.
Step3: run npm init (make sure nodejs has been install in your computer) will create package.json file
Step4: install karma as test runner
npm install karma --save-dev
Step5: install -g karma-cli
Step6: npm install karma-jasmine karma-chrome-launcher –save-dev
Step7:karma init (for add the config file for karma)
Step 8: normal running test in phantom is fast so also need install packages:
“karma-phantomjs-launcher”: “^0.2.1”,
“phantomjs”: “^1.9.18”
Now your package.json file will look like this
{
"name": "sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "karma start karma.conf.js"
},
"keywords": [
"sample"
],
"author": "sandy zhang",
"license": "ISC",
"devDependencies": {
"jasmine-core": "^2.3.4",
"karma": "^0.13.9",
"karma-chrome-launcher": "^0.2.0",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^0.2.1",
"phantomjs": "^1.9.18"
}
}
Step9: Add angularjs and angular-mock.js into the lib folder, and included it into the karma config file
// Karma configuration
// Generated on Sat Nov 12 2016 22:15:24 GMT+1100 (AUS Eastern Daylight Time)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'lib/angular/angular.min.js',
'lib/angular/angular-mocks.js',
'src/**/*.js',
'spec/**/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
Now I create a calculater.js file inside src folder with one function
function add(x,y){
return x+y;
};
And create your first calculater.spec.js inside spec folder
describe(‘calculator’,function(){
it(‘should add two number’,function(){
expect(add(1,2)).toBe(3);
})
});
then run npm test, then your will get your result.