

Unit testing tool for web components
source link: https://www.tuicool.com/articles/hit/yaUF3mQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Easy Unit-Tests your web-components
I will cover in this short article how to create automated unit-tests for your web components of any stack (i.e. Lit-Element, Slim.js, Stencil, Polymer…) in one simple utility.
Introducing showroom
Showroom was built to provide a DDD/TDD platform for web components, as an alternative to Storybook that works well with React/Vue/Angular but does not work out of the box with native components and you have to find plugins for each library, and still, it will not cover the whole API .
Setting up
First, install via npm/yarn: npm install showroom
Add .showroom
folder in your project, where the component descriptor files would be.
We have some basic folder structure, let’s start writing DDD/TDD with showroom .

Our demo countdown-component is a simple timer that has a start/stop/resume functions, receiving time as an attribute and expected to dispatch the following events: onstart, onstop, ontimeout, ontick.
I will skip the implementation part as this is not the focus of this article. It can be found here
Adding descriptor (a.k.a. “story”) file
In .showroom
folder let’s add the following file (any file name would work)

Running showroom
Now let’s run showroom: npx showroom
Browsing to http://localhost:3000
should show us the following screen:

On the left hand side we can see the list of components described in .showroom
files. Clicking on a component from the list will display it on the rendering section. Attributes & Properties can be edited in the dashboard section (bottom-left) and functions can be triggered. On the right-side of the dashboard we can see all the dispatched events.
Showroom provides functionalities focused for the custom elements API
This helps us to work on the component completely isolated from the application, and run automated tests.
Writing unit tests for our web components
Let’s add our first automated test. First we should install a test runner (I favour mocha, but any would work) and puppeteer. npm i -D mocha puppeteer
In the test folder we can add the following test file:

Let’s run the test npx mocha --exit --timeout 5000

Adding additional tests
In the following example, our automated test will focus on the triggered events when and final result after invoking the “start” function.

And the test results should be:

Showroom provides many more functionalities, focusing on the custom elements API, such as:
- Wrapping HTML
- InnerHTML (for shadow-DOM based components)
- Shadow-DOM querying inside components
- Properties (including visual object editor)
- Attributes
- Events
- Function triggering
- Extending native elements (exclusive for showroom)
- Component documentation
- And more…
Conclusion
Showroom provides a lightweight alternative that integrates with puppeteer and exposes the custom elements API out of the box, with near-zero configuration.
You can see a live demo of showroom (with multiple libraries of custom elements), though it will be better if you try it yourself .
Recommend
-
86
We do a rerun of the most important review tests on the HTC U11+. See how things turned out.
-
54
Introduction In this article, we will learn how to write (simple) unit tests for your Angular modeling blocks (service, component, async task, etc). We will be using a simple Quotes application to...
-
70
I’m a firm believer in unit testing. I’ve done more tech talks on the subject than I’d care to count, and always tell audiences the same thing: prefer unit tests, here’s
-
46
Unit testing is a great discipline which can lead to 40%-80% reductions in production bug density . Unit testing also has several other important benefits:
-
12
Background I posted a previous article (about building an analytics tool) onto the Hacker News forum. It was quickly buried and didn’t get any attention.
-
13
If you write code, write tests. – The Way of Testivus 目录 Table of Contents 背景 单元测试的重要性无需多言,但...
-
0
Testing HttpRequest.Form Variables in Unit Tests for ASP.NET...
-
7
Differences Between Unit Tests and Integration Tests In React Components
-
3
How to Perform Unit Tests for UI Components
-
7
Unit-testing a web service in RustJuly 7, 2023 · About 19 minutes · Tags: endbasic, endtracker,
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK