Last active
December 4, 2019 08:09
-
-
Save casperin/b3d4aeb825dfb8353825 to your computer and use it in GitHub Desktop.
Karma, PhantomJs, Jasmine setup using npm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* I couldn't find a super simple example of how to run Karma, PhantomJs, with | |
* Jasmine tests via npm, so eventually I decided to create my own. Maybe | |
* someone will find it useful. | |
* | |
* | |
* Installation (using npm): | |
* | |
* npm install -g karma-cli | |
* npm install -g karma --save-dev | |
* npm install -g phantomjs | |
* npm install -g karma-jasmine --save-dev | |
* npm install -g karma-phantomjs-launcher --save-dev | |
* | |
* | |
* file structure: | |
* | |
* index.js // This file | |
* test/ | |
* karma.conf.js // This is auto generated | |
* test.js | |
* | |
* | |
* Once everything is installed, you run: | |
* | |
* karma init test/karma.conf.js | |
* | |
* And to run the actual tests: | |
* | |
* karma start test/karma.conf.js | |
* | |
* | |
* Below is just an object with some functions that are easy to test. | |
* | |
*/ | |
var dummy = { | |
aboveFive: function (n) { | |
return n > 5; | |
}, | |
setClass: function (el, classname) { | |
el.className = classname; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* This file is auto generated when you run | |
* karma init test/karma.conf.js | |
* | |
* I only included it here for you to see. | |
* | |
*/ | |
// Karma configuration | |
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: [ | |
'index.js', | |
'test/test.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 | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* globals describe, it, expect, dummy */ | |
describe('Checking that everything is hooked up nicely', function () { | |
it('Simple function', function () { | |
expect(dummy.aboveFive(2)).toEqual(false); | |
}); | |
it('involving the dom', function () { | |
var el = document.createElement('span'); | |
dummy.setClass(el, 'foo'); | |
expect(el.className).toBe('foo'); | |
}); | |
}); |
Note: Since karma-jasmine 0.3.0 the jasmine library is no longer bundled with karma-jasmine and you have to install it on your own. You can simply do it by:
$ npm install jasmine-core --save-dev
How to use webpage
within tests? I currently get errors
var webPage = require("webpage");
var page = webPage.create();
PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
{
"message": "Error: Cannot find module \"webpage\
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you