zoukankan      html  css  js  c++  java
  • Jest | expect常用匹配方法

    来自:https://juejin.im/post/5ba8b6256fb9a05cd7774432

    -------------------------------------------------------------------

    expect(sum(1, 1)) 返回一个“期望”对象,.toBe(2) 是匹配器。匹配器将 expect() 的结果(实际值)与自己的参数(期望值)进行比较。当 Jest 运行时,它会跟踪所有失败的匹配器,并打印出错误信息。

    toBe 使用 Object.is 判断是否严格相等。
    toEqual 递归检查对象或数组的每个字段。
    toBeNull 只匹配 null。
    toBeUndefined 只匹配 undefined。
    toBeDefined 只匹配非 undefined。
    toBeTruthy 只匹配真。
    toBeFalsy 只匹配假。
    toBeGreaterThan 实际值大于期望。
    toBeGreaterThanOrEqual 实际值大于或等于期望值
    toBeLessThan 实际值小于期望值。
    toBeLessThanOrEqual 实际值小于或等于期望值。
    toBeCloseTo 比较浮点数的值,避免误差。
    toMatch 正则匹配。
    toContain 判断数组中是否包含指定项。
    .toHaveProperty(keyPath, value) 判断对象中是否包含指定属性。
    toThrow 判断是否抛出指定的异常。
    toBeInstanceOf 判断对象是否是某个类的实例,底层使用 instanceof。

    所有的匹配器都可以使用 .not 取反:

    test('Adding 1 + 1 does not equal 3', () => {
      expect(1 + 1).not.toBe(3)
    })

    对于 Promise 对象,我们可以使用 .resolves 和 .rejects

    // .resolves
    test('resolves to lemon', () => {
      // make sure to add a return statement
      return expect(Promise.resolve('lemon')).resolves.toBe('lemon')
    })
    
    // .rejects
    test('rejects to octopus', () => {
      // make sure to add a return statement
      return expect(Promise.reject(new Error('octopus'))).rejects.toThrow(
        'octopus',
      )
    })
  • 相关阅读:
    Hibernate个人学习笔记(1)
    wangzhi
    星星评分
    关于jquery中jQuery slideToggle() 方法实现的原理
    css盒模型和块级、行内元素深入理解display:in
    html 界面跳转
    wxgz平台开发
    关于Apache PHP实现无后缀名 URL重写
    读书笔记
    比较好的书本
  • 原文地址:https://www.cnblogs.com/liujinyu/p/13229600.html
Copyright © 2011-2022 走看看