Merge pull request #95 from OneUptime/59-tests-for-commontypesexceptionts

Add tests for exceptions
This commit is contained in:
Simon Larsen
2022-05-18 20:25:13 +01:00
committed by GitHub
8 changed files with 111 additions and 2 deletions

View File

@@ -0,0 +1,16 @@
import APIException from '../../../Types/Exception/ApiException';
describe('ApiException', () => {
test('should return error message from ApiException', () => {
expect(
new APIException('Server responded with a status code of 5000')
.message
).toBe('Server responded with a status code of 5000');
});
test('should return 2 as the code for ApiException', () => {
expect(
new APIException('Server responded with a status code of 5000').code
).toBe(2);
});
});

View File

@@ -0,0 +1,15 @@
import BadDataException from '../../../Types/Exception/BadDataException';
describe('BadDataException', () => {
test('should throw a not bad data exception', () => {
expect(() => {
throw new BadDataException('This is not a valid IPv4 address');
}).toThrow('This is not a valid IPv4 address');
});
test('should return 400 as the code for BadDataException', () => {
expect(
new BadDataException('This is not a valid IPv4 address').code
).toBe(400);
});
});

View File

@@ -0,0 +1,16 @@
import BadOperationException from '../../../Types/Exception/BadOperationException';
describe('BadOperationException', () => {
test('should return error message from BadOperationException', () => {
expect(
new BadOperationException('Cannot await a non-thenable code')
.message
).toBe('Cannot await a non-thenable code');
});
test('should return 5 as the code for BadOperationException', () => {
expect(
new BadOperationException('Cannot await a non-thenable code').code
).toBe(5);
});
});

View File

@@ -0,0 +1,15 @@
import BadRequestException from '../../../Types/Exception/BadRequestException';
describe('BadRequestException', () => {
test('should throw a bad request exception', () => {
expect(() => {
throw new BadRequestException('Forbidden. A token is needed');
}).toThrow('Forbidden. A token is needed');
});
test('should return 400 as the code for BadRequestException', () => {
expect(
new BadRequestException('Forbidden. A token is needed').code
).toBe(400);
});
});

View File

@@ -0,0 +1,13 @@
import DatabaseNotConnectedException from '../../../Types/Exception/DatabaseNotConnectedException';
describe('DatabaseNotConnectedException', () => {
test('should return the error message set in database exception', () => {
expect(new DatabaseNotConnectedException().message).toBe(
'Database not connected'
);
});
test('should return 3 as the code for DatabaseNotConnectedException', () => {
expect(new DatabaseNotConnectedException().code).toBe(3);
});
});

View File

@@ -0,0 +1,21 @@
import Exception from '../../../Types/Exception/Exception';
describe('Exception', () => {
test('should throw an exception from exception class', () => {
expect(() => {
throw new Exception(1, 'General exception error message');
}).toThrow('General exception error message');
});
test('should return error message', () => {
expect(
new Exception(0, 'This code has not been implemented').message
).toBe('This code has not been implemented');
});
test('should return 1 as the code for Exception', () => {
expect(new Exception(1, 'This is not a valid IPv4 address').code).toBe(
1
);
});
});

View File

@@ -0,0 +1,13 @@
import NotImplementedException from '../../../Types/Exception/NotImplementedException';
describe('NotImplementedException', () => {
test('should throw a not implemented exception', () => {
expect(() => {
throw new NotImplementedException();
}).toThrow('This code is not implemented');
});
test('should return 0 as the code for NotImplementedException', () => {
expect(new NotImplementedException().code).toBe(0);
});
});

View File

@@ -7,8 +7,8 @@ export default class Exception extends Error {
return this._code;
}
public set code(v: ExceptionCode) {
this._code = v;
public set code(value: ExceptionCode) {
this._code = value;
}
public constructor(code: ExceptionCode, message: string) {