Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /* eslint-disable @typescript-eslint/no-explicit-any */
import {Result} from 'express-validator';
import sinon, {match} from 'sinon';
import * as config from '../config';
import errorHandler from './error-handler';
import InternalError from './internal-error';
import InvalidRequestError from './invalid-request-error';
import RestError from './rest-error';
const sandbox = sinon.createSandbox();
describe('ErrorHandler', () => {
/**
* Restore default sandbox
*/
afterEach(() => {
sandbox.restore();
});
it('throws specific Error if instanceof RestError', () => {
// Spy and stub
const responseStub: any = sandbox.stub();
const requestStub: any = sandbox.stub();
const nextStub = sinon.stub();
const validationResultStub: any = sandbox.createStubInstance(Result);
// Stub methods
validationResultStub.isEmpty.returns(true);
responseStub.status = sandbox.stub().returns(responseStub);
responseStub.send = sandbox.stub();
// Test
const error = new InvalidRequestError(validationResultStub);
errorHandler(error, requestStub, responseStub, nextStub);
sandbox.assert.notCalled(requestStub);
sandbox.assert.calledOnce(validationResultStub.isEmpty);
sandbox.assert.calledOnce(responseStub.status);
sandbox.assert.calledOnce(responseStub.send);
sandbox.assert.calledWith(responseStub.send,
match.instanceOf(RestError));
sandbox.assert.calledWith(responseStub.send,
match.has('message', error.message));
sandbox.assert.calledWith(responseStub.send,
match.has('timestamp', match.date));
sandbox.assert.calledWith(responseStub.send,
match.has('statusCode', error.statusCode));
sandbox.assert.calledWith(
responseStub.send,
match.has(
'detail',
{
...error.detail,
errorName: 'InvalidRequestError',
},
),
);
});
it('throws InternalError if not instanceof RestError', () => {
// Spy and stub
const responseStub: any = sandbox.stub();
const requestStub: any = sandbox.stub();
const nextStub: any = sandbox.stub();
// Stub methods
responseStub.status = sandbox.stub().returns(responseStub);
responseStub.send = sandbox.stub();
// Test
const error = new Error();
const stack = 'SOME STACK';
error.stack = stack;
errorHandler(error, requestStub, responseStub, nextStub);
sandbox.assert.notCalled(requestStub);
sandbox.assert.calledOnce(responseStub.status);
sandbox.assert.calledOnce(responseStub.send);
sandbox.assert.calledWith(responseStub.send,
match.instanceOf(InternalError));
sandbox.assert.calledWith(responseStub.send,
match.has('stack', stack));
});
it('throws InternalError without stack if environment is production', () => {
config.default.error.withStack = false;
// Spy and stub
const responseStub: any = sandbox.stub();
const requestStub: any = sandbox.stub();
const nextStub: any = sandbox.stub();
// Stub methods
responseStub.status = sandbox.stub().returns(responseStub);
responseStub.send = sandbox.stub();
// Test
const error = new Error();
errorHandler(error, requestStub, responseStub, nextStub);
sandbox.assert.notCalled(requestStub);
sandbox.assert.calledOnce(responseStub.status);
sandbox.assert.calledOnce(responseStub.send);
sandbox.assert.calledWith(responseStub.send,
match.instanceOf(InternalError));
sandbox.assert.calledWith(responseStub.send, match.has('stack', undefined));
});
});
|