Javascript unit testing in Visual Studio using Karma

Title Text

  • Alternative way of setting up 
  • Simplify Angular code testing, especially directives
  • Lots of helpful packages for preprocessing
  • Bullet Three

Stuff that you need

  • NodeJS
  • Python
  • Karma
  • Karma Test Adapter Visual Studio extension

NodeJS

Node installation location

NPM

  • NPM is a Node Package Manager, and it's included in NodeJS installation package

  • It's good to update NPM in order to tackle some windows related symptoms, like too deep folder structures that some node packages use

  • There's a powershell script by MS that's an easy way to update NPM:
    https://github.com/felixrieseberg/npm-windows-upgrade

     

Installation

You may need to alter the execution policy first

 

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade

Then run these commands

Python

Python Version

  • Install Python from: https://www.python.org/downloads/
  • Recommend to install latest 2.x version and not 3.x version, because otherwise you'll get following error when installing Karma:
gyp ERR! stack Error: Python executable "python" is v3.5.0, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.

Karma

Description

Karma Test Adapter

Testing Method

  • You can run the tests either in Visual Studio using extension, or running Karma in command line
  • Extension is nice, because you can easily navigate to failing tests

deck

By Jarmo Jokela

deck

  • 798