Web 组件/HtmlElement:单元测试

Web Component / HtmlElement : unit testing

我正在尝试测试网络组件。 这是我的项目结构:

├── package.json
├── src
│   ├── app.js
│   └── index.html
└── test
    └── hello-world-test.html

这是我的工作代码:

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>

我正在尝试使用 web-component-tester 测试该 Web 组件。 我在全球范围内安装了该实用程序:

npm install -g web-component-tester

我在 package.json 文件中声明了它:

"devDependencies": {
    "web-component-tester": "^6.9.0"
}

然后,我把我的测试写在test文件夹里,并保存到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>

最后,我输入了:

wct --npm

然后在Chrome中得到如下错误:

我错过了什么 运行 正确的测试? 我找到的唯一像样的材料是 this one and that one,但它们已经过时了。

有很多错误:

  • 首先,请阅读最后一段中的 whole 文档,很明显,对于那些使用 npm 的人,您需要通过 wctPackageName :

Components which wish to support npm-based installation should include wct-browser-legacy in their devDependencies, which is a package that contains only the client-side javascript necessary for executing WCT tests in an npm-based environment. WCT will attempt to identify which package provides the client-side code by checking for compatible packages in the following order of preference: wct-mocha, wct-browser-legacy and web-component-tester. If you want to specify which package provides WCT client-side code, use the --wct-package-name flag or wctPackageName option in wct.conf.json with the npm package name.

因此您需要在 devDependencies

中添加 wct-browser-legacy
  • 根据您的项目结构,您将 app.js 包含在同一级别中。应该是 ../src/app.js.
  • 您应该将 type="module" 添加到该导入
  • 您声明了一个夹具但没有通过函数从中获利 fixture

如果我必须更正您的代码:

  • 命令应该是wct --npm -wct-package-name=wct-browser-legacy。或者甚至更好地创建一个包含以下信息的 wct.conf.js 文件:

module.exports = {
    npm:true,
    verbose: true,
    plugins: {
        local: {
            browsers: ["chrome"]
        }
    },
    wctPackageName: "wct-browser-legacy"
};

  • 您的测试应修改如下:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script src="../node_modules/web-component-tester/browser.js"></script>
    <script src="../src/app.js"></script>
</head>
<body>
    <test-fixture id="helloWorldFixture">
        <template>
            <hello-world>
            </hello-world>
        </template>
    </test-fixture>
    <script>
        suite('<hello-world>', () => {
            let component;
            setup(() => {
                component = fixture('helloWorldFixture');
            });

            test('contains hello world string ?', () => {
                let index = component.innerText.indexOf('Hello');
                assert.isAtLeast(index, 0);
            });
        });
    </script>
</body>
</html>

请注意,我使用了夹具的 ID 并将组件初始化放在 setup 函数中。

Zakaria 的回答很好,但我建议放弃 wct-browser-legacy 转而使用 wct-mocha,因为它更轻量并且没有像旧版本的 lodash 和 sinon 等过时的依赖项.

有关完整详细信息,请参阅自述文件:https://www.npmjs.com/package/wct-mocha

tl;dr 版本:

$ npm rm --save wct-browser-legacy
$ npm install --save-dev \
  @webcomponents/webcomponentsjs \
  @polymer/test-fixture \
  wct-mocha \
  mocha \
  chai

您不应该需要指定它,但是如果您有wct.conf.js文件,您应该将现有的 wctPackageName 条目更改为:

wctPackageName: "wct-mocha"

你的 HTML 需要稍微改变一下,你需要确保 mocha 是一个直接依赖项,因为 wct-mocha 不会自动加载。如果你正在使用 chai 断言,你还需要使用 chai 来做到这一点,如果你使用这些断言,则 @polymer/test-fixture

<head>
  <meta charset="utf-8">
  <script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script> 
  <script src="../node_modules/mocha/mocha.js"></script> 
  <script src="../node_modules/chai/chai.js"></script> 
  <script src="../node_modules/@polymer/test-fixture/test-fixture.js"></script> 
  <script src="../node_modules/wct-mocha/wct-mocha.js"></script> 
</head>