代码之家  ›  专栏  ›  技术社区  ›  agusgambina

摩卡和柴测试预计不会抛出错误

  •  0
  • agusgambina  · 技术社区  · 6 年前

    我想测试,因为他们正在通过测试,如果他们没有抛出错误

    例如

    async function sum(a, b){
        Promise.resolve()
        .then()
        if(typeof(a) !== 'number')
            throw new Error('a should be a number')
    
        if(typeof(b) !== 'number')  
            throw new Error('b should be a number')
    
        console.log('the sum is ', a + b)
    
        return
    }
    

    expect(sum(1, 2)).not.to.throw()
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   agusgambina    6 年前

    解决这个问题的方法是 chai-as-promise 图书馆

    const chai = require('chai');
    const chaiAsPromised = require("chai-as-promised");
    chai.use(chaiAsPromised);
    const expect = chai.expect;
    
    expect(sum(1, 2)).to.eventually.not.be.rejectedWith(Error)
    
    推荐文章