测试对于框架来说比较重要,对于web 组件的测试同样很重要,类似的jest 很方便,stenciljs也是基于jest 开发的
包含两个核心api render(), flush()

测试配置

package.json 配置

  1. "devDependencies": {
  2. ...
  3. "@types/jest": "^21.1.1",
  4. "jest": "^21.2.1"
  5. },

npm script 配置

  1. "scripts": {
  2. ...
  3. "test": "jest --no-cache",
  4. "test.watch": "jest --watch --no-cache"
  5. },

组件渲染测试

主要函数

  • render({components:[],html:string} ) 进行组件列表的渲染,html 是html 的代码片段,包含组件的使用
  • flush(element) 用来在属性变更之后刷新元素

渲染组件

  • components 组件列表
  • html html 片段
    参考
  1. beforeEach(async () => {
  2. element = await render({
  3. components: [MyName],
  4. html: '<my-name></my-name>'
  5. });
  6. });

刷新组件

flush 函数

  1. it('should work with both the first and the last name', async () => {
  2. element.first = 'Peter'
  3. element.last = 'Parker';
  4. await flush(element);
  5. expect(element.textContent).toEqual('Hello, my name is Peter Parker');
  6. });

组件测试

参考

  1. it('should least each part of the name breaking on spaces', async () => {
  2. element.first = 'Pasta Primavera';
  3. element.last = 'O Shea Buttersworth';
  4. await flush(element);
  5. const list = element.querySelector('ul');
  6. expect(list.children.length).toEqual(5);
  7. expect(list.children[0].textContent).toEqual('Pasta');
  8. expect(list.children[1].textContent).toEqual('Primavera');
  9. expect(list.children[2].textContent).toEqual('O');
  10. expect(list.children[3].textContent).toEqual('Shea');
  11. expect(list.children[4].textContent).toEqual('Buttersworth');
  12. });
  13. 组件方法测试
  14. it('should return an empty string if there is no first or last name', () => {
  15. const myName = new MyName();
  16. expect(myName.formatted()).toEqual('');
  17. });

参考资料

https://stenciljs.com/docs/unit-testing

 
 
 
 

stenciljs 学习八 组件测试的更多相关文章

  1. stenciljs 学习六 组件开发样式指南

    组件不是动作,最好使用名词而不是动词, 文件结构 每个文件一个组件. 每个目录一个组件.虽然将类似的组件分组到同一目录中可能是有意义的,但我们发现当每个组件都有自己的目录时,更容易记录组件. 实现(. ...

  2. stenciljs 学习四 组件装饰器

    stenciljs 可以方便的构建交互式组件 支持以下装饰器 component prop watch state method element component 说明 component 包含ta ...

  3. stenciljs 学习三 组件生命周期

    stenciljs 组件包含好多生命周期方法, will did load update unload 实现生命周期的方法比价简单类似 componentWillLoad ....,使用typescr ...

  4. Android Testing学习01 介绍 测试测什么 测试的类型

    Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...

  5. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  6. surging如何使用swagger 组件测试业务模块

    1.前言 微服务架构概念的提出已经有非常长一段时间了,但在近期几年却开始频繁地出现,大家都着手升级成微服务架构,使用着各种技术,大家认为框架有服务治理就是微服务,实现单一协议的服务调用,微服务虽然没有 ...

  7. Shell学习之条件测试(四)

    Shell学习之条件测试 目录 逻辑测试 文件测试 数值比较 字符串比较 逻辑测试 格式: [ 表达式 ] 操作符 [ 表达式2 ] …… 命令1 操作符 命令2 …… 常用的操作符 ( 注意:-a和 ...

  8. SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  9. Hadoop YARN学习之组件功能简述(3)

    Hadoop YARN学习之组件功能简述(3) 1. YARN的三大组件功能简述: ResourceManager(RM)是集群的资源的仲裁者, 它有两部分:一个可插拔的调度器和一个Applicati ...

随机推荐

  1. 【异常】Application failed to start due to an exception org.springframework.beans.factory.BeanCurrentlyInCreationException

    一. 异常信息:   2018-05-17 18:03:22.224 -DEBUG [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   ...

  2. 一个登陆浏览api接口; 其他相关: Form_with参数的不同写法; 简单使用curl。

    eeting-up app: 完成一个需求: 完成:https://github.com/chentianwei411/meeting-up-app 第四步实现API接口 Add api base a ...

  3. 4-3 atom订阅源

    部分章节还没有实做练习. 网上购买了安道的Rails指南中文版.联系了这个作者问了一个问题Rails5的翻译问题. try(), 判断是否存在,不存在的话返回nil.例子:pasting @perso ...

  4. After reading a picture than out a picture

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  5. CSS——标准盒子模型

    在写网页的时候一般都先用Div把网页的框架搭好(用不同的背景颜色来区分不同的Div块),然后填充每一个Div,最后把每个Div的背景颜色去掉 <html> <head> < ...

  6. 62. 63. Unique Paths 64. Minimum Path Sum

    1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  7. JS获取昨天/今天/今年第一天的方法

    talk is cheap,show me the code! //获取昨天 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1 ...

  8. 改变进程的优先级,nice,getpriority,setpriority

    int getpriority(int which, int who);返回一组进程的优先级 参数which和who确定返回哪一组进程的优先级 The value which is one of PR ...

  9. winRAR显示树树目录

    这样 比较方便

  10. vs2015如何设置类或函数前不显示引用的数量

    这几天,从vs2012换成vs2015,感觉15版本增加了一个类或函数前提示引用的数量,这个感觉很别扭,如何取消显示这个呢? 问题如下: 取消显示这个引用的步骤: 找到菜单栏: 工具 ---> ...