Mocha / Chai expect.to.throw not catching thrown errors

前端 未结 7 1843
傲寒
傲寒 2020-11-22 07:20

I\'m having issues getting Chai\'s expect.to.throw to work in a test for my node.js app. The test keeps failing on the thrown error, but If I wrap the test case

相关标签:
7条回答
  • 2020-11-22 08:22

    This question has many, many duplicates, including questions not mentioning the Chai assertion library. Here are the basics collected together:

    The assertion must call the function, instead of it evaluating immediately.

    assert.throws(x.y.z);      
       // FAIL.  x.y.z throws an exception, which immediately exits the
       // enclosing block, so assert.throw() not called.
    assert.throws(()=>x.y.z);  
       // assert.throw() is called with a function, which only throws
       // when assert.throw executes the function.
    assert.throws(function () { x.y.z });   
       // if you cannot use ES6 at work
    function badReference() { x.y.z }; assert.throws(badReference);  
       // for the verbose
    assert.throws(()=>model.get(z));  
       // the specific example given.
    homegrownAssertThrows(model.get, z);
       //  a style common in Python, but not in JavaScript
    

    You can check for specific errors using any assertion library:

    Node

      assert.throws(() => x.y.z);
      assert.throws(() => x.y.z, ReferenceError);
      assert.throws(() => x.y.z, ReferenceError, /is not defined/);
      assert.throws(() => x.y.z, /is not defined/);
      assert.doesNotThrow(() => 42);
      assert.throws(() => x.y.z, Error);
      assert.throws(() => model.get.z, /Property does not exist in model schema./)
    

    Should

      should.throws(() => x.y.z);
      should.throws(() => x.y.z, ReferenceError);
      should.throws(() => x.y.z, ReferenceError, /is not defined/);
      should.throws(() => x.y.z, /is not defined/);
      should.doesNotThrow(() => 42);
      should.throws(() => x.y.z, Error);
      should.throws(() => model.get.z, /Property does not exist in model schema./)
    

    Chai Expect

      expect(() => x.y.z).to.throw();
      expect(() => x.y.z).to.throw(ReferenceError);
      expect(() => x.y.z).to.throw(ReferenceError, /is not defined/);
      expect(() => x.y.z).to.throw(/is not defined/);
      expect(() => 42).not.to.throw();
      expect(() => x.y.z).to.throw(Error);
      expect(() => model.get.z).to.throw(/Property does not exist in model schema./);
    

    You must handle exceptions that 'escape' the test

    it('should handle escaped errors', function () {
      try {
        expect(() => x.y.z).not.to.throw(RangeError);
      } catch (err) {
        expect(err).to.be.a(ReferenceError);
      }
    });
    

    This can look confusing at first. Like riding a bike, it just 'clicks' forever once it clicks.

    0 讨论(0)
提交回复
热议问题