javascript - Web组件快照测试

标签 javascript web components jestjs

给定这个自定义组件:

const style = `
  div {
    color: red;
  }
`;

const html = `
  <div>
    SAMPLE WEB COMPONENT
  </div>
`;

const $element = document.createElement('template');
$element.innerHTML = `
  <style>
    ${style}
  </style>
  ${html}
`;

export default class Sample extends HTMLElement {
  constructor() {
    super();
    this.root = this.attachShadow({ mode: 'closed' });
    this.root.appendChild($element.content.cloneNode(true));
  }
}

是否有可能使用 Jest 进行快照测试?使用像 jest-snapshot-serializer-raw 或 jsdom + serializer 或其他东西。

最佳答案

我有同样的问题,所要做的就是以序列化方式 (innerHTML) 获取 WebComponent 并检查快照:

it('should render correctly', () => {
    const myComponent = new MyComponent();

    expect(myComponent.innerHTML).toMatchSnapshot();
});

关于javascript - Web组件快照测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770409/

相关文章:

java - 将 Web 应用程序移动到 Web 托管/Web 服务器

AngularJS - 检查组件的绑定(bind)函数是否是一个函数

浏览器子窗口中的 Angular 组件

javascript - 应该使用多websocket连接来实现并行通信吗?

Firefox 中的 php jquery 错误

php - TinyMCE/Tinybrowser 上传选项卡给出 404

javascript - msie 奇怪的 div 格式(以及其他问题)我做错了什么?

security - 生产服务器上的 TLS 证书

javascript - 如何从Jquery正确调用JS

Angular 2 至 4/5/6 : What are some things to know/do/check before upgrading?