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',
      )
    })
  • 相关阅读:
    8组-Alpha冲刺-2/6
    8组-Alpha冲刺-1/6
    8组 需求分析报告
    结对编程作业
    8组 团队展示
    第一次个人编程作业
    第一次博客作业
    面向对象程序设计寒假作业3
    面向对象程序设计寒假作业2
    面向对象程序设计寒假作业1
  • 原文地址:https://www.cnblogs.com/liujinyu/p/13229600.html
Copyright © 2011-2022 走看看