DEV Community

Rupesh Tiwari
Rupesh Tiwari

Posted on • Originally published at rupeshtiwari.com on

Testing Node.js Project with Jasmine & Karma

Do you have node.js project that you want to write specs using Jasmine? Are you wondering how to write specs and run all specs in your node.js project? Lets answer all of this in this article so stay tuned!

Setting up Jasmine in node.js

In order to write specs for your project the best option is to use Jasmine. So do not delay and just install jasmine now.

# Run below script

npm i -D jasmine

Enter fullscreen mode Exit fullscreen mode

Now you can write your node.js with some specs over there just like below:

function reverseWord(word) {
  const length = word.length
  let reversed = ''
  for (let idx = length - 1; idx >= 0; idx--) {
    reversed += word[idx]
  }

  return reversed
}

// This is the spec written in Jasmin BDD style
describe('reverseWord', () => {
  it('test #1', () => {
    expect(reverseWord('Geeks')).toBe('skeeG')
  })
  it('test #1', () => {
    expect(reverseWord('for')).toBe('rof')
  })
})

Enter fullscreen mode Exit fullscreen mode

How to run the Jasmine specs in Node.js project?

Well you can use jasmine cli or javascript to run jasmine specs. Read this article to learn how to run specs in node.js.

However, if you want to debug your specs in Chrome browser. Then I would recommend use 3rd party test runner. So I choose karma. It is a test runner that will give you script to run all the specs in your project smoothly and you can debug them too!

How to setup karma in Node.js project?

Karma official document has nice steps to follow and install karma in your node.js project please read them here.

I followed below steps to setup karma in my node.js project.

Step 1: Installing Karma plugins

# Install Karma:
npm install karma --save-dev

# Install plugins that your project needs:
npm install karma-jasmine karma-chrome-launcher jasmine-core --save-dev

# Run below then, you can run Karma simply by karma from anywhere and it will always run the local version.
npm install -g karma-cli

Enter fullscreen mode Exit fullscreen mode

Step 2: Creating Karma Configuration file

Read this article to follow step by step to configure karma.

I did below to configure karma in my node.js project.

karma init karma.conf.js

Which testing framework do you want to use?
Press tab to list possible options. Enter to move to the next question.
> jasmine

Do you want to use Require.js?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> no

Do you want to capture a browser automatically?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome
> ChromeHeadless
> ""

What is the location of your source and test files?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Press Enter to move to the next question.
>**/*.js
>""

Should any of the files included by the previous patterns be excluded?
You can use glob patterns, eg. "**/*.swp".
Press Enter to move to the next question.
>

Do you want Karma to watch all the files and run the tests on change?
Press tab to list possible options.
> yes

Enter fullscreen mode Exit fullscreen mode

Step 3: Add reporters to show spec result text in Chrome browser

You need to follow below steps to show your spec text in chrome browser:

View Karma Test Output in a Chrome Browser Steps

View Karma Test Output in a Chrome Browser?

  • Install npm i -D karma-jasmine-html-reporter
  • Open karma.conf.js
  • add 'kjhtml' in your reporter.
  • add client:{clearContext:false}
    reporters: ['progress', 'kjhtml'],
    client:{clearContext:false},

Enter fullscreen mode Exit fullscreen mode

Example file for karma.conf.js

Step 4: Starting Karma and running specs

Add below script in your npm package scripts.

If you want to run your specs on chrome browser and watch your files and auto refresh specs:

"test": "karma start karma.conf.js"

Enter fullscreen mode Exit fullscreen mode

Just run once your node.js specs in ChromeHeadless browser without watch.

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

Enter fullscreen mode Exit fullscreen mode

Run all node.js specs locally

Lets run specs npm run test

Hurray! 💯 my specs are running and it is watching too!

Lets test in without watch mode.

npm run test:once

Hurray! 💯 again tests are working. Superb.

We did it our project is setup and enjoy writing specs.

Here is my node.js project that has jasmine and karma setup. If you enjoy this project please press the star button.

Next step is how to run these specs in GitHub when we push the code? This I will explore in my next blog so stay tuned!

Happy coding and be happy!


Thanks for reading my article till end. I hope you learned something special today. If you enjoyed this article then please share to your friends and if you have suggestions or thoughts to share with me then please write in the comment box.

Become full stack developer 💻

I teach at Fullstack Master. If you want to become Software Developer and grow your carrier as new Software Engineer or Lead Developer/Architect. Consider subscribing to our full stack development training programs. You will learn Angular, RxJS, JavaScript, System Architecture and much more with lots of hands on coding. We have All-Access Monthly membership plans and you will get unlimited access to all of our video courses, slides , download source code & Monthly video calls.

  • Please subscribe to All-Access Membership PRO plan to access current and future angular, node.js and related courses.
  • Please subscribe to All-Access Membership ELITE plan to get everything from PRO plan. Additionally, you will get access to a monthly live Q&A video call with Rupesh and you can ask doubts/questions and get more help, tips and tricks.

Your bright future is awaiting for you so visit today FullstackMaster and allow me to help you to board on your dream software company as a new Software Developer, Architect or Lead Engineer role.

💖 Say 👋 to me!

Rupesh Tiwari

Founder of Fullstack Master

Email: rupesh.tiwari.info@gmail.com

Website: RupeshTiwari.com

Top comments (0)