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)$":"
延伸文章資訊
- 1Stencil Test - OpenGL Wiki
The Stencil Test is a per-sample operation performed after the Fragment Shader. The fragment's st...
- 2Stencil testing - LearnOpenGL
Stencil testing has many more purposes (beside outlining objects) like drawing textures inside a ...
- 3The Basics of Unit Testing in StencilJS | Elite Ionic
A unit test is a chunk of code that is written and then executed to test that a single "unit" of ...
- 4Examples of how to test Stencil components - GitHub
Examples of how to test Stencil components. Contribute to jagreehal/stencil-how-to-test-component...
- 5Unit Testing - Stencil.js
Stencil makes it easy to unit test components and app utility functions using Jest. Unit tests va...