Skip to content

Commit 2d942a8

Browse files
test(nestjs) fix broken unit tests (context creator)
1 parent e0f5e1d commit 2d942a8

2 files changed

Lines changed: 47 additions & 31 deletions

File tree

packages/microservices/test/context/rpc-context-creator.spec.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
import * as sinon from 'sinon';
21
import { expect } from 'chai';
2+
import { of } from 'rxjs';
3+
import * as sinon from 'sinon';
4+
import { ApplicationConfig } from '../../../core/application-config';
5+
import { GuardsConsumer } from '../../../core/guards/guards-consumer';
6+
import { GuardsContextCreator } from '../../../core/guards/guards-context-creator';
7+
import { NestContainer } from '../../../core/injector/container';
8+
import { InterceptorsConsumer } from '../../../core/interceptors/interceptors-consumer';
9+
import { InterceptorsContextCreator } from '../../../core/interceptors/interceptors-context-creator';
10+
import { PipesConsumer } from '../../../core/pipes/pipes-consumer';
11+
import { PipesContextCreator } from '../../../core/pipes/pipes-context-creator';
12+
import { RpcException } from '../../index';
313
import {
14+
Component,
415
Guard,
516
Injectable,
617
UseGuards,
7-
Component,
818
UsePipes,
919
} from './../../../common';
10-
import { RpcProxy } from './../../context/rpc-proxy';
11-
import { RpcContextCreator } from './../../context/rpc-context-creator';
12-
import { RpcExceptionsHandler } from '../../exceptions/rpc-exceptions-handler';
1320
import { ExceptionFiltersContext } from './../../context/exception-filters-context';
14-
import { PipesContextCreator } from '../../../core/pipes/pipes-context-creator';
15-
import { PipesConsumer } from '../../../core/pipes/pipes-consumer';
16-
import { PARAMTYPES_METADATA } from '../../../common/constants';
17-
import { GuardsContextCreator } from '../../../core/guards/guards-context-creator';
18-
import { GuardsConsumer } from '../../../core/guards/guards-consumer';
19-
import { NestContainer } from '../../../core/injector/container';
20-
import { Observable, of } from 'rxjs';
21-
import { RpcException } from '../../index';
22-
import { InterceptorsContextCreator } from '../../../core/interceptors/interceptors-context-creator';
23-
import { InterceptorsConsumer } from '../../../core/interceptors/interceptors-consumer';
24-
import { ApplicationConfig } from '../../../core/application-config';
21+
import { RpcContextCreator } from './../../context/rpc-context-creator';
22+
import { RpcProxy } from './../../context/rpc-proxy';
2523

2624
@Guard()
2725
class TestGuard {
@@ -102,6 +100,9 @@ describe('RpcContextCreator', () => {
102100
describe('when proxy called', () => {
103101
it('should call guards consumer `tryActivate`', async () => {
104102
const tryActivateSpy = sinon.spy(guardsConsumer, 'tryActivate');
103+
sinon
104+
.stub(guardsContextCreator, 'create')
105+
.callsFake(() => [{ canActivate: () => true }]);
105106
const proxy = await contextCreator.create(
106107
instance,
107108
instance.test,
@@ -168,4 +169,11 @@ describe('RpcContextCreator', () => {
168169
});
169170
});
170171
});
172+
describe('createGuardsFn', () => {
173+
it('should throw exception when "tryActivate" returns false', () => {
174+
const guardsFn = contextCreator.createGuardsFn([null], null, null);
175+
sinon.stub(guardsConsumer, 'tryActivate').callsFake(() => false);
176+
expect(guardsFn([])).to.eventually.throw();
177+
});
178+
});
171179
});

packages/websockets/test/context/ws-context-creator.spec.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import * as sinon from 'sinon';
21
import { expect } from 'chai';
2+
import { of } from 'rxjs';
3+
import * as sinon from 'sinon';
4+
import { GuardsConsumer } from '../../../core/guards/guards-consumer';
5+
import { GuardsContextCreator } from '../../../core/guards/guards-context-creator';
6+
import { NestContainer } from '../../../core/injector/container';
7+
import { InterceptorsConsumer } from '../../../core/interceptors/interceptors-consumer';
8+
import { InterceptorsContextCreator } from '../../../core/interceptors/interceptors-context-creator';
9+
import { PipesConsumer } from '../../../core/pipes/pipes-consumer';
10+
import { PipesContextCreator } from '../../../core/pipes/pipes-context-creator';
11+
import { WsException } from '../../index';
312
import {
13+
Component,
414
Guard,
515
Injectable,
616
UseGuards,
7-
Component,
817
UsePipes,
918
} from './../../../common';
10-
import { WsProxy } from './../../context/ws-proxy';
11-
import { WsContextCreator } from './../../context/ws-context-creator';
12-
import { WsExceptionsHandler } from '../../exceptions/ws-exceptions-handler';
1319
import { ExceptionFiltersContext } from './../../context/exception-filters-context';
14-
import { PipesContextCreator } from '../../../core/pipes/pipes-context-creator';
15-
import { PipesConsumer } from '../../../core/pipes/pipes-consumer';
16-
import { PARAMTYPES_METADATA } from '../../../common/constants';
17-
import { GuardsContextCreator } from '../../../core/guards/guards-context-creator';
18-
import { GuardsConsumer } from '../../../core/guards/guards-consumer';
19-
import { NestContainer } from '../../../core/injector/container';
20-
import { Observable, of } from 'rxjs';
21-
import { WsException } from '../../index';
22-
import { InterceptorsContextCreator } from '../../../core/interceptors/interceptors-context-creator';
23-
import { InterceptorsConsumer } from '../../../core/interceptors/interceptors-consumer';
20+
import { WsContextCreator } from './../../context/ws-context-creator';
21+
import { WsProxy } from './../../context/ws-proxy';
2422

2523
@Guard()
2624
class TestGuard {
@@ -99,6 +97,9 @@ describe('WsContextCreator', () => {
9997
describe('when proxy called', () => {
10098
it('should call guards consumer `tryActivate`', async () => {
10199
const tryActivateSpy = sinon.spy(guardsConsumer, 'tryActivate');
100+
sinon
101+
.stub(guardsContextCreator, 'create')
102+
.callsFake(() => [{ canActivate: () => true }]);
102103
const proxy = await contextCreator.create(
103104
instance,
104105
instance.test,
@@ -165,4 +166,11 @@ describe('WsContextCreator', () => {
165166
});
166167
});
167168
});
169+
describe('createGuardsFn', () => {
170+
it('should throw exception when "tryActivate" returns false', () => {
171+
const guardsFn = contextCreator.createGuardsFn([null], null, null);
172+
sinon.stub(guardsConsumer, 'tryActivate').callsFake(() => false);
173+
expect(guardsFn([])).to.eventually.throw();
174+
});
175+
});
168176
});

0 commit comments

Comments
 (0)