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
- Install Node from: https://nodejs.org/en/
- If you want to change nodejs installation location and where global node packages and cache are stored, follow instructions on this post:
- http://stackoverflow.com/questions/7300132/how-to-use-npm-with-node-exe/9366416#9366416
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 installation instructions: http://karma-runner.github.io/0.13/intro/installation.html
- Karma configuration instructions: http://karma-runner.github.io/0.13/intro/configuration.htmlBullet
Karma Test Adapter
- You can run integrate Karma to Visual Studio and run unit tests from Test Explorer using Karma Test Adapter extension: https://visualstudiogallery.msdn.microsoft.com/4cd59e4a-82e8-4b4e-8302-d102fc81b090
- Additional details: http://mortenhoustonludvigsen.github.io/KarmaTestAdapter/
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
- 850