<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vincent Ogloblinsky</title>
    <description>The latest articles on DEV Community by Vincent Ogloblinsky (@vogloblinsky).</description>
    <link>https://dev.to/vogloblinsky</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F136799%2Ffe8124f8-e3ac-4ac8-b2d5-5de6b1fdb445.jpg</url>
      <title>DEV Community: Vincent Ogloblinsky</title>
      <link>https://dev.to/vogloblinsky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vogloblinsky"/>
    <language>en</language>
    <item>
      <title>Cross Browser Testing your Stencil Web Component like a boss</title>
      <dc:creator>Vincent Ogloblinsky</dc:creator>
      <pubDate>Fri, 15 Feb 2019 15:31:01 +0000</pubDate>
      <link>https://dev.to/vogloblinsky/cross-browser-testing-your-stencil-web-component-like-a-boss-2n4c</link>
      <guid>https://dev.to/vogloblinsky/cross-browser-testing-your-stencil-web-component-like-a-boss-2n4c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oDdcEYCy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A67fHwt9WyjJTRy5c" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oDdcEYCy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2A67fHwt9WyjJTRy5c" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@christianfregnan?utm_source=medium&amp;amp;utm_medium=referral"&gt;Christian Fregnan&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stenciljs.com/"&gt;Stencil&lt;/a&gt; is a really great piece of software. For those who are not familiar with it, it is a “ Compiler for Web Components ”.&lt;/p&gt;

&lt;p&gt;Stencil comes by default with tools for testing your Web Component : &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; for the testing framework, and &lt;a href="https://pptr.dev/"&gt;Puppeteer&lt;/a&gt; for “ End-to-end ”(e2e) tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pptr.dev/"&gt;Puppeteer&lt;/a&gt; is a robust solution for driving a Chrome Headless instance, but it only works with Chrome.&lt;/p&gt;

&lt;p&gt;If you want to support or test your Web Component in more browsers, you have two strategies :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;using a solution that drives a browser using &lt;a href="https://www.w3.org/TR/webdriver/"&gt;W3C WebDriver API&lt;/a&gt; : &lt;a href="https://webdriver.io"&gt;WebdriverIO&lt;/a&gt;, &lt;a href="http://nightwatchjs.org/"&gt;Nightwatch.js&lt;/a&gt; are two great solutions. The main drawback is that querying Shadow DOM is a bit tricky with these solutions, due to &lt;a href="https://github.com/SeleniumHQ/selenium/issues/5869"&gt;bad support of Shadow DOM&lt;/a&gt; in WebDriver APIs.&lt;/li&gt;
&lt;li&gt;using a solution that runs your test inside a browser : &lt;a href="https://github.com/Polymer/tools/tree/master/packages/web-component-tester"&gt;Web Component Tester&lt;/a&gt; from the Polymer team or &lt;a href="https://open-wc.org/testing/"&gt;@open-wc/testing&lt;/a&gt; are two great solutions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have tested the first strategy with no success for “all browsers” supporting Web Components (with some polyfills). IE11 was the bad guy in the story…&lt;/p&gt;

&lt;p&gt;This is the second approach I will explain in details in this blog post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZB964ZTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Aia288m2UEvaWMsSf4ZV5pQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZB964ZTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Aia288m2UEvaWMsSf4ZV5pQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://open-wc.org/"&gt;Open-wc&lt;/a&gt; is a mix of recommendations and tools for many developer’s use cases : developing, linting, testing, building, demoing, publishing. Some boilerplates are very opinionated, but for my use-case about testing, it just works ! Have a look at &lt;a href="https://open-wc.org/about/#our-philosophy"&gt;their philosophy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this tutorial, Iwill explain in details the setup for the “ Component ” boilerplate of Stencil CLI :&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;STEP 1 : generate the project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kU8cZo7j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ARYC4c5olX6QbR5ObF-XfRA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kU8cZo7j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ARYC4c5olX6QbR5ObF-XfRA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should got these folders and files structure :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--soCoFoRt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/269/1%2AVOBSu_-ql2W2t2_ZwWFzkw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--soCoFoRt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/269/1%2AVOBSu_-ql2W2t2_ZwWFzkw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 2 : install &lt;a href="https://open-wc.org/"&gt;Open-wc&lt;/a&gt; testing tools
&lt;/h3&gt;

&lt;p&gt;After installing Yeoman and &lt;a href="https://open-wc.org/"&gt;Open-wc&lt;/a&gt; generator, you can run it inside your Stencil project :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WL0_PPfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A4esGCda5rzo_Ltr0mpq3MQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WL0_PPfs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A4esGCda5rzo_Ltr0mpq3MQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;STEP 3 : edit test file&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The generated test file has to be updated to work with your Stencil web component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kc4atdaa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ALi1JZJOySjdJR1Cc5Mh5lw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kc4atdaa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ALi1JZJOySjdJR1Cc5Mh5lw.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We import the &lt;strong&gt;defineCustomElements&lt;/strong&gt; on line 6 exported by Stencil, and call it with window context on line 7 to register the component inside the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry"&gt;&lt;strong&gt;CustomElementsRegistry&lt;/strong&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We can start testing our Web component. &lt;a href="https://open-wc.org/"&gt;Open-wc&lt;/a&gt; comes with very cool helpers like &lt;strong&gt;“ fixture ”&lt;/strong&gt;. It helps you generate for each test suit an isolated component, so if each test suit is running in parallel, you don’t have side effects from one to another.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Important :&lt;/strong&gt; Before trying querying and testing with assertions your Web Component, it has to be rendered inside the DO and you have to wait a certain amount of time. Fixture helpers has internal bindings using the powerful async/await to call a public API from Stencil, &lt;a href="https://stenciljs.com/docs/methods/"&gt;&lt;strong&gt;componentOnReady()&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  STEP 4 : run your test locally
&lt;/h3&gt;

&lt;p&gt;After running &lt;strong&gt;npm install&lt;/strong&gt; to install all the devDependencies, build your Web Component with Stencil and launch the test, everything should be fine except for code coverage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tmZwBQT8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/960/1%2AyE_BUEUn_iTbUssf-i6irQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tmZwBQT8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/960/1%2AyE_BUEUn_iTbUssf-i6irQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The default code coverage configuration of thresholds of &lt;a href="https://github.com/open-wc/open-wc/blob/master/packages/testing-karma/default-settings.js#L48"&gt;open-wc/testing are at 90%&lt;/a&gt;. You can change that by editing the &lt;strong&gt;karma.conf.js&lt;/strong&gt; file, and configuring thresholds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mhfeRclo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AoDILaAebV0tXJCrKvi0OpQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mhfeRclo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AoDILaAebV0tXJCrKvi0OpQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not enough for our use-case, because &lt;a href="https://istanbul.js.org/"&gt;Istanbul&lt;/a&gt;, the code coverage tool used by Karma, calculates the coverage of all the files generated by Stencil compiler inside the &lt;strong&gt;dist&lt;/strong&gt;  folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eI8gB-Mm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1020/1%2AvR061nKlZAFOedo0zbg1WA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eI8gB-Mm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1020/1%2AvR061nKlZAFOedo0zbg1WA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;es2017&lt;/strong&gt; folder contains files used to bootstrap our Web Component, and also the Stencil runtime. Excluding these files from code coverage requires adding the following regular expressions inside &lt;strong&gt;karma.conf.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kMwFA7Y_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2At_hkNbWh3pxr0sPUwheVsA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kMwFA7Y_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2At_hkNbWh3pxr0sPUwheVsA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  STEP 5 : run your test in BrowserStack
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://open-wc.org/"&gt;Open-wc&lt;/a&gt; has a package for that, &lt;strong&gt;testing-karma-bs&lt;/strong&gt;. It was installed during step 2. Edit the &lt;strong&gt;karma.es5.bs.config.js&lt;/strong&gt; to edit the &lt;strong&gt;name&lt;/strong&gt; of your project inside &lt;strong&gt;BrowserStack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next step, you have to setup a  &lt;strong&gt;.travis.yml&lt;/strong&gt; file to configure your CI. (You can also do the same thing with &lt;a href="https://circleci.com/"&gt;CircleCI&lt;/a&gt;, &lt;a href="https://www.jetbrains.com/teamcity/"&gt;TeamCity&lt;/a&gt; or &lt;a href="https://about.gitlab.com/product/continuous-integration/"&gt;Gitlab&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3i_6dRgf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ApDrloE0SQYqDjp60teiYDA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3i_6dRgf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2ApDrloE0SQYqDjp60teiYDA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YGgCyQgA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/301/1%2AZVvyK-nAVmI_nquZKukCtg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YGgCyQgA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/301/1%2AZVvyK-nAVmI_nquZKukCtg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nest step is to add two &lt;strong&gt;Environment Variables&lt;/strong&gt; inside the setting of your Travis repository, &lt;strong&gt;BROWSER_STACK_ACCESS_KEY&lt;/strong&gt; and &lt;strong&gt;BROWSER_STACK_USERNAME&lt;/strong&gt;. You can find them at the top left corner of BrowserStack dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iD4XNfaU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A-fm8LsY_QD_0HpA2_sUspA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iD4XNfaU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A-fm8LsY_QD_0HpA2_sUspA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last step is to launch a build and wait for results :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8Eebypw---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/802/1%2AHhefYswjPCfCRjr3W9ScmQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8Eebypw---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/802/1%2AHhefYswjPCfCRjr3W9ScmQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The default list of browsers running inside &lt;strong&gt;BrowserStack&lt;/strong&gt; are configured inside default settings of &lt;strong&gt;open-wc/testing-karma-bs/bs-settings.js&lt;/strong&gt;  : latest stable version of Chrome, Firefox, Safari, Edge and IE11.&lt;/p&gt;

&lt;p&gt;You can add more browsers by adding custom launchers inside &lt;a href="https://github.com/vogloblinsky/open-wc-stencil/blob/master/karma.es5.bs.config.js"&gt;&lt;strong&gt;karma.es5.bs.config.js&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Real use-case : Nutrition Web Components
&lt;/h3&gt;

&lt;p&gt;If you want, you can also see another implementation of &lt;a href="https://open-wc.org/"&gt;open-wc&lt;/a&gt; testing tools inside a recent open-source project i created : &lt;a href="https://github.com/vogloblinsky/nutrition-web-components"&gt;Nutrition Web Components&lt;/a&gt;. It is a collection of two Web Components for displaying Nova and Nutri-score nutritional badges. I will use them inside a nutritional open-source project which will be online soon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jRM8Ccw4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/646/1%2AmJH7gUFX6YgYef7O0wrK7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jRM8Ccw4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/646/1%2AmJH7gUFX6YgYef7O0wrK7g.png" alt=""&gt;&lt;/a&gt;Nova &amp;amp; Nutri-score badges&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you !
&lt;/h3&gt;

&lt;p&gt;That’s it! I hope you enjoyed reading this post as much as I enjoyed writing it.&lt;/p&gt;

&lt;p&gt;The final source code is available on Github :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/vogloblinsky/open-wc-stencil"&gt;vogloblinsky/open-wc-stencil&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter&lt;/em&gt; &lt;a href="https://twitter.com/vogloblinsky"&gt;&lt;em&gt;@vogloblinsky&lt;/em&gt;&lt;/a&gt; &lt;em&gt;for news about Web Components.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks&lt;/em&gt; &lt;a href="https://medium.com/u/d44026c69d50"&gt;&lt;em&gt;Wassim Chegham&lt;/em&gt;&lt;/a&gt; &lt;em&gt;and&lt;/em&gt; &lt;a href="https://twitter.com/daKmoR"&gt;&lt;em&gt;Thomas Allmer&lt;/em&gt;&lt;/a&gt; &lt;em&gt;for the review.&lt;/em&gt;&lt;/p&gt;




</description>
      <category>webcomponents</category>
      <category>karma</category>
      <category>stencil</category>
      <category>stenciljs</category>
    </item>
  </channel>
</rss>
