Unit Testing Stencil Components - Ionic Blog
文章推薦指數: 80 %
We have added a unit testing framework to Stencil. Stencil is a tool for building modern Web Components. Stencil combines some of the best ...
Appflow
GettingStarted
Docs
Platform
Enterprise
Studio
Appflow
Pricing
Resources
Documentation
ResourceCenter
Community
Blog
Solutions
OfflineStorage
BiometricSignin
UserAuthentication
GetHelp
Support
Advisory
Training
Login
Signup
Products
GettingStarted
Documentation
Login
Signup
Platform
Products
Framework
Studio
Appflow
Native
Solutions
Integrations
OfflineStorage
BiometricSignIn
UserAuthentication
ProgressiveWebApps
CrossPlatformApps
GetHelp
Support
Advisory
Training
Developers
Docs
Installation
UIComponents
Native
CLI
AppflowDocs
Quickstart
NativeBuilds
LiveUpdating
Automation
OpenSource
Framework
Stencil
Capacitor
Resources
ResourceCenter
Articles&Whitepapers
CaseStudies
Webinars
Videos
Podcast
CommunityHub
Forum
Slack
Twitter
YouTube
GitHub
Courses
EliteIonic
IonicAcademy
IonicCLI
PWAswithIonic
Seeall...
FantasticMobileDevTools,andWheretoFindThem!…
Pricing
Enterprise
Blog
/UnitTestingStencilComponents
GettheNewsletter
Togglenavigation
Share
UnitTestingStencilComponents
By
Ken Sodemann
on
November17,2017
in
testingTools
WehaveaddedaunittestingframeworktoStencil.StencilisatoolforbuildingmodernWebComponents.Stencilcombinessomeofthebestfeaturesfromtraditionalframeworks,butoutputs100%standards-compliantCustomElements,partoftheWebComponentspec.
Thegoalswehadwhenaddingthetestingframeworkweresimple:makeiteasytounderstand,andkeeptheAPIassmallaspossible.Asaresultofthis,thetestingAPIhasatotaloftwomethods:render()andflush().
render()–TherendermethodtakesanobjectcontainingalistofcomponentsthattheStencilcompilerneedstoknowaboutandanHTMLstring.Itreturnstherenderedelement.
flush()–Theflushmethodtakesanelementandcausesittorerender.Thisistypicallydoneafterpropertiesontheelementarechanged.
GettingStarted
InordertogetstartedwithunittestingStencilcomponents,clonetheStencilComponentStarterandrunnpminstall.Toruntheunittests,useoneofthefollowingcommands:
npmtest–performsasinglerunoftheunittests
npmruntest.watch–runstheunittestsandwatchesforchangestothesource,rerunningtheunittestwitheachsavedchange
Ineithercase,Jestisusedtoruntheunittests.
ApplicationSetup
Inordertoperformunittestsonthecomponentsintheaproject,someminimalsetupisrequiredintheproject’spackage.jsonfile.Thetestingpackagesneedtobeinstalledasdevelopmentdependencies,thenpmscriptsthatrunthetestsneedtobedefined,andJestneedstobeconfiguredtocompilethesourcesandthetestsproperly.
Theonlypackagesrequiredinordertorunthetestsarejestand@types/jest:
"devDependencies":{
...
"@types/jest":"^21.1.1",
"jest":"^21.2.1"
},
Thetwotestingrelatednpmscriptsaretestandtest.watch.
"scripts":{
...
"test":"jest--no-cache",
"test.watch":"jest--watch--no-cache"
},
ThejestsetupsectionspecifiesthemoduleFileExtensions,testRegex,andtransform.TheonlypartofthesetupthatisspecifictoStencilisthetransformsection.StencilprovidesapreprocessorscriptthatusestheStencilcompilertobuildthesourceandtestfiles.
"jest":{
"transform":{
"^.+\\.(ts|tsx)$":"
延伸文章資訊
- 1Examples of how to test Stencil components - GitHub
Examples of how to test Stencil components. Contribute to jagreehal/stencil-how-to-test-component...
- 2Stencil buffer - Wikipedia
Stencil test
- 3Stencil Test - OpenGL Wiki
The Stencil Test is a per-sample operation performed after the Fragment Shader. The fragment's st...
- 4Stencil testing - LearnOpenGL
Stencil testing has many more purposes (beside outlining objects) like drawing textures inside a ...
- 5Creating Unit Tests for StencilJS Components - YouTube