Aurelia Unit Testing --> Expect Error


currently I’m trying to create a unit test case that expects a thrown exception:

it('should throw an error because of invalid base url', () => {
        let errorApiConfiguration = new TestApiConfigurationDev();
        errorApiConfiguration.baseRoute = 'localhost:9000';

the respective setApiConfigurations method throws the exception:

public setApiConfigurations(object: Object[]) {
     throw new Error('test');

With this setup the test run fail:

ApiService › should throw an error because of invalid base url


      207 |    public setApiConfigurations(object: Object[]) {
    > 208 |         throw new Error('test'); 
      209 |     }   

Does anyone know what I’m doing wrong here respectively how to get that test working?

You have to change the assertion to execute an function which in turn throws.

expect(() => this.apiService.setApiConfigurations([errorApiConfiguration])).toThrowError(Error);
1 Like