Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Client Side Testing
Search
Andrew Best
May 26, 2015
Programming
75
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Client Side Testing
A quick trip around javascript and Angular unit testing using Jasmine and Chutzpah.
Andrew Best
May 26, 2015
More Decks by Andrew Best
See All by Andrew Best
The Surprising Truths Behind Good Mentoring
andrewabest
0
140
Learn Authentication The Hard Way
andrewabest
0
350
Finding The Sweet Spot BNE
andrewabest
1
260
Finding The Sweet Spot
andrewabest
0
480
Automating AWS
andrewabest
2
380
Conventional Wisdom
andrewabest
1
490
DSC a-b-c's
andrewabest
0
160
AWS a-b-c's
andrewabest
3
170
What is Git?
andrewabest
0
230
Other Decks in Programming
See All in Programming
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
120
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
160
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
130
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
480
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.3k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
3
1.3k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
180
Agentic UI
manfredsteyer
PRO
0
130
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
6
870
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
350
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
250
Featured
See All Featured
Odyssey Design
rkendrick25
PRO
2
690
How to build a perfect <img>
jonoalderson
1
5.6k
Leo the Paperboy
mayatellez
7
1.8k
Documentation Writing (for coders)
carmenintech
77
5.4k
Evolving SEO for Evolving Search Engines
ryanjones
0
210
Faster Mobile Websites
deanohume
310
31k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
1
170
Making Projects Easy
brettharned
120
6.7k
The agentic SEO stack - context over prompts
schlessera
0
800
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Transcript
Testing the web with Chutzpah and Jasmine
Page “I’m a c# guy that uses NUnit” › In
the javascriptworld, Jasmine is your NUnitequivalent. (http://jasmine.github.io/) › In Jasmine, you create ‘Suites’ that describe a set of expectations you have for the thing you want to test. › Jasmine also allows you to also set your expectations within these suites. / Copyright ©2014 by Readify Pty Ltd 2
Page So how do I do X in Jasmine? ›
Setup and teardown: › Ignore tests: / Copyright ©2014 by Readify Pty Ltd 3
Page Cool, now how do I run my tests? ›
Chutzpah! (https://github.com/mmanela/chutzpah) › Chutzpah generates html to run our specs with Jasmine in a headless browser (phantomjs). › It will also generate html to run our specs in our browser of choice (handy for debugging tests!) › It also takes care of loading dependencies for us where needed. / Copyright ©2014 by Readify Pty Ltd 4
Page So how to we hook it all up? ›
Install-Package Chutzpah › Install the Chutzpah VS Extensions › Add and configure your chutzpah.json › Don’t worry about installing Jasmine! Chutzpah bundles it up for you › Write your hello world unit test! / Copyright ©2014 by Readify Pty Ltd 5
Page Man this thing is busted! › Chutzpah is a
bit of a black box when used from VS – if it doesn’t seem to be working from VS, you can always use the command line runner. › Running chutzpah.console.exe {fullpathtofile.js} /trace will give you a detailed log of the test run named chutzpah.log alongside the console exe (in your packages folder) / Copyright ©2014 by Readify Pty Ltd 6
Page Okay cool, now how about Ng? › To test
an Ng app, because of how angular’smodule / DI system works, we need to make sure we include all necessary references to the applications scripts in our chutzpah.json › If you have a bunch of bootstrapping activity in your standard app.js, you may want to craft a slimmed down version for testing / Copyright ©2014 by Readify Pty Ltd 7
Page Dude, where’s my Mocks? › ngMock(https://docs.angularjs.org/api/ngMock) › Injects and
mocks most of the core angular services for us, such as $http and $log › It *must* be loaded by Chutzpah after angular.js (it overrides default behaviour) › No further wireuprequired! › Jasmine also provides some mocking functions similar to NSubstitute / Copyright ©2014 by Readify Pty Ltd 8
Page Example Ng Test / Copyright ©2014 by Readify Pty
Ltd 9 describe("Landing Page Directive", function() { var $sut; describe('If form is started', function() { var goToDashboardCalled = false; beforeEach(function() { module(function($provide) { $provide.service('utilityService', function() { return { goToDashboard: function() { goToDashboardCalled = true; } } }); }); inject(function($compile, $rootScope, $httpBackend) { $httpBackend.expectPOST('apiBaseUrlforms/getbasicformdetails').respond(200); $rootScope.formStarted = true; var elem = angular.element("<div landing-page></div>"); $sut = $compile(elem)($rootScope); $rootScope.$digest(); }); }); it('Redirects to the dashboard.', function() { expect(goToDashboardCalled).toBe(true); }); }); });
Page Hey what were those things? › Module: allows us
to replace services to be injected into our class under test. This allows us to mockother things out. › Inject: allows us to mutate mocked dependencies to set them up for our SUT, and also supply them when building our SUT. › These are both given to us via ngMock, and for ease of use are attached to the window object for us. / Copyright ©2014 by Readify Pty Ltd 10
Page Ng testing gotchas › Http interactions *must* be mocked.
› You can only set up dependencies via Module() before your call to Inject(). If you call it after… BOOM. › If something doesn’t seem to be working, remember to double check your chutzpah.jsonand the html generated by Chutzpah for script errors! / Copyright ©2014 by Readify Pty Ltd 11
Page Staying DRY › So you have a bunch of
code you want to run before every test? Sick of copy pasta? › Jasmine allows us to define a spec-helper.js file which can include global beforeEvery and afterEvery functions! › This can be a useful place to bootstrap things like Ng constants, if you have some. / Copyright ©2014 by Readify Pty Ltd 12
Page Bonus slide: CI › So now I have my
tests, how do I plug them into my CI pipeline? › Use the Chutzpah console exe that you checked in with your chutzpah package because you are a good developer and detest package restore. › And it goes a little something like this… / Copyright ©2014 by Readify Pty Ltd 13
None