Skip to content

Commit 5a8e9f0

Browse files
committed
test(core): correct expectation in catch
1 parent 36d5a81 commit 5a8e9f0

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

packages/core/test/router/router-execution-context.spec.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
3-
import { RouteParamMetadata } from '../../../common';
3+
import { RouteParamMetadata, HttpStatus } from '../../../common';
44
import { CUSTOM_ROUTE_AGRS_METADATA } from '../../../common/constants';
55
import { RouteParamtypes } from '../../../common/enums/route-paramtypes.enum';
66
import { AbstractHttpAdapter } from '../../adapters';
@@ -15,6 +15,8 @@ import { PipesContextCreator } from '../../pipes/pipes-context-creator';
1515
import { RouteParamsFactory } from '../../router/route-params-factory';
1616
import { RouterExecutionContext } from '../../router/router-execution-context';
1717
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
18+
import { FORBIDDEN_MESSAGE } from '../../guards/constants';
19+
import { ForbiddenException } from '@nestjs/common/exceptions/forbidden.exception';
1820

1921
describe('RouterExecutionContext', () => {
2022
let contextCreator: RouterExecutionContext;
@@ -132,11 +134,20 @@ describe('RouterExecutionContext', () => {
132134
done();
133135
});
134136
});
135-
it('should throw exception when "tryActivate" returns false', () => {
137+
it('should throw exception when "tryActivate" returns false', async () => {
136138
tryActivateStub.callsFake(async () => false);
137-
proxyContext(request, response, next).catch(
138-
error => expect(error).to.not.be.undefined,
139-
);
139+
let error: Error;
140+
try {
141+
await proxyContext(request, response, next);
142+
} catch (e) {
143+
error = e;
144+
}
145+
expect(error).to.be.instanceOf(ForbiddenException);
146+
expect(error.message).to.be.eql({
147+
statusCode: HttpStatus.FORBIDDEN,
148+
error: 'Forbidden',
149+
message: FORBIDDEN_MESSAGE,
150+
});
140151
});
141152
it('should apply expected context when "canActivateFn" apply', () => {
142153
proxyContext(request, response, next).then(() => {
@@ -282,10 +293,21 @@ describe('RouterExecutionContext', () => {
282293
});
283294
});
284295
describe('createGuardsFn', () => {
285-
it('should throw exception when "tryActivate" returns false', () => {
296+
it('should throw ForbiddenException when "tryActivate" returns false', async () => {
286297
const guardsFn = contextCreator.createGuardsFn([null], null, null);
287298
sinon.stub(guardsConsumer, 'tryActivate').callsFake(async () => false);
288-
guardsFn([]).catch(err => expect(err).to.not.be.undefined);
299+
let error: ForbiddenException;
300+
try {
301+
await guardsFn([]);
302+
} catch (e) {
303+
error = e;
304+
}
305+
expect(error).to.be.instanceOf(ForbiddenException);
306+
expect(error.message).to.be.eql({
307+
statusCode: HttpStatus.FORBIDDEN,
308+
error: 'Forbidden',
309+
message: FORBIDDEN_MESSAGE,
310+
});
289311
});
290312
});
291313
describe('createHandleResponseFn', () => {

0 commit comments

Comments
 (0)