Unit testing native Web Components
Hello!
I am Andy Desmarais
2
Web Components
DOM native components, no framework
3
Basics of Web Components
4
Attach �shadow
Define new �custom element
Extend HTMLElement
class WebComponentExample extends HTMLElement {
�
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
}
customElements.define(
'web-component-example-tag-name',
WebComponentExample);
What’s the problem?
We need DOM native functionality for testing,
but starting a browser is slow.
5
jsdom is missing some pieces
6
Invert control
Inject the window!?
7
Webpack - use externals
8
Rollup - use globals
9
ESM - Create a window.mjs
export const customElements = window.customElements;
10
WE HAVE A SEAM
Testing is now REALLY straight forward
11
Fake it ‘til you make it
const window = {
HTMLElement: class HTMLElement {
attachShadow() {
this.shadowRoot = document.createElement('div');
}
},
customElements: {
define: () => {}
}
};
Object.freeze(window);
module.exports = window;
12
Examples on github
https://github.com/terodox/dotjs2019
13