Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对polyfilled webcomponents自定义元素进行单元测试_Javascript_Unit Testing_Jasmine_Web Component_Custom Element - Fatal编程技术网

Javascript 如何对polyfilled webcomponents自定义元素进行单元测试

Javascript 如何对polyfilled webcomponents自定义元素进行单元测试,javascript,unit-testing,jasmine,web-component,custom-element,Javascript,Unit Testing,Jasmine,Web Component,Custom Element,我想对使用webcomponents.jspolyfill的web组件进行单元测试 我的组件是在es6+SCS中制作的,通过构建任务,我将es6转换为es5,将SCS处理为css,并将两个结果文件插入html文件中,以便在应用程序中使用具有html导入功能的组件。 以下是自定义元素声明的组件类示例: class my-component extends HTMLElement { createdCallback() {...} ... //other component methods

我想对使用
webcomponents.js
polyfill的web组件进行单元测试

我的组件是在es6+SCS中制作的,通过构建任务,我将es6转换为es5,将SCS处理为css,并将两个结果文件插入html文件中,以便在应用程序中使用具有
html导入功能的组件。
以下是自定义元素声明的组件类示例:

class my-component extends HTMLElement {
  createdCallback() {...}
  ... //other component methods

  //getter/setter
  get colors() {
    return this._color;
  }
  set colors(val) {
    this._color = val;
  }
}
现在我做了一个测试任务,可以启动
Karma
服务器,用
babel
传输UT,用
Jasmine
运行UT

我的所有测试都通过了Chrome,但在IE11中,所有访问getter/setter或方法的测试都失败了

例如:

describe...
  beforeEach(function() {
    this.component = document.createElement(COMP_NAME);
  }
  it("should return an array", function() {
    expect(this.component.colors).toEqual(jasmine.any(Array));
  });
});
此UT将在Chrome中通过,但在IE中它将失败,
预期未定义为等于

我的诊断是polyfill需要一些时间来创建组件。在我的测试中,我将在组件完全创建之前访问它的getter(这就是为什么我没有定义…) 我试图推迟考试

setTimeout(() => {
  expect(this.component.colors).to...
});
但这项工作有时是有时不是

谁能告诉我怎么解决这个问题?
作为补充说明,并非所有组件都会发生这种情况。似乎只有在创建时才有许多方法/访问器和一些逻辑要运行…

您是否尝试在beforeach函数中使用“whenDefined”

beforeEach(function(done) {
  this.component = document.createElement(COMP_NAME);
  customElements.whenDefined(COMP_NAME).then(done)
}
然后,测试将在组件升级后运行

在不使用polyfill(如chrome)的浏览器中,它会毫不延迟地升级


参见

您是否尝试在beforeach函数中使用“whenDefined”

beforeEach(function(done) {
  this.component = document.createElement(COMP_NAME);
  customElements.whenDefined(COMP_NAME).then(done)
}
然后,测试将在组件升级后运行

在不使用polyfill(如chrome)的浏览器中,它会毫不延迟地升级


请参见

您是否找到了解决方案,或者您是否有如何解决的示例?我正在与同样的情况抗争…不,没有解决方案…你有没有找到解决方案,或者你有如何做到这一点的例子?我正在与同样的情况抗争…不,没有解决方案。。。