Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupeshtiwari/bfb27c26729ada7ab62b73484afbad19 to your computer and use it in GitHub Desktop.
Save rupeshtiwari/bfb27c26729ada7ab62b73484afbad19 to your computer and use it in GitHub Desktop.
Karma and Jasmine Setup for Node.js Projects
tags
karam, jasmine, node.js, javascript

Create new node project

npm init --y

Npm packages required for Node.js project to enable Karma

npm i -D jasmine karma karma-chrome-launcher karma-jasmine karma-jasmine-html-reporter

Create Karma.conf.js file at root folder of project

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,
  });
}

Creating npm test script

"scripts": {
    "test": "karma start",
    "test:once": "karma start --single-run --browsers ChromeHeadless karma.conf.js"
  },

Adding specs

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);
  });
});

Run specs

npm test 

Sample project

https://github.com/rupeshtiwari/coding-examples-datastructures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment