I'm trying to test a web component. Here is my project structure :
├── package.json
├── src
│ ├── app.js
│ └── index.html
└── test
└── hello-world-test.html
Here is my working code :
class HelloWorld extends HTMLElement {
connectedCallback () {
this.innerHTML = 'Hello, World!'
}
}
customElements.define('hello-world', HelloWorld);
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="app.js"></script>
</head>
<body>
<hello-world></hello-world>
</body>
</html>
I'm trying to test that web component with web-component-tester
.
I installed the utility globally :
npm install -g web-component-tester
I declared it in the package.json
file :
"devDependencies": {
"web-component-tester": "^6.9.0"
}
then, I wrote my test in the test
folder and saved it to hello-world-test.html
:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="../node_modules/web-component-tester/browser.js"></script>
<script src="app.js"></script>
</head>
<body>
<test-fixture id="hello-world-fixture">
<hello-world></hello-world>
</test-fixture>
<script>
suite('<hello-world>', function(){
let component = document.querySelector('hello-world');
test('contains hello world string ?', () => {
let index = component.innerText.indexOf('Hello');
assert.isAtLeast(index, 0);
});
});
</script>
</body>
</html>
Finally, I typed :
wct --npm
Then obtained the following error in Chrome :
What am I missing to run the test correctly ? The only decent materials I've found are this one and that one but they are outdated.
There are many errors :
npm
you need an additional dependency through thewctPackageName
:So you will need to add
wct-browser-legacy
in yourdevDependencies
app.js
as if it was at the same level. It should be../src/app.js
.type="module"
to that importfixture
If I had to correct your code :
wct --npm -wct-package-name=wct-browser-legacy
. Or even better create awct.conf.js
file with the following information :Please, notice that I used the fixture's id and put the component initialisation in the
setup
function.