2019-05-01+Web+Designer+UK

(Brent) #1

lassicJavaScripttestingframeworks
sawthelightina differenttimeand
age:AddyOsmanihadbarelystarted
workonhiscollectionofobject-
orientedprogrammingpatterns,whilelarge
frameworkssuchasAngularorReactwere
unimaginable.Backthen,‘simple’case-based
comparativeenginesworkedwellenough—
checkingtheoutputofa computationalengineora
bitofgluecodedoesnotrequiremuchthinking.
TheJestdeveloperteamis a strictadherentto
theideaofzeroconfiguration.Thismakesthe
frameworkeasytobringupaslongasa package
managerlikeNPMorYARNis available.Another
aspectis thewide-rangingextensibility,whichmakes
addingnewfunctionalitytotheframeworka breeze.
Finally,Jestintegratesitselfintovariousversion
controlsystemslikeGitandMercurial.Thisprocessis
beneficialinthatit providesJestwithanoverviewof
commitsandeliminatesunneededtestruns.
Surprisingly,allofthisdoesnotcomeata
significantpriceintermsofperformance.
Comparativeevaluationsattesttheproducttohave
market-leadingperformance— thetimesoflimiting
unittestscopeduetoCPUconstraintsshouldbe
overforJestusers.


1 .InstallYARN
WhileJestcanalsoliveinsidea NPMenvironment,all
ofthedocumentationis basedonYARN.Fortunately,
addingtheproductis easyonUnixoidoperating
systems— thedeveloperteamprovidesa well-
maintainedpackagerepository.
Userswitha non-Ubuntuoperatingsystemshould
lookattheinstallationpageshowninthefigureand
foundatYARNpkg.com/lang/en/docs/
install/#debian-stable.


tamhan@tamhan-thinkpad:~$ curl -sS
dl.YARNpkg.com/debian/pubkey.gpg | sudo
apt-key add -
tamhan@tamhan-thinkpad:~$ echo'deb
dl.YARNpkg.com/debian/ stablemain'| sudo
tee /etc/apt/sources.list.d/YARN.list
tamhan@tamhan-thinkpad:~$ sudoapt-get update
&& sudo apt-get installYARN

2 .Createa project
YARNandNPMbehavesimilarlywhenpackage
managementis concerned.Inbothcases,a new


projectmustbegenerated— afterthat,the
Jest-relatedcomponentscanbeadded.Simply
followthestepsoutlined— thedefaultvalues
providedbythewizardareacceptable.

tamhan@TAMHAN14:~/YARNspace$ YARNinit
YARN initv1.13.0
question name(YARNspace): Jesttest
question version (1.0.0):

...
success Savedpackage.json
Done in 7.82s.
tamhan@TAMHAN14:~/YARNspace$ YARNadd --dev
Jest
YARN add v1.13.0
...
ٽڃ [email protected]
Done in 8.61s.


3 .Firsttest!
WhileJestcanberunfromthecommandline,opt
forbuildflowintegration.YARN-basedprojectsoffer
anoptionthatcontainsa package.jsonfile,whichis
tobemodifiedasoutlinedinthecodesnippet
accompanyingthisstep.

C


ModernUnix, beware
VeryrecentversionsofUbuntu(around17.xx)
faceissueswheninstallingYARN.Further
informationandadviceonmitigationcanbe
foundbyexaminingtheissueatgithub.com/
YA R N p kg / YA R N / i s s u e s / 2 8 2 1.

Tutorials


{

...
'devDependencies': {
'Jest': '^24.1.0'
},
'scripts': {
'test': 'Jest'
}
}


4 .Performa dryrun
Atthispointintime,YARNis readytorun.Enter
YARNtesttoinvoketheelementaddedtothescript
tagabove,andfeastyourselfontheerrorshownin
thefigure.Jestfindstestfilesbyanalysingthefile
names— if nomatchingfileis found,testcase
executionterminateswithanerror.

5 .Writesomelogic...
Writingunittests— logically— requirescodetobe
tested.Createtwosimplemathematicalfunctions
anda stringreturner,whicharetobeplacedinthe
filetam.jsandlooklikethecodewiththisstep.

function sayHello()
{
return 'Hello';
}
function add(a,b)

4

1

tutorial _________________________________________________ 79
Free download pdf