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

前端 未结 7 1847
傲寒
傲寒 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:02

    I have found a nice way around it:

    // The test, BDD style
    it ("unsupported site", () => {
        The.function(myFunc)
        .with.arguments({url:"https://www.ebay.com/"})
        .should.throw(/unsupported/);
    });
    
    
    // The function that does the magic: (lang:TypeScript)
    export const The = {
        'function': (func:Function) => ({
            'with': ({
                'arguments': function (...args:any) {
                    return () => func(...args);
                }
            })
        })
    };
    

    It's much more readable then my old version:

    it ("unsupported site", () => {
        const args = {url:"https://www.ebay.com/"}; //Arrange
        function check_unsupported_site() { myFunc(args) } //Act
        check_unsupported_site.should.throw(/unsupported/) //Assert
    });
    

提交回复
热议问题