tags |
---|
karam, jasmine, node.js, javascript |
npm init --y
npm i -D jasmine karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter
You can also use karma cli node package to cretae below config file. However, you can now copy below text in karma.conf.js
file.
// Karma configuration
// Generated on Tue Jul 13 2021 06:39:55 GMT-0400 (Eastern Daylight Time)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: 'src',
// frameworks to use
// available frameworks: https://www.npmjs.com/search?q=keywords:karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: ['**/*.js'],
// list of files / patterns to exclude
exclude: ['**/*.yml', '**/*.json', '**/*.md', '**/*.txt'],
// preprocess matching files before serving them to the browser
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
preprocessors: {},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://www.npmjs.com/search?q=keywords:karma-reporter
reporters: ['progress', 'kjhtml'],
client: { clearContext: false },
// 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://www.npmjs.com/search?q=keywords:karma-launcher
browsers: ['Chrome', 'ChromeHeadless'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser instances should be started simultaneously
concurrency: Infinity,
});
}
"scripts": {
"test": "karma start",
"test:once": "karma start --single-run --browsers ChromeHeadless karma.conf.js"
},
Create a Javascript .js
or Javascript module .mjs
file add your test and run them.
/**
* RECOMMENDED IN COMPETETIVE PROGRAMMING
* Constant Time solution
*/
class Fibonacci {
/**
* O(n) time | O(1) space
*/
execute(num) {
if (num === 0 || num === 1) return num;
let previous0 = 0;
let previous1 = 1;
let result = 0;
for (let idx = 2; idx <= num; idx++) {
result = previous0 + previous1;
previous0 = previous1;
previous1 = result;
}
return result;
}
}
describe('Fibonacci', () => {
it('should work #1', () => {
var start = performance.now();
expect(new Fibonacci().execute(100)).toBe(354224848179262000000);
var end = performance.now();
console.log('Iterative', end - start);
});
it('should work #2', () => {
var start = performance.now();
expect(new Fibonacci().execute(3)).toBe(2);
var end = performance.now();
console.log('Iterative', end - start);
});
it('should work #3', () => {
var start = performance.now();
expect(new Fibonacci().execute(5)).toBe(5);
var end = performance.now();
console.log('Iterative', end - start);
});
});
npm test
https://github.com/rupeshtiwari/coding-examples-datastructures