---
# System prepended metadata

title: Untitled

---

![Built With Stencil](https://img.shields.io/badge/-Built%20With%20Stencil-16161d.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE5LjIuMSwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IgoJIHZpZXdCb3g9IjAgMCA1MTIgNTEyIiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCA1MTIgNTEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI%2BCjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI%2BCgkuc3Qwe2ZpbGw6I0ZGRkZGRjt9Cjwvc3R5bGU%2BCjxwYXRoIGNsYXNzPSJzdDAiIGQ9Ik00MjQuNywzNzMuOWMwLDM3LjYtNTUuMSw2OC42LTkyLjcsNjguNkgxODAuNGMtMzcuOSwwLTkyLjctMzAuNy05Mi43LTY4LjZ2LTMuNmgzMzYuOVYzNzMuOXoiLz4KPHBhdGggY2xhc3M9InN0MCIgZD0iTTQyNC43LDI5Mi4xSDE4MC40Yy0zNy42LDAtOTIuNy0zMS05Mi43LTY4LjZ2LTMuNkgzMzJjMzcuNiwwLDkyLjcsMzEsOTIuNyw2OC42VjI5Mi4xeiIvPgo8cGF0aCBjbGFzcz0ic3QwIiBkPSJNNDI0LjcsMTQxLjdIODcuN3YtMy42YzAtMzcuNiw1NC44LTY4LjYsOTIuNy02OC42SDMzMmMzNy45LDAsOTIuNywzMC43LDkyLjcsNjguNlYxNDEuN3oiLz4KPC9zdmc%2BCg%3D%3D&colorA=16161d&style=flat-square)

To run the unit tests for the components, run:

```bash
npm test
```

Need help? Check out our docs [here](https://stenciljs.com/docs/my-first-component).

## Findings on the using testing library on stencil web components

In this repository, test-button component has been built as a stencil web component and applied testing library on button component to perform basic unit testing.

- Although stencil officially uses jest for their unit testing, there is no official support for stencil web components from the testing library.

- Another reason for not directly using the testing library is that Jest's default environment is jsdom, which is used to generate a browser-like environment within the tests. Stencil, on the other hand, develops its own environment that Jest uses instead, complete with its own mocked DOM. As a result, newSpecPage, which is imported from the stencil/testing library, must be used to generate the DOM.

- We can access document, window, and other objects via the page object, after which we can use the testing library and its API, such as getByText, fire events, and so on.

- Because stencil has a different component lifecycle than react, we can't use the testing-library to mount components, hence we have to utilise the stencil testing package exclusively for mounting and re-rendering parts. Another factor is the fact that shadow DOM works differently than other DOMS.

- Everything appears to be running smoothly with the testing library, and the unit test cases are performing as expected.

- The conclusion is that we can use the testing library for stencil and the mounting is handled by the stencil official package. Given the varying lifecycle of stencil and use of shadow dom, there are multiple ways to mount without utilising @stencil/testing. However the unit testing code will be centralised within the ecosystem by using the Testing library API.

- Disadvantage of using JSDom as part of Testing library is 

> "When Stencil was initially introduced, it came with testing batteries included. The stencil team quite rightfully chose Jest as its test runner, and opted for the JSDom that was included with Jest as a rendering engine for the components. A TestWindow utility was introduced that wrapped JSDom and initialized it with Stencil components. Sadly, this turned out not to be a smooth path.
> 
>   JSDom is mocking browser APIs in node which makes it fast and reliable, but it also means that not everything you would expect in a browser environment exists. While JSDom is constantly updated according to browser specs, it is not always catching up. A main issue was still an open one is support for web components APIs. And since stencil is a, well, web components compiler, this problem became a real hurdle in testing with JSDom."

as mentioned in https://tally-b.medium.com/unit-testing-stenciljs-1-0-c4e902a4e63c
