SlideShare une entreprise Scribd logo
1  sur  47
Télécharger pour lire hors ligne
LEARN FRONTEND
TESTING
SEATTLE JS - OCT. 16TH, 2013
Ryan Roemer | @ryan_roemer
@FormidableLabs
SPONSORS
MENTORS

Try to keep pace with the presentation,
but side conversations are encouraged
and don't let us stop any good
directions.
And, thanks!
MOTIVATION

Web applications are increasingly
becoming frontend heavy.
We need to verify app logic and
behavior, and that means braving the
browser.
SO LET'S TEST

Backend is straightforward and easy
... but what about the frontend?
FRONTEND TESTING
Frontend testing is difficult and errorprone.
Asynchronous events, timing
Browser idiosyncracies
State of testing technologies
BUT GETTING BETTER
... so let's get started with a modern
frontend test stack.
GET THE CODE

github.com/FormidableLabs/learn-frontendtesting

$gtcoehts/gtu.o/omdbeas
i ln tp:/ihbcmFrialLb/
lanfotn-etn.i
er-rnedtsiggt
OVERVIEW
Installation and test page
Suites
Assertions
Fakes
Automation
WE WILL LEARN HOW TO
Hook frontend JS to tests
Write assertions against behavior
Fake application behavior
Run and verify the tests
THINGS WE'RE NOT
GOING TO COVER
TDD / BDD
Application development
Functional testing
Performance testing
CODING TIME
We're going to say hello:

"SeattleJS" ➞ "Hello SeattleJS!"
And camel case strings:
"fun-test-time" ➞ "funTestTime"
SET UP YOUR PROJECT
#Cp teseeo apiain
oy h kltn plcto.
$c - seeo M_P_AE
p r kltn YAPNM
PROJECT STRUCTURE
Using with the "skeleton" application.

M_P_AE
YAPNM/
j/
s
ap
p/
hloj
el.s
cmlj
ae.s
lb
i/
ca.s
hij
mcaj
oh.s
mcacs
oh.s
snnj
io.s
idxhm
ne.tl
HELLO!

skeleton/js/app/hello.js
/ Hlo[AU]
/ el VLE!
vrhlo=fnto (a){
a el
ucin vl
rtr "el "+vl+""
eun Hlo
a
!;
}
;
CAMEL CASE

skeleton/js/app/camel.js

/ Cmlcs asrn.
/ ae ae
tig
vrcml=fnto (a){
a ae
ucin vl
/ Upraetefrtcaatratrads
/ pecs h is hrce fe
a
rtr vlrpae/()g fnto (,fr
eun a.elc(-./, ucin m i
rtr frttUprae)
eun is.opeCs(;
};
)
}
;
DEMO

skeleton/index.html
TEST HARNESS
TEST LIBRARIES
Mocha (v . 3 0 Framework
1 1 . ):
Chai (v . . ): Assertions
173
Sinon.JS (v . . ): Fakes - spies and stubs
181
DIRECTORY LAYOUT
A combined structure.

M_P_AE
YAPNM/
j/
s
ap
p/
lb
i/
se/
pc
hlose.s
el.pcj
*se.s
.pcj
ts.tl
ethm
idxhm
ne.tl
THE TEST PAGE

Create a test "driver" web page.
example/test.html
$tuhM_P_AEts.tl
oc YAPNM/ethm
TEST.HTML
<tl
hm>
<ed
ha>
<il>rnedTsig/il>
tteFotn etn<tte
<- Apiainlbais ->
!- plcto irre. <citsc"saphloj"<srp>
srp r=j/p/el.s>/cit
<citsc"sapcmlj"<srp>
srp r=j/p/ae.s>/cit
<- Ts sye adlbais ->
!- et tls n irre. <ikrl"tlset
ln e=syehe"
he=j/i/oh.s"/
rf"slbmcacs >
<citsc"slbmcaj"<srp>
srp r=j/i/oh.s>/cit
<citsc"slbca.s>/cit
srp r=j/i/hij"<srp>
<citsc"slbsnnj"<srp>
srp r=j/i/io.s>/cit
TEST.HTML
<- Ts Stp->
!- et eu <cit
srp>
/ Stu Ca adMca
/ e p hi n oh.
wno.xet=ca.xet
idwepc
hiepc;
mcastp"d";
oh.eu(bd)
/ Rntsso wno la.
/ u et n idw od
wno.nod=fnto ( {
idwola
ucin )
mcarn)
oh.u(;
<srp>
/cit
TEST.HTML

<- Tss ->
!- et. <- ..se srp icue g hr ..
!- . pc cit nlds o ee .
<ha>
/ed
<oy
bd>
<i i=mca>/i>
dv d"oh"<dv
<bd>
/oy
<hm>
/tl

example/test-empty.html
MOCHA SUITES, SPECS
Spec: A test.
Suite: A collection of specs or suites.
SUITES, SPECS

test-mocha.html | mocha-suite.spec.js
dsrb(snl lvl,fnto ( {
ecie"ige ee" ucin )
i(sol ts smtig)
t"hud et oehn";
};
)

dsrb(tplvl,fnto ( {
ecie"o-ee" ucin )
dsrb(nse" fnto ( {
ecie"etd, ucin )
i(i so adayc,fnto (oe {
t"s lw n sn" ucin dn)
stieu(ucin( {dn(;} 30
eTmotfnto )
oe) , 0)
};
)
};
)
};
)
SETUP, TEARDOWN
test-mocha.html | mochasetup.spec.js

dsrb(stptadw" fnto ( {
ecie"eu/eron, ucin )
bfr(ucin(oe {dn(;};
eoefnto dn)
oe) )
bfrEc(ucin( {)
eoeahfnto ) };
atrfnto (oe {dn(;};
fe(ucin dn)
oe) )
atrahfnto ( {)
feEc(ucin ) };
i(sol ts smtig)
t"hud et oehn";
};
)
CHAI ASSERTIONS
Natural language syntax.
Chained assertions.
CHAI API
The "bdd" API:

Chains: t , b , b e , h v
o e en ae
Groups: a d
n
Basics: ae u ll n t , m t h
, qa, egh ac
HELLO!

test-hello.html | hello.spec.js
dsrb(hlo,fnto ( {
ecie"el" ucin )
i(sol syhlo,fnto ( {
t"hud a el" ucin )
epc(el(Wrd)
xethlo"ol")
.ob.(srn".n
t.ea"tig)ad
.oeul"el Wrd".n
t.qa(Hlo ol!)ad
.ohv.egh1)ad
t.aelnt(2.n
.omth/el{})
t.ac(H[]2/;
};
)
};
)
CAMEL CASE

test-camel.html | camel.spec.js

dsrb(cml,fnto ( {
ecie"ae" ucin )
i(hnlsbs css,fnto ( {
t"ade ae ae" ucin )
epc(ae(")t.qa(";
xetcml").oeul")
epc(ae(snl")t.qa(snl"
xetcml"ige).oeul"ige)
};
)
i(hnlsdse css,fnto ( {
t"ade ahd ae" ucin )
epc(ae(abc).oeul"B";
xetcml"--")t.qa(aC)
epc(ae(oeto).oeul"nTo
xetcml"n-w")t.qa(oew"
};
)
};
)
MORE CHAI

test-chai.html | chai.spec.js | chaifail.spec.js

dsrb(ca" fnto ( {
ecie"hi, ucin )
i(asrs,fnto ( {
t"set" ucin )
epc("n" "w")t.oti(to)
xet[oe, to].ocnan"w";
epc(fo {a:1})
xet{o: br 2}
.ohv.eppoet(fobr,1)
t.aede.rpry"o.a" 2;
};
)
};
)
dsrb(ca" fnto ( {
ecie"hi, ucin )
i(fis,fnto ( {
t"al" ucin )
epc(oe)t.qa(to)
xet"n".oeul"w";
};
)
SINON.JS FAKES

Dependencies, complexities? Fake it!
Spies: Observe function behavior.
Stubs: Spies that replace behavior.
Fake Server: Override $ a a , etc.
.jx
SINON.JS SPY

test-sinon.html | camel-spy.spec.js |
camel.js

dsrb(cml,fnto ( {
ecie"ae" ucin )
i(sisuprcs" fnto ( {
t"pe pe ae, ucin )
vrsy=snnsySrn.rttp,"o
a p
io.p(tigpooye tU

epc(p.alon)t.qa()
xetsyclCut.oeul0;
epc(ae(ab).oeul"B)
xetcml"-")t.qa(a";
epc(p.alon)t.qa()
xetsyclCut.oeul1;
epc(p.isCl.eunau)t.qa
xetsyfrtalrtrVle.oeul
syrsoe)
p.etr(;
};
)
SINON.JS STUB

test-sinon.html | camel-stub.spec.js |
camel.js

dsrb(cml,fnto ( {
ecie"ae" ucin )
i(susuprcs" fnto ( {
t"tb pe ae, ucin )
vrsu =snnsu(tigpooye "
a tb
io.tbSrn.rttp, t
fnto ( {rtr "O" };
ucin )
eun FO; )
epc(ae(ab).oeul"FO)
xetcml"-")t.qa(aO";
epc(tbclCut.oeul1;
xetsu.alon)t.qa()
su.etr(;
tbrsoe)
};
)
};
)
AUTOMATION

Drive our frontend tests with
PhantomJS using Mocha-PhantomJS
PREP TEST.HTML
Update the test.html file:

wno.nod=fnto ( {
idwola
ucin )
(idwmcahnoJ | mca.u(;
wno.ohPatmS | oh)rn)
}
;
HEADLESS!

Install and drive tests from the
command line:
$nmisalmcapatms
p ntl oh-hnoj
$nd_oue/bnmcapatms
oemdls.i/oh-hnoj
M_P_AEts.tl
YAPNM/ethm
... and that's all for now!
WHAT WE'VE COVERED
Test harness
Suites, specs
Assertions
Fakes
Automation
ADDITIONAL TOPICS
Advanced testing: DOM, fixtures
TDD / BDD
Functional testing
Performance testing
Continuous Integration: (Travis CI)
THANKS!

Ryan Roemer | @ryan_roemer
bit.ly/frontend-testing
bit.ly/frontend-testingsrc
backbone-testing.com

Contenu connexe

Similaire à Learn Frontend Testing

Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Pat Cito
 
Creating windows store java script apps
Creating windows store java script appsCreating windows store java script apps
Creating windows store java script apps
Eugene Zharkov
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
Tony Fabeen
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
Michael Peacock
 

Similaire à Learn Frontend Testing (20)

Angular.js + Rails at WeWork or: The Accidental Feature
Angular.js + Rails at WeWork or: The Accidental FeatureAngular.js + Rails at WeWork or: The Accidental Feature
Angular.js + Rails at WeWork or: The Accidental Feature
 
Building modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and javaBuilding modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and java
 
A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
An Introduction to CSS Preprocessors
An Introduction to CSS PreprocessorsAn Introduction to CSS Preprocessors
An Introduction to CSS Preprocessors
 
PhpUnit Best Practices
PhpUnit Best PracticesPhpUnit Best Practices
PhpUnit Best Practices
 
Hazelcast - In-Memory DataGrid
Hazelcast - In-Memory DataGridHazelcast - In-Memory DataGrid
Hazelcast - In-Memory DataGrid
 
Beginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleBeginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at Google
 
One page app with AngularJS
One page app with AngularJSOne page app with AngularJS
One page app with AngularJS
 
Theme Development and Customization
Theme Development and CustomizationTheme Development and Customization
Theme Development and Customization
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit Testing
 
Starting out with Ember.js
Starting out with Ember.jsStarting out with Ember.js
Starting out with Ember.js
 
Scalaxb preso
Scalaxb presoScalaxb preso
Scalaxb preso
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
 
Creating windows store java script apps
Creating windows store java script appsCreating windows store java script apps
Creating windows store java script apps
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Learn Frontend Testing

  • 1. LEARN FRONTEND TESTING SEATTLE JS - OCT. 16TH, 2013 Ryan Roemer | @ryan_roemer @FormidableLabs
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. MENTORS Try to keep pace with the presentation, but side conversations are encouraged and don't let us stop any good directions. And, thanks!
  • 8. MOTIVATION Web applications are increasingly becoming frontend heavy. We need to verify app logic and behavior, and that means braving the browser.
  • 9. SO LET'S TEST Backend is straightforward and easy ... but what about the frontend?
  • 10. FRONTEND TESTING Frontend testing is difficult and errorprone. Asynchronous events, timing Browser idiosyncracies State of testing technologies
  • 12. ... so let's get started with a modern frontend test stack.
  • 14. OVERVIEW Installation and test page Suites Assertions Fakes Automation
  • 15. WE WILL LEARN HOW TO Hook frontend JS to tests Write assertions against behavior Fake application behavior Run and verify the tests
  • 16. THINGS WE'RE NOT GOING TO COVER TDD / BDD Application development Functional testing Performance testing
  • 17. CODING TIME We're going to say hello: "SeattleJS" ➞ "Hello SeattleJS!" And camel case strings: "fun-test-time" ➞ "funTestTime"
  • 18. SET UP YOUR PROJECT #Cp teseeo apiain oy h kltn plcto. $c - seeo M_P_AE p r kltn YAPNM
  • 19. PROJECT STRUCTURE Using with the "skeleton" application. M_P_AE YAPNM/ j/ s ap p/ hloj el.s cmlj ae.s lb i/ ca.s hij mcaj oh.s mcacs oh.s snnj io.s idxhm ne.tl
  • 20. HELLO! skeleton/js/app/hello.js / Hlo[AU] / el VLE! vrhlo=fnto (a){ a el ucin vl rtr "el "+vl+"" eun Hlo a !; } ;
  • 21. CAMEL CASE skeleton/js/app/camel.js / Cmlcs asrn. / ae ae tig vrcml=fnto (a){ a ae ucin vl / Upraetefrtcaatratrads / pecs h is hrce fe a rtr vlrpae/()g fnto (,fr eun a.elc(-./, ucin m i rtr frttUprae) eun is.opeCs(; }; ) } ;
  • 24. TEST LIBRARIES Mocha (v . 3 0 Framework 1 1 . ): Chai (v . . ): Assertions 173 Sinon.JS (v . . ): Fakes - spies and stubs 181
  • 25. DIRECTORY LAYOUT A combined structure. M_P_AE YAPNM/ j/ s ap p/ lb i/ se/ pc hlose.s el.pcj *se.s .pcj ts.tl ethm idxhm ne.tl
  • 26. THE TEST PAGE Create a test "driver" web page. example/test.html $tuhM_P_AEts.tl oc YAPNM/ethm
  • 27. TEST.HTML <tl hm> <ed ha> <il>rnedTsig/il> tteFotn etn<tte <- Apiainlbais -> !- plcto irre. <citsc"saphloj"<srp> srp r=j/p/el.s>/cit <citsc"sapcmlj"<srp> srp r=j/p/ae.s>/cit <- Ts sye adlbais -> !- et tls n irre. <ikrl"tlset ln e=syehe" he=j/i/oh.s"/ rf"slbmcacs > <citsc"slbmcaj"<srp> srp r=j/i/oh.s>/cit <citsc"slbca.s>/cit srp r=j/i/hij"<srp> <citsc"slbsnnj"<srp> srp r=j/i/io.s>/cit
  • 28. TEST.HTML <- Ts Stp-> !- et eu <cit srp> / Stu Ca adMca / e p hi n oh. wno.xet=ca.xet idwepc hiepc; mcastp"d"; oh.eu(bd) / Rntsso wno la. / u et n idw od wno.nod=fnto ( { idwola ucin ) mcarn) oh.u(; <srp> /cit
  • 29. TEST.HTML <- Tss -> !- et. <- ..se srp icue g hr .. !- . pc cit nlds o ee . <ha> /ed <oy bd> <i i=mca>/i> dv d"oh"<dv <bd> /oy <hm> /tl example/test-empty.html
  • 30. MOCHA SUITES, SPECS Spec: A test. Suite: A collection of specs or suites.
  • 31. SUITES, SPECS test-mocha.html | mocha-suite.spec.js dsrb(snl lvl,fnto ( { ecie"ige ee" ucin ) i(sol ts smtig) t"hud et oehn"; }; ) dsrb(tplvl,fnto ( { ecie"o-ee" ucin ) dsrb(nse" fnto ( { ecie"etd, ucin ) i(i so adayc,fnto (oe { t"s lw n sn" ucin dn) stieu(ucin( {dn(;} 30 eTmotfnto ) oe) , 0) }; ) }; ) }; )
  • 32. SETUP, TEARDOWN test-mocha.html | mochasetup.spec.js dsrb(stptadw" fnto ( { ecie"eu/eron, ucin ) bfr(ucin(oe {dn(;}; eoefnto dn) oe) ) bfrEc(ucin( {) eoeahfnto ) }; atrfnto (oe {dn(;}; fe(ucin dn) oe) ) atrahfnto ( {) feEc(ucin ) }; i(sol ts smtig) t"hud et oehn"; }; )
  • 33. CHAI ASSERTIONS Natural language syntax. Chained assertions.
  • 34. CHAI API The "bdd" API: Chains: t , b , b e , h v o e en ae Groups: a d n Basics: ae u ll n t , m t h , qa, egh ac
  • 35. HELLO! test-hello.html | hello.spec.js dsrb(hlo,fnto ( { ecie"el" ucin ) i(sol syhlo,fnto ( { t"hud a el" ucin ) epc(el(Wrd) xethlo"ol") .ob.(srn".n t.ea"tig)ad .oeul"el Wrd".n t.qa(Hlo ol!)ad .ohv.egh1)ad t.aelnt(2.n .omth/el{}) t.ac(H[]2/; }; ) }; )
  • 36. CAMEL CASE test-camel.html | camel.spec.js dsrb(cml,fnto ( { ecie"ae" ucin ) i(hnlsbs css,fnto ( { t"ade ae ae" ucin ) epc(ae(")t.qa("; xetcml").oeul") epc(ae(snl")t.qa(snl" xetcml"ige).oeul"ige) }; ) i(hnlsdse css,fnto ( { t"ade ahd ae" ucin ) epc(ae(abc).oeul"B"; xetcml"--")t.qa(aC) epc(ae(oeto).oeul"nTo xetcml"n-w")t.qa(oew" }; ) }; )
  • 37. MORE CHAI test-chai.html | chai.spec.js | chaifail.spec.js dsrb(ca" fnto ( { ecie"hi, ucin ) i(asrs,fnto ( { t"set" ucin ) epc("n" "w")t.oti(to) xet[oe, to].ocnan"w"; epc(fo {a:1}) xet{o: br 2} .ohv.eppoet(fobr,1) t.aede.rpry"o.a" 2; }; ) }; ) dsrb(ca" fnto ( { ecie"hi, ucin ) i(fis,fnto ( { t"al" ucin ) epc(oe)t.qa(to) xet"n".oeul"w"; }; )
  • 38. SINON.JS FAKES Dependencies, complexities? Fake it! Spies: Observe function behavior. Stubs: Spies that replace behavior. Fake Server: Override $ a a , etc. .jx
  • 39. SINON.JS SPY test-sinon.html | camel-spy.spec.js | camel.js dsrb(cml,fnto ( { ecie"ae" ucin ) i(sisuprcs" fnto ( { t"pe pe ae, ucin ) vrsy=snnsySrn.rttp,"o a p io.p(tigpooye tU epc(p.alon)t.qa() xetsyclCut.oeul0; epc(ae(ab).oeul"B) xetcml"-")t.qa(a"; epc(p.alon)t.qa() xetsyclCut.oeul1; epc(p.isCl.eunau)t.qa xetsyfrtalrtrVle.oeul syrsoe) p.etr(; }; )
  • 40. SINON.JS STUB test-sinon.html | camel-stub.spec.js | camel.js dsrb(cml,fnto ( { ecie"ae" ucin ) i(susuprcs" fnto ( { t"tb pe ae, ucin ) vrsu =snnsu(tigpooye " a tb io.tbSrn.rttp, t fnto ( {rtr "O" }; ucin ) eun FO; ) epc(ae(ab).oeul"FO) xetcml"-")t.qa(aO"; epc(tbclCut.oeul1; xetsu.alon)t.qa() su.etr(; tbrsoe) }; ) }; )
  • 41. AUTOMATION Drive our frontend tests with PhantomJS using Mocha-PhantomJS
  • 42. PREP TEST.HTML Update the test.html file: wno.nod=fnto ( { idwola ucin ) (idwmcahnoJ | mca.u(; wno.ohPatmS | oh)rn) } ;
  • 43. HEADLESS! Install and drive tests from the command line: $nmisalmcapatms p ntl oh-hnoj $nd_oue/bnmcapatms oemdls.i/oh-hnoj M_P_AEts.tl YAPNM/ethm
  • 44. ... and that's all for now!
  • 45. WHAT WE'VE COVERED Test harness Suites, specs Assertions Fakes Automation
  • 46. ADDITIONAL TOPICS Advanced testing: DOM, fixtures TDD / BDD Functional testing Performance testing Continuous Integration: (Travis CI)
  • 47. THANKS! Ryan Roemer | @ryan_roemer bit.ly/frontend-testing bit.ly/frontend-testingsrc backbone-testing.com