unit testing javascript: jasmine & karma intro

17
JASMINE & KARMA Unit testing your JavaScript code Maurice de Beijer

Upload: maurice-beijer

Post on 15-Jul-2015

490 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Unit testing JavaScript: Jasmine & karma intro

JASMINE & KARMAUnit testing your JavaScript code

Maurice de Beijer

Page 2: Unit testing JavaScript: Jasmine & karma intro

Goals

• Understand how to write Jasmine BDD specs

• Run Jasmine specs using Karma

• Test AngularJS code with Jasmine and Karma

Page 4: Unit testing JavaScript: Jasmine & karma intro

Jasmine

• Test suites describe the functionality

• Using a describe() function

• Can be nested

• Specs test the functionality

• Using an it() function

• Contain one or more expectations

Page 5: Unit testing JavaScript: Jasmine & karma intro

Basic Jasmine tests

Page 6: Unit testing JavaScript: Jasmine & karma intro

Running Jasmine tests

• We need a test runner to execute tests

• The browser

• Visual Studio with R#

• Karma

Page 7: Unit testing JavaScript: Jasmine & karma intro

Successful test in the Browser

Page 8: Unit testing JavaScript: Jasmine & karma intro
Page 9: Unit testing JavaScript: Jasmine & karma intro

Matchers

• Used to verify expectations

• toBe()

• toEqual()

• toMatch()

• toBeDefined()

• toBeTruthy()

• toContain()

• toBeGreaterThan()

• toBeCloseTo()

• And more

• Can create custom matchers if needed

• You can also throw an Error to fail a test

Page 10: Unit testing JavaScript: Jasmine & karma intro

Jasmine spies

• Can intercept function calls

• Verify if the function was called

• Optionally with specific parameters

• Can be configured to:

• Call original function

• Return a predefined result

• Call a fake function

• Throw an Error

Page 12: Unit testing JavaScript: Jasmine & karma intro

Karma Setup

• Karma is a NodeJS application

• So first install Node.js

• Install Karma using NPM

• NPM Install Karma-Cli – g

• NPM Install Karma –save-dev

• Karma requires a configuration file

• Create using karma init

• Requires a browser to run tests

• PhantomJS is an invisible browser

• Can use multiple browser at the same time• Either in the config file

• Or by navigating to http://localhost:9876/

Page 13: Unit testing JavaScript: Jasmine & karma intro

Successful test in Karma

Page 14: Unit testing JavaScript: Jasmine & karma intro

Failing test in Karma

Page 15: Unit testing JavaScript: Jasmine & karma intro

Testing AngularJS code

• Include angular-mocks.js as the last AngularJS script

• Changes some of the normal behavior

• Create Jasmine test as normal

• Add beforeEach() to load module under test

• Use the inject() function to trigger Dependency Injection

• The $controller service is the factory to create controllers

• Usually requires at least a $scope

• Other dependencies can be provided

• If not the normal DI lookup happens.

Page 16: Unit testing JavaScript: Jasmine & karma intro

$httpBackend

• Fake implementation of the XmlHttpRequest

• Expected requests can be configured

• whenGET, whenPOST etc

• expectGET, expectPOST etc

• When... functions are soft requirement

• Do not fail a test

• Will resolve multiple time

• Expect… functions are assertions

• Fail a test is not satisfied

• Appears to the calling code as asynchronous

• Use flush() to resolve requests

Page 17: Unit testing JavaScript: Jasmine & karma intro

Questions?

• Source code on GitHub

• https://github.com/mauricedb/Jasmine-Karma-Intro