build: use strict null checks part 3

This commit is contained in:
Kamil Myśliwiec
2024-11-26 13:49:07 +01:00
parent 22008976ec
commit b94b0b910e
41 changed files with 209 additions and 197 deletions

View File

@@ -44,7 +44,7 @@ describe('@UseInterceptors', () => {
try {
UseInterceptors({
intercept() {
return null;
return null!;
},
})({ name: 'target' } as any);
} catch (e) {

View File

@@ -112,7 +112,7 @@ describe('ConfigurableModuleBuilder', () => {
provideInjectionTokensFrom[2],
);
expect(MODULE_OPTIONS_TOKEN).to.equal('RANDOM_TEST_MODULE_OPTIONS');
expect((definition.providers[0] as any).provide).to.equal(
expect((definition.providers![0] as any).provide).to.equal(
'RANDOM_TEST_MODULE_OPTIONS',
);

View File

@@ -21,7 +21,10 @@ describe('ParseBoolPipe', () => {
it('should not throw an error if the value is undefined/null and optional is true', async () => {
const target = new ParseBoolPipe({ optional: true });
const value = await target.transform(undefined, {} as ArgumentMetadata);
const value = await target.transform(
undefined!,
{} as ArgumentMetadata,
);
expect(value).to.equal(undefined);
});
});

View File

@@ -14,12 +14,12 @@ describe('ParseDatePipe', () => {
it('should return a valid date object', () => {
const date = new Date().toISOString();
const transformedDate = target.transform(date);
const transformedDate = target.transform(date)!;
expect(transformedDate).to.be.instanceOf(Date);
expect(transformedDate.toISOString()).to.equal(date);
const asNumber = transformedDate.getTime();
const transformedNumber = target.transform(asNumber);
const transformedNumber = target.transform(asNumber)!;
expect(transformedNumber).to.be.instanceOf(Date);
expect(transformedNumber.getTime()).to.equal(asNumber);
});

View File

@@ -30,7 +30,10 @@ describe('ParseEnumPipe', () => {
it('should not throw an error if enumType is undefined/null and optional is true', async () => {
const target = new ParseEnumPipe('DOWN', { optional: true });
const value = await target.transform(undefined, {} as ArgumentMetadata);
const value = await target.transform(
undefined!,
{} as ArgumentMetadata,
);
expect(value).to.equal(undefined);
});
});

View File

@@ -1,8 +1,7 @@
import * as sinon from 'sinon';
import { expect } from 'chai';
import { HttpException } from '../../exceptions';
import { ArgumentMetadata } from '../../interfaces';
import { ParseFloatPipe } from '../../pipes/parse-float.pipe';
import { HttpException } from '../../exceptions';
class CustomTestError extends HttpException {
constructor() {
@@ -27,7 +26,10 @@ describe('ParseFloatPipe', () => {
});
it('should not throw an error if the value is undefined/null and optional is true', async () => {
const target = new ParseFloatPipe({ optional: true });
const value = await target.transform(undefined, {} as ArgumentMetadata);
const value = await target.transform(
undefined!,
{} as ArgumentMetadata,
);
expect(value).to.equal(undefined);
});
});

View File

@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { HttpException } from '../../exceptions';
import { ArgumentMetadata } from '../../interfaces';
import { ParseIntPipe } from '../../pipes/parse-int.pipe';
import { HttpException } from '../../exceptions';
class CustomTestError extends HttpException {
constructor() {
@@ -32,7 +32,10 @@ describe('ParseIntPipe', () => {
});
it('should not throw an error if the value is undefined/null and optional is true', async () => {
const target = new ParseIntPipe({ optional: true });
const value = await target.transform(undefined, {} as ArgumentMetadata);
const value = await target.transform(
undefined!,
{} as ArgumentMetadata,
);
expect(value).to.equal(undefined);
});
});

View File

@@ -43,7 +43,10 @@ describe('ParseUUIDPipe', () => {
});
it('should not throw an error if the value is undefined/null and optional is true', async () => {
const target = new ParseUUIDPipe({ optional: true });
const value = await target.transform(undefined, {} as ArgumentMetadata);
const value = await target.transform(
undefined!,
{} as ArgumentMetadata,
);
expect(value).to.equal(undefined);
});
});
@@ -59,7 +62,7 @@ describe('ParseUUIDPipe', () => {
it('should throw an error - not a string', async () => {
target = new ParseUUIDPipe({ exceptionFactory });
await expect(
target.transform(undefined, {} as ArgumentMetadata),
target.transform(undefined!, {} as ArgumentMetadata),
).to.be.rejectedWith(TestException);
});

View File

@@ -123,7 +123,7 @@ describe('Shared utils', () => {
});
it('should return empty path', () => {
expect(addLeadingSlash('')).to.be.eql('');
expect(addLeadingSlash(null)).to.be.eql('');
expect(addLeadingSlash(null!)).to.be.eql('');
expect(addLeadingSlash(undefined)).to.be.eql('');
});
});
@@ -140,7 +140,7 @@ describe('Shared utils', () => {
});
it('should return / for empty path', () => {
expect(normalizePath('')).to.be.eql('/');
expect(normalizePath(null)).to.be.eql('/');
expect(normalizePath(null!)).to.be.eql('/');
expect(normalizePath(undefined)).to.be.eql('/');
});
});

View File

@@ -1,8 +1,5 @@
import { RequestMethod } from '@nestjs/common';
import {
GlobalPrefixOptions,
VersioningOptions,
} from '@nestjs/common/interfaces';
import { GlobalPrefixOptions } from '@nestjs/common/interfaces';
import { expect } from 'chai';
import { ApplicationConfig } from '../application-config';
import { ExcludeRouteMetadata } from '../router/interfaces/exclude-route-metadata.interface';
@@ -141,7 +138,7 @@ describe('ApplicationConfig', () => {
const options = { type: 'test', defaultVersion: ['1', '2', '2', '1'] };
appConfig.enableVersioning(options as any);
expect(appConfig.getVersioning().defaultVersion).to.be.eql(['1', '2']);
expect(appConfig.getVersioning()!.defaultVersion).to.be.eql(['1', '2']);
});
it('should have undefined as the versioning by default', () => {

View File

@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { UnknownDependenciesException } from '../../../errors/exceptions/unknown-dependencies.exception';
import {
INVALID_MODULE_MESSAGE,
UNDEFINED_MODULE_MESSAGE,
} from '../../../errors/messages';
import { Module } from '../../../injector/module';
import { stringCleaner } from '../../utils/string.cleaner';
import {
UNDEFINED_MODULE_MESSAGE,
INVALID_MODULE_MESSAGE,
} from '../../../errors/messages';
describe('Error Messages', () => {
const CatsModule = { name: 'CatsModule' };
@@ -115,7 +115,7 @@ describe('Error Messages', () => {
const actualMessage = stringCleaner(
new UnknownDependenciesException('CatService', {
index,
dependencies: ['', undefined],
dependencies: ['', undefined!],
}).message,
);

View File

@@ -33,7 +33,7 @@ describe('BaseExceptionFilterContext', () => {
expect(filter.getFilterInstance(Filter)).to.be.eql(wrapper.instance);
});
it('should return null', () => {
sinon.stub(filter, 'getInstanceByMetatype').callsFake(() => null);
sinon.stub(filter, 'getInstanceByMetatype').callsFake(() => null!);
expect(filter.getFilterInstance(Filter)).to.be.eql(null);
});
});
@@ -43,7 +43,7 @@ describe('BaseExceptionFilterContext', () => {
describe('when "moduleContext" is nil', () => {
it('should return undefined', () => {
(filter as any).moduleContext = undefined;
expect(filter.getInstanceByMetatype(null)).to.be.undefined;
expect(filter.getInstanceByMetatype(null!)).to.be.undefined;
});
});
describe('when "moduleContext" is not nil', () => {
@@ -54,7 +54,7 @@ describe('BaseExceptionFilterContext', () => {
describe('and when module exists', () => {
it('should return undefined', () => {
sinon.stub(container.getModules(), 'get').callsFake(() => undefined);
expect(filter.getInstanceByMetatype(null)).to.be.undefined;
expect(filter.getInstanceByMetatype(null!)).to.be.undefined;
});
});

View File

@@ -120,7 +120,7 @@ describe('ExceptionsHandler', () => {
expect((handler as any).filters).to.be.eql(filters);
});
it('should throw exception when passed argument is not an array', () => {
expect(() => handler.setCustomFilters(null)).to.throws(
expect(() => handler.setCustomFilters(null!)).to.throws(
InvalidExceptionFilterException,
);
});
@@ -128,7 +128,7 @@ describe('ExceptionsHandler', () => {
describe('invokeCustomFilters', () => {
describe('when filters array is empty', () => {
it('should return false', () => {
expect(handler.invokeCustomFilters(null, null)).to.be.false;
expect(handler.invokeCustomFilters(null, null!)).to.be.false;
});
});
describe('when filters array is not empty', () => {
@@ -144,7 +144,7 @@ describe('ExceptionsHandler', () => {
(handler as any).filters = filters;
});
it('should call funcSpy', () => {
handler.invokeCustomFilters(new TestException(), null);
handler.invokeCustomFilters(new TestException(), null!);
expect(funcSpy.notCalled).to.be.false;
});
it('should call funcSpy with exception and response passed as an arguments', () => {
@@ -155,17 +155,17 @@ describe('ExceptionsHandler', () => {
expect(funcSpy.calledWith(exception, res)).to.be.true;
});
it('should return true', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
expect(handler.invokeCustomFilters(new TestException(), null!)).to.be
.true;
});
});
describe('when filter does not exists in filters array', () => {
it('should not call funcSpy', () => {
handler.invokeCustomFilters(new TestException(), null);
handler.invokeCustomFilters(new TestException(), null!);
expect(funcSpy.notCalled).to.be.true;
});
it('should return false', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
expect(handler.invokeCustomFilters(new TestException(), null!)).to.be
.false;
});
});

View File

@@ -1,6 +1,5 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { ExceptionFilter } from '../../../common';
import { Catch } from '../../../common/decorators/core/catch.decorator';
import { UseFilters } from '../../../common/decorators/core/exception-filters.decorator';
import { ApplicationConfig } from '../../application-config';
@@ -38,7 +37,7 @@ describe('ExternalExceptionFilterContext', () => {
const filter = exceptionFilter.create(
new EmptyMetadata(),
() => ({}) as any,
undefined,
undefined!,
);
expect((filter as any).filters).to.be.empty;
});
@@ -51,7 +50,7 @@ describe('ExternalExceptionFilterContext', () => {
const filter = exceptionFilter.create(
new WithMetadata(),
() => ({}) as any,
undefined,
undefined!,
);
expect((filter as any).filters).to.not.be.empty;
});

View File

@@ -18,7 +18,7 @@ describe('ExternalExceptionsHandler', () => {
describe('next', () => {
it('should method returns expected stream with message when exception is unknown', () => {
const error = new Error();
expect(() => handler.next(error, null)).to.throw(error);
expect(() => handler.next(error, null!)).to.throw(error);
});
describe('when "invokeCustomFilters" returns value', () => {
const observable$ = of(true);
@@ -26,7 +26,7 @@ describe('ExternalExceptionsHandler', () => {
sinon.stub(handler, 'invokeCustomFilters').returns(observable$ as any);
});
it('should return observable', () => {
const result = handler.next(new Error(), null);
const result = handler.next(new Error(), null!);
expect(result).to.be.eql(observable$);
});
});
@@ -38,13 +38,13 @@ describe('ExternalExceptionsHandler', () => {
expect((handler as any).filters).to.be.eql(filters);
});
it('should throw exception when passed argument is not an array', () => {
expect(() => handler.setCustomFilters(null)).to.throw();
expect(() => handler.setCustomFilters(null!)).to.throw();
});
});
describe('invokeCustomFilters', () => {
describe('when filters array is empty', () => {
it('should return identity', () => {
expect(handler.invokeCustomFilters(null, null)).to.be.null;
expect(handler.invokeCustomFilters(null, null!)).to.be.null;
});
});
describe('when filters array is not empty', () => {
@@ -61,16 +61,16 @@ describe('ExternalExceptionsHandler', () => {
(handler as any).filters = filters;
});
it('should call funcSpy', async () => {
await handler.invokeCustomFilters(new TestException(), null);
await handler.invokeCustomFilters(new TestException(), null!);
expect(funcSpy.notCalled).to.be.false;
});
it('should call funcSpy with exception and response passed as an arguments', async () => {
const exception = new TestException();
await handler.invokeCustomFilters(exception, null);
await handler.invokeCustomFilters(exception, null!);
expect(funcSpy.calledWith(exception)).to.be.true;
});
it('should return stream', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
expect(handler.invokeCustomFilters(new TestException(), null!)).to.be
.not.null;
});
});
@@ -82,11 +82,11 @@ describe('ExternalExceptionsHandler', () => {
(handler as any).filters = filters;
});
it('should not call funcSpy', async () => {
await handler.invokeCustomFilters(new TestException(), null);
await handler.invokeCustomFilters(new TestException(), null!);
expect(funcSpy.notCalled).to.be.true;
});
it('should return null', () => {
expect(handler.invokeCustomFilters(new TestException(), null)).to.be
expect(handler.invokeCustomFilters(new TestException(), null!)).to.be
.null;
});
});

View File

@@ -16,7 +16,7 @@ describe('GuardsConsumer', () => {
[],
[],
{ constructor: null },
null,
null!,
);
expect(canActivate).to.be.true;
});
@@ -28,7 +28,7 @@ describe('GuardsConsumer', () => {
[...guards, { canActivate: () => false }],
[],
{ constructor: null },
null,
null!,
);
expect(canActivate).to.be.false;
});
@@ -39,7 +39,7 @@ describe('GuardsConsumer', () => {
guards,
[],
{ constructor: null },
null,
null!,
);
expect(canActivate).to.be.true;
});

View File

@@ -79,7 +79,7 @@ describe('GuardsContextCreator', () => {
describe('getGuardInstance', () => {
describe('when param is an object', () => {
it('should return instance', () => {
const instance = { canActivate: () => null };
const instance = { canActivate: () => null! };
expect(guardsContextCreator.getGuardInstance(instance)).to.be.eql(
instance,
);
@@ -101,7 +101,7 @@ describe('GuardsContextCreator', () => {
it('should return null', () => {
sinon
.stub(guardsContextCreator, 'getInstanceByMetatype')
.callsFake(() => null);
.callsFake(() => null!);
expect(guardsContextCreator.getGuardInstance(Guard)).to.be.eql(null);
});
});
@@ -111,7 +111,7 @@ describe('GuardsContextCreator', () => {
describe('when "moduleContext" is nil', () => {
it('should return undefined', () => {
(guardsContextCreator as any).moduleContext = undefined;
expect(guardsContextCreator.getInstanceByMetatype(null)).to.be
expect(guardsContextCreator.getInstanceByMetatype(null!)).to.be
.undefined;
});
});

View File

@@ -54,7 +54,9 @@ describe('ContextUtils', () => {
);
const keys = Object.keys(metadata);
const custom = keys.find(key => key.includes(CUSTOM_ROUTE_ARGS_METADATA));
const custom = keys.find(key =>
key.includes(CUSTOM_ROUTE_ARGS_METADATA),
)!;
expect(metadata[custom]).to.be.an('object');
expect(metadata[custom].index).to.be.eq(2);
@@ -90,7 +92,7 @@ describe('ContextUtils', () => {
it('should return "paramsProperties" when paramtypes array doesn\'t exists', () => {
const paramsProperties = ['1'];
expect(
contextUtils.mergeParamsMetatypes(paramsProperties as any, null),
contextUtils.mergeParamsMetatypes(paramsProperties as any, null!),
).to.be.eql(paramsProperties);
});
});
@@ -113,7 +115,7 @@ describe('ContextUtils', () => {
const customFactory = undefined;
expect(
contextUtils.getCustomFactory(
customFactory,
customFactory!,
undefined,
contextFactory,
)(),

View File

@@ -55,7 +55,7 @@ describe('ExternalContextCreator', () => {
contextCreator,
'getContextModuleKey',
);
contextCreator.create({ foo: 'bar' }, callback, '', '', null);
contextCreator.create({ foo: 'bar' }, callback, '', '', null!);
expect(getContextModuleKeySpy.called).to.be.true;
done();
});
@@ -65,7 +65,7 @@ describe('ExternalContextCreator', () => {
beforeEach(() => {
instance = { foo: 'bar' };
proxyContext = contextCreator.create(instance, callback, '', '', null);
proxyContext = contextCreator.create(instance, callback, '', '', null!);
});
it('should be a function', () => {
expect(proxyContext).to.be.a('function');
@@ -187,12 +187,12 @@ describe('ExternalContextCreator', () => {
{
index: 1,
type: 'test',
data: null,
data: null!,
pipes: [],
extractValue: () => null,
},
],
);
)!;
await pipesFn([]);
expect(pipesFn).to.be.a('function');
});

View File

@@ -30,25 +30,25 @@ describe('NestContainer', () => {
});
it('should "addProvider" throw "CircularDependencyException" when provider is nil', () => {
expect(() => container.addProvider(null, 'TestModule')).throw(
expect(() => container.addProvider(null!, 'TestModule')).throw(
CircularDependencyException,
);
});
it('should "addController" throw "UnknownModuleException" when module is not stored in collection', () => {
expect(() => container.addController(null, 'TestModule')).throw(
expect(() => container.addController(null!, 'TestModule')).throw(
UnknownModuleException,
);
});
it('should "addExportedProvider" throw "UnknownModuleException" when module is not stored in collection', () => {
expect(() => container.addExportedProvider(null, 'TestModule')).throw(
expect(() => container.addExportedProvider(null!, 'TestModule')).throw(
UnknownModuleException,
);
});
it('should "addInjectable" throw "UnknownModuleException" when module is not stored in collection', () => {
expect(() => container.addInjectable(null, 'TestModule', null)).throw(
expect(() => container.addInjectable(null!, 'TestModule', null!)).throw(
UnknownModuleException,
);
});
@@ -74,7 +74,7 @@ describe('NestContainer', () => {
});
it('should throw an exception when metatype is not defined', () => {
expect(container.addModule(undefined, [])).to.eventually.throws();
expect(container.addModule(undefined!, [])).to.eventually.throws();
});
it('should add global module when module is global', async () => {
@@ -104,7 +104,7 @@ describe('NestContainer', () => {
});
it('should throw an exception when metatype is not defined', () => {
expect(container.addModule(undefined, [])).to.eventually.throws();
expect(container.addModule(undefined!, [])).to.eventually.throws();
});
it('should add global module when module is global', async () => {
@@ -156,7 +156,7 @@ describe('NestContainer', () => {
describe('when "module" is not "globalModule"', () => {
it('should call "addImport"', () => {
const module = { addImport: sinon.spy() };
container.bindGlobalModuleToModule(module as any, null);
container.bindGlobalModuleToModule(module as any, null!);
expect(module.addImport.calledOnce).to.be.true;
});
});
@@ -181,7 +181,7 @@ describe('NestContainer', () => {
describe('when dynamic metadata exists', () => {
it('should add to the dynamic metadata collection', async () => {
const addSpy = sinon.spy(collection, 'set');
const dynamicMetadata = { module: null };
const dynamicMetadata = { module: null! };
await container.addDynamicMetadata(token, dynamicMetadata, []);
expect(addSpy.calledWith(token, dynamicMetadata)).to.be.true;
@@ -190,7 +190,7 @@ describe('NestContainer', () => {
describe('when dynamic metadata does not exists', () => {
it('should not add to the dynamic metadata collection', async () => {
const addSpy = sinon.spy(collection, 'set');
await container.addDynamicMetadata(token, null, []);
await container.addDynamicMetadata(token, null!, []);
expect(addSpy.called).to.be.false;
});
});
@@ -201,7 +201,7 @@ describe('NestContainer', () => {
describe('when array is empty/undefined', () => {
it('should not call "addModule"', async () => {
const addModuleSpy = sinon.spy(container, 'addModule');
await container.addDynamicModules(undefined, []);
await container.addDynamicModules(undefined!, []);
expect(addModuleSpy.called).to.be.false;
});
});

View File

@@ -19,7 +19,7 @@ describe('provider classifier', () => {
it('should return false if useClass is undefined', () => {
const classProvider: ClassProvider = {
useClass: undefined,
useClass: undefined!,
provide: 'token',
};
@@ -35,9 +35,9 @@ describe('provider classifier', () => {
});
it('should return false if provider is undefined', () => {
const classProvider = undefined;
const classProvider = undefined!;
expect(isClassProvider(classProvider as ClassProvider)).to.be.false;
expect(isClassProvider(classProvider as ClassProvider)!).to.be.false;
});
});
@@ -96,7 +96,7 @@ describe('provider classifier', () => {
});
it('should return false if provider is undefined', () => {
const valueProvider = undefined;
const valueProvider = undefined!;
expect(isValueProvider(valueProvider as ValueProvider)).to.be.false;
});
@@ -123,7 +123,7 @@ describe('provider classifier', () => {
it('should return false if useFactory is undefined', () => {
const factoryProvider: FactoryProvider = {
provide: 'token',
useFactory: undefined,
useFactory: undefined!,
};
expect(isFactoryProvider(factoryProvider)).to.be.false;

View File

@@ -167,13 +167,13 @@ describe('Injector', () => {
it('should create prototype of instance', () => {
injector.loadPrototype(test, moduleDeps.providers);
expect(moduleDeps.providers.get('Test').instance).to.deep.equal(
expect(moduleDeps.providers.get('Test')!.instance).to.deep.equal(
Object.create(Test.prototype),
);
});
it('should return undefined when collection is nil', () => {
const result = injector.loadPrototype(test, null);
const result = injector.loadPrototype(test, null!);
expect(result).to.be.undefined;
});
@@ -201,10 +201,10 @@ describe('Injector', () => {
it('should throw "RuntimeException" when param is undefined', async () => {
return expect(
injector.resolveSingleParam(
null,
undefined,
null!,
undefined!,
{ index: 0, dependencies: [] },
null,
null!,
),
).to.eventually.be.rejected;
});
@@ -227,7 +227,7 @@ describe('Injector', () => {
await injector.loadMiddleware(
{ metatype: { name: '', prototype: {} } } as any,
collection as any,
null,
null!,
);
expect(loadInstanceSpy.called).to.be.true;
});
@@ -243,7 +243,7 @@ describe('Injector', () => {
await injector.loadMiddleware(
{ metatype: { name: '' } } as any,
collection as any,
null,
null!,
);
expect(loadInstanceSpy.called).to.be.false;
});
@@ -307,7 +307,7 @@ describe('Injector', () => {
};
const result = await injector.lookupComponent(
collection as any,
null,
null!,
{ name: metatype.name, index: 0, dependencies: [] },
wrapper,
);
@@ -323,7 +323,7 @@ describe('Injector', () => {
};
const result = injector.lookupComponent(
collection as any,
null,
null!,
{ name, index: 0, dependencies: [] },
Object.assign(wrapper, {
name,
@@ -339,7 +339,7 @@ describe('Injector', () => {
};
await injector.lookupComponent(
collection as any,
null,
null!,
{ name: metatype.name, index: 0, dependencies: [] },
wrapper,
);
@@ -420,7 +420,7 @@ describe('Injector', () => {
injector.lookupComponentInImports(
moduleFixture as any,
metatype as any,
null,
null!,
),
).to.be.eventually.eq(null);
@@ -443,7 +443,7 @@ describe('Injector', () => {
injector.lookupComponentInImports(
moduleFixture as any,
metatype as any,
null,
null!,
),
).to.eventually.be.eq(null);
});
@@ -500,7 +500,7 @@ describe('Injector', () => {
await injector.lookupComponentInImports(
moduleFixture as any,
metatype as any,
null,
null!,
);
expect(loadProvider.called).to.be.false;
});
@@ -546,7 +546,7 @@ describe('Injector', () => {
});
describe('resolveComponentInstance', () => {
let module;
let module: any;
beforeEach(() => {
module = {
providers: [],
@@ -559,7 +559,7 @@ describe('Injector', () => {
const loadStub = sinon
.stub(injector, 'loadProvider')
.callsFake(() => null);
.callsFake(() => null!);
sinon
.stub(injector, 'lookupComponent')
.returns(Promise.resolve(wrapper));
@@ -576,7 +576,7 @@ describe('Injector', () => {
const wrapper = new InstanceWrapper({ isResolved: true });
const loadStub = sinon
.stub(injector, 'loadProvider')
.callsFake(() => null);
.callsFake(() => null!);
sinon
.stub(injector, 'lookupComponent')
@@ -597,7 +597,7 @@ describe('Injector', () => {
});
const loadStub = sinon
.stub(injector, 'loadProvider')
.callsFake(() => null);
.callsFake(() => null!);
sinon
.stub(injector, 'lookupComponent')
@@ -615,7 +615,7 @@ describe('Injector', () => {
describe('when instanceWrapper has async property', () => {
it('should await instance', async () => {
sinon.stub(injector, 'loadProvider').callsFake(() => null);
sinon.stub(injector, 'loadProvider').callsFake(() => null!);
const instance = Promise.resolve(true);
const wrapper = new InstanceWrapper({
@@ -713,7 +713,7 @@ describe('Injector', () => {
const container = new NestContainer();
const moduleCtor = class TestModule {};
const ctx = STATIC_CONTEXT;
const { moduleRef } = await container.addModule(moduleCtor, []);
const { moduleRef } = (await container.addModule(moduleCtor, []))!;
moduleRef.addProvider({
provide: TestClass,
@@ -798,7 +798,7 @@ describe('Injector', () => {
const loadCtorMetadataSpy = sinon.spy(injector, 'loadCtorMetadata');
await injector.resolveConstructorParams(
wrapper,
null,
null!,
[],
() => {
expect(loadCtorMetadataSpy.called).to.be.true;
@@ -818,7 +818,7 @@ describe('Injector', () => {
injector,
'loadPropertiesMetadata',
);
await injector.resolveProperties(wrapper, null, null, { id: 2 });
await injector.resolveProperties(wrapper, null!, null!, { id: 2 });
expect(loadPropertiesMetadataSpy.called).to.be.true;
});
});

View File

@@ -12,15 +12,15 @@ describe('InternalCoreModuleFactory', () => {
it('should return the internal core module definition', () => {
const moduleDefinition = InternalCoreModuleFactory.create(
new NestContainer(),
null,
null,
null,
null,
null!,
null!,
null!,
null!,
);
expect(moduleDefinition.module).to.equal(InternalCoreModule);
const providedInjectables = moduleDefinition.providers.map(
const providedInjectables = moduleDefinition.providers!.map(
item => (item as ClassProvider | FactoryProvider).provide,
);
expect(providedInjectables).to.deep.equal([
@@ -31,7 +31,7 @@ describe('InternalCoreModuleFactory', () => {
SerializedGraph,
]);
const lazyModuleLoaderProvider = moduleDefinition.providers.find(
const lazyModuleLoaderProvider = moduleDefinition.providers!.find(
item => (item as FactoryProvider)?.provide === LazyModuleLoader,
) as FactoryProvider;
expect(lazyModuleLoaderProvider.useFactory()).to.be.instanceOf(

View File

@@ -356,7 +356,7 @@ describe('Module', () => {
sinon.stub(moduleRef, 'hasProvider').callsFake(() => true);
sinon.stub(moduleRef.providers, 'get').callsFake(() => wrapper as any);
moduleRef.replace(null, { isProvider: true });
moduleRef.replace(null!, { isProvider: true });
expect(wrapper.mergeWith.called).to.be.true;
});
});
@@ -371,7 +371,7 @@ describe('Module', () => {
.stub(moduleRef.injectables, 'get')
.callsFake(() => wrapper as any);
moduleRef.replace(null, {});
moduleRef.replace(null!, {});
expect(wrapper.mergeWith.called).to.be.true;
});
});

View File

@@ -76,7 +76,7 @@ describe('GraphInspector', () => {
metadata: {
injectionType: 'constructor',
keyOrIndex: 0,
sourceClassName: instanceWrapper.metatype.name,
sourceClassName: instanceWrapper.metatype!.name,
sourceClassToken: instanceWrapper.token,
sourceModuleName: 'TestModule',
targetClassName: param1.name,
@@ -92,7 +92,7 @@ describe('GraphInspector', () => {
metadata: {
injectionType: 'constructor',
keyOrIndex: 1,
sourceClassName: instanceWrapper.metatype.name,
sourceClassName: instanceWrapper.metatype!.name,
sourceClassToken: instanceWrapper.token,
sourceModuleName: 'TestModule',
targetClassName: param2.name,
@@ -129,7 +129,7 @@ describe('GraphInspector', () => {
class RandomPipe {}
it('should inspect all modules', async () => {
const { moduleRef } = await container.addModule(TestModule, []);
const { moduleRef } = (await container.addModule(TestModule, []))!;
moduleRef.addController(AController);
const subtype = 'interceptor';
@@ -203,7 +203,7 @@ describe('GraphInspector', () => {
initTime: 100,
},
};
const insertedNode = graph.insertNode(nodeDefinition);
const insertedNode = graph.insertNode(nodeDefinition)!;
graphInspector.insertAttachedEnhancer(instanceWrapper);

View File

@@ -28,7 +28,7 @@ describe('InterceptorsConsumer', () => {
next = sinon.spy();
});
it('should call next()', async () => {
await consumer.intercept([], null, { constructor: null }, null, next);
await consumer.intercept([], null!, { constructor: null }, null!, next);
expect(next.calledOnce).to.be.true;
});
});
@@ -40,9 +40,9 @@ describe('InterceptorsConsumer', () => {
it('does not call `intercept` (lazy evaluation)', async () => {
await consumer.intercept(
interceptors,
null,
null!,
{ constructor: null },
null,
null!,
next,
);
@@ -52,9 +52,9 @@ describe('InterceptorsConsumer', () => {
it('should call every `intercept` method when subscribe', async () => {
const intercepted = await consumer.intercept(
interceptors,
null,
null!,
{ constructor: null },
null,
null!,
next,
);
await transformToResult(intercepted);
@@ -65,9 +65,9 @@ describe('InterceptorsConsumer', () => {
it('should not call `next` (lazy evaluation)', async () => {
await consumer.intercept(
interceptors,
null,
null!,
{ constructor: null },
null,
null!,
next,
);
expect(next.called).to.be.false;
@@ -75,9 +75,9 @@ describe('InterceptorsConsumer', () => {
it('should call `next` when subscribe', async () => {
const intercepted = await consumer.intercept(
interceptors,
null,
null!,
{ constructor: null },
null,
null!,
next,
);
await transformToResult(intercepted);
@@ -97,13 +97,13 @@ describe('InterceptorsConsumer', () => {
}
}
const next = () => {
return Promise.resolve(storage.getStore().value);
return Promise.resolve(storage.getStore()!.value);
};
const intercepted = await consumer.intercept(
[new StorageInterceptor()],
null,
null!,
{ constructor: null },
null,
null!,
next,
);
const result = await transformToResult(intercepted);
@@ -131,9 +131,9 @@ describe('InterceptorsConsumer', () => {
}
const intercepted = await consumer.intercept(
[new RetryInterceptor()],
null,
null!,
{ constructor: null },
null,
null!,
next,
);
expect(await transformToResult(intercepted)).to.equal(3);
@@ -207,9 +207,9 @@ describe('InterceptorsConsumer', () => {
const observable = await consumer.intercept(
testInterceptors,
null,
null!,
{ constructor: null },
null,
null!,
async () => 1,
);

View File

@@ -84,7 +84,7 @@ describe('InterceptorsContextCreator', () => {
describe('getInterceptorInstance', () => {
describe('when param is an object', () => {
it('should return instance', () => {
const instance = { intercept: () => null };
const instance = { intercept: () => null! };
expect(
interceptorsContextCreator.getInterceptorInstance(instance),
).to.be.eql(instance);
@@ -106,7 +106,7 @@ describe('InterceptorsContextCreator', () => {
it('should return null', () => {
sinon
.stub(interceptorsContextCreator, 'getInstanceByMetatype')
.callsFake(() => null);
.callsFake(() => null!);
expect(
interceptorsContextCreator.getInterceptorInstance(Interceptor),
).to.be.eql(null);
@@ -118,7 +118,7 @@ describe('InterceptorsContextCreator', () => {
describe('when "moduleContext" is nil', () => {
it('should return undefined', () => {
(interceptorsContextCreator as any).moduleContext = undefined;
expect(interceptorsContextCreator.getInstanceByMetatype(null)).to.be
expect(interceptorsContextCreator.getInstanceByMetatype(null!)).to.be
.undefined;
});
});

View File

@@ -47,7 +47,7 @@ describe('MiddlewareContainer', () => {
},
];
container.insertConfig(config, 'Module');
expect([...container.getConfigurations().get('Module')]).to.deep.equal(
expect([...container.getConfigurations().get('Module')!]).to.deep.equal(
config,
);
});
@@ -68,7 +68,7 @@ describe('MiddlewareContainer', () => {
expect(collection.size).to.eql(config.length);
expect(insertedMiddleware).to.be.instanceOf(InstanceWrapper);
expect(insertedMiddleware.scope).to.be.undefined;
expect(insertedMiddleware.metatype).to.be.eql(TestMiddleware);
expect(insertedMiddleware!.scope).to.be.undefined;
expect(insertedMiddleware!.metatype).to.be.eql(TestMiddleware);
});
});

View File

@@ -91,7 +91,7 @@ describe('MiddlewareModule', () => {
configureSpy.calledWith(
new MiddlewareBuilder(
(middlewareModule as any).routesMapper,
undefined,
undefined!,
new RouteInfoPathExtractor(new ApplicationConfig()),
),
),

View File

@@ -35,7 +35,7 @@ describe('MiddlewareResolver', () => {
const module = { metatype: { name: '' } } as any;
mockContainer.expects('getMiddlewareCollection').returns(middleware);
await resolver.resolveInstances(module, null);
await resolver.resolveInstances(module, null!);
expect(loadMiddleware.callCount).to.be.equal(middleware.size);
expect(loadMiddleware.calledWith(wrapper as any, middleware, module)).to.be

View File

@@ -22,7 +22,7 @@ describe('NestApplicationContext', () => {
injector,
new GraphInspector(nestContainer),
);
const { moduleRef } = await nestContainer.addModule(class T {}, []);
const { moduleRef } = (await nestContainer.addModule(class T {}, []))!;
nestContainer.addProvider(
{

View File

@@ -20,7 +20,7 @@ describe('PipesContextCreator', () => {
describe('createConcreteContext', () => {
describe('when metadata is empty or undefined', () => {
it('should return empty array', () => {
expect(creator.createConcreteContext(undefined)).to.be.deep.equal([]);
expect(creator.createConcreteContext(undefined!)).to.be.deep.equal([]);
expect(creator.createConcreteContext([])).to.be.deep.equal([]);
});
});
@@ -49,7 +49,7 @@ describe('PipesContextCreator', () => {
expect(creator.getPipeInstance(Pipe)).to.be.eql(wrapper.instance);
});
it('should return null', () => {
sinon.stub(creator, 'getInstanceByMetatype').callsFake(() => null);
sinon.stub(creator, 'getInstanceByMetatype').callsFake(() => null!);
expect(creator.getPipeInstance(Pipe)).to.be.eql(null);
});
});
@@ -59,7 +59,7 @@ describe('PipesContextCreator', () => {
describe('when "moduleContext" is nil', () => {
it('should return undefined', () => {
(creator as any).moduleContext = undefined;
expect(creator.getInstanceByMetatype(null)).to.be.undefined;
expect(creator.getInstanceByMetatype(null!)).to.be.undefined;
});
});
describe('when "moduleContext" is not nil', () => {
@@ -70,7 +70,7 @@ describe('PipesContextCreator', () => {
describe('and when module exists', () => {
it('should return undefined', () => {
sinon.stub(container.getModules(), 'get').callsFake(() => undefined);
expect(creator.getInstanceByMetatype(null)).to.be.undefined;
expect(creator.getInstanceByMetatype(null!)).to.be.undefined;
});
});

View File

@@ -18,14 +18,14 @@ describe('DebugReplFn', () => {
before(async () => {
const container = new NestContainer();
const { moduleRef: aModuleRef } = await container.addModule(
const { moduleRef: aModuleRef } = (await container.addModule(
class ModuleA {},
[],
);
const { moduleRef: bModuleRef } = await container.addModule(
))!;
const { moduleRef: bModuleRef } = (await container.addModule(
class ModuleB {},
[],
);
))!;
container.addController(class ControllerA {}, aModuleRef.token);
container.addProvider(class ProviderA1 {}, aModuleRef.token);

View File

@@ -18,14 +18,14 @@ describe('MethodsReplFn', () => {
before(async () => {
const container = new NestContainer();
const { moduleRef: aModuleRef } = await container.addModule(
const { moduleRef: aModuleRef } = (await container.addModule(
class ModuleA {},
[],
);
const { moduleRef: bModuleRef } = await container.addModule(
))!;
const { moduleRef: bModuleRef } = (await container.addModule(
class ModuleB {},
[],
);
))!;
container.addController(class ControllerA {}, aModuleRef.token);
container.addProvider(class ProviderA1 {}, aModuleRef.token);

View File

@@ -89,7 +89,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTest',
);
)!;
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
@@ -104,7 +104,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTest',
);
)!;
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
@@ -119,7 +119,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTestUsingArray',
);
)!;
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
@@ -134,7 +134,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTestUsingArray',
);
)!;
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
@@ -153,7 +153,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTest',
);
)!;
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
@@ -171,7 +171,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTest',
);
)!;
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
@@ -189,7 +189,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTestUsingArray',
);
)!;
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);
@@ -207,7 +207,7 @@ describe('PathsExplorer', () => {
instance,
instanceProto,
'getTestUsingArray',
);
)!;
expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);

View File

@@ -98,8 +98,8 @@ describe('RouterExecutionContext', () => {
const canActivateFn = contextCreator.createGuardsFn(
[1] as any,
null,
null,
null!,
null!,
);
sinon.stub(contextCreator, 'createGuardsFn').returns(canActivateFn);
tryActivateStub = sinon
@@ -144,9 +144,9 @@ describe('RouterExecutionContext', () => {
} catch (e) {
error = e;
}
expect(error).to.be.instanceOf(ForbiddenException);
expect(error.message).to.be.eql('Forbidden resource');
expect(error.getResponse()).to.be.eql({
expect(error!).to.be.instanceOf(ForbiddenException);
expect(error!.message).to.be.eql('Forbidden resource');
expect(error!.getResponse()).to.be.eql({
statusCode: HttpStatus.FORBIDDEN,
error: 'Forbidden',
message: FORBIDDEN_MESSAGE,
@@ -287,7 +287,7 @@ describe('RouterExecutionContext', () => {
});
describe('createGuardsFn', () => {
it('should throw ForbiddenException when "tryActivate" returns false', async () => {
const guardsFn = contextCreator.createGuardsFn([null], null, null);
const guardsFn = contextCreator.createGuardsFn([null!], null!, null!)!;
sinon.stub(guardsConsumer, 'tryActivate').callsFake(async () => false);
let error: ForbiddenException;
@@ -297,9 +297,9 @@ describe('RouterExecutionContext', () => {
error = e;
}
expect(error).to.be.instanceOf(ForbiddenException);
expect(error.message).to.be.eql('Forbidden resource');
expect(error.getResponse()).to.be.eql({
expect(error!).to.be.instanceOf(ForbiddenException);
expect(error!.message).to.be.eql('Forbidden resource');
expect(error!.getResponse()).to.be.eql({
statusCode: HttpStatus.FORBIDDEN,
message: FORBIDDEN_MESSAGE,
error: 'Forbidden',
@@ -323,7 +323,7 @@ describe('RouterExecutionContext', () => {
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(template);
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,
@@ -339,11 +339,11 @@ describe('RouterExecutionContext', () => {
const response = { render: sinon.spy() };
sinon.stub(contextCreator, 'reflectResponseHeaders').returns([]);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectSse').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns(undefined!);
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,
@@ -391,11 +391,11 @@ describe('RouterExecutionContext', () => {
const response = { redirect: sinon.spy() };
sinon.stub(contextCreator, 'reflectResponseHeaders').returns([]);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectSse').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns(undefined!);
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,
@@ -411,11 +411,11 @@ describe('RouterExecutionContext', () => {
const result = Promise.resolve('test');
const response = {};
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectSse').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns(undefined!);
const handler = contextCreator.createHandleResponseFn(
null,
null!,
false,
undefined,
1234,
@@ -441,11 +441,11 @@ describe('RouterExecutionContext', () => {
const request = new PassThrough();
request.on = sinon.spy();
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns('/');
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,
@@ -461,11 +461,11 @@ describe('RouterExecutionContext', () => {
const response = new PassThrough();
const request = new PassThrough();
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns('/');
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,
@@ -493,11 +493,11 @@ describe('RouterExecutionContext', () => {
const request = new PassThrough();
request.on = sinon.spy();
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined);
sinon.stub(contextCreator, 'reflectRenderTemplate').returns(undefined!);
sinon.stub(contextCreator, 'reflectSse').returns('/');
const handler = contextCreator.createHandleResponseFn(
null,
null!,
true,
undefined,
200,

View File

@@ -72,13 +72,13 @@ describe('RouterExplorer', () => {
exceptionsFilter = new RouterExceptionFilters(
container,
applicationConfig,
null,
null!,
);
routerBuilder = new RouterExplorer(
new MetadataScanner(),
container,
injector,
null,
null!,
exceptionsFilter,
applicationConfig,
routePathFactory,
@@ -99,9 +99,9 @@ describe('RouterExplorer', () => {
];
routerBuilder.applyPathsToRouterProxy(
null,
null!,
paths as any,
null,
null!,
'',
{},
'',
@@ -126,9 +126,9 @@ describe('RouterExplorer', () => {
versioningOptions: { type: VersioningType.URI },
};
routerBuilder.applyPathsToRouterProxy(
null,
null!,
paths as any,
null,
null!,
'',
routePathMetadata,
'1',
@@ -197,7 +197,7 @@ describe('RouterExplorer', () => {
moduleKey,
methodKey,
);
await handler(null, null, null);
await handler(null!, null, null!);
expect(nextSpy.called).to.be.true;
expect(nextSpy.getCall(0).args[0]).to.be.instanceOf(Error);
@@ -229,8 +229,8 @@ describe('RouterExplorer', () => {
expect(
router.applyVersionFilter.calledOnceWithExactly(
handler,
routePathMetadata.methodVersion,
routePathMetadata.versioningOptions,
routePathMetadata.methodVersion!,
routePathMetadata.versioningOptions!,
),
).to.be.true;

View File

@@ -7,7 +7,6 @@ import {
ROUTES,
targetModulesByContainer,
} from '../../router/router-module';
import { FactoryProvider } from '@nestjs/common';
class TestModuleClass {}
@@ -29,7 +28,7 @@ describe('RouterModule', () => {
});
describe('when instantiated', () => {
it('should update the "targetModulesByContainer" weak map', () => {
const moduleRef = new Module(TestModuleClass, new NestContainer(null));
const moduleRef = new Module(TestModuleClass, new NestContainer(null!));
const container = new ModulesContainer([
[TestModuleClass.name, moduleRef],
]);
@@ -45,7 +44,8 @@ describe('RouterModule', () => {
},
]);
expect(targetModulesByContainer.get(container).has(moduleRef)).to.be.true;
expect(targetModulesByContainer.get(container)!.has(moduleRef)).to.be
.true;
});
});
});

View File

@@ -27,7 +27,7 @@ describe('RouterProxy', () => {
const proxy = routerProxy.createProxy((req, res, next) => {
throw httpException;
}, handler);
await proxy(null, null, null);
await proxy(null, null, null!);
expect(nextStub.calledOnce).to.be.true;
expect(
@@ -43,7 +43,7 @@ describe('RouterProxy', () => {
throw httpException;
}, handler);
await proxy(null, null, null);
await proxy(null, null, null!);
expect(nextStub.calledOnce).to.be.true;
expect(
@@ -68,7 +68,7 @@ describe('RouterProxy', () => {
},
handler,
);
await proxy(null, null, null, null);
await proxy(null, null, null, null!);
expect(nextStub.calledOnce).to.be.true;
expect(
@@ -87,7 +87,7 @@ describe('RouterProxy', () => {
handler,
);
await proxy(null, null, null, null);
await proxy(null, null, null, null!);
expect(nextStub.calledOnce).to.be.true;
expect(

View File

@@ -189,7 +189,7 @@ describe('RouterResponseController', () => {
.stub(routerResponseController, 'transformToResult')
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
const result = {};
await routerResponseController.redirect(result, null, null);
await routerResponseController.redirect(result, null, null!);
expect(transformToResultSpy.firstCall.args[0]).to.be.equal(result);
});
it('should pass the response to redirect', async () => {
@@ -198,7 +198,7 @@ describe('RouterResponseController', () => {
.returns(Promise.resolve({ statusCode: 123, url: 'redirect url' }));
const redirectSpy = sinon.spy(adapter, 'redirect');
const response = {};
await routerResponseController.redirect(null, response, null);
await routerResponseController.redirect(null, response, null!);
expect(redirectSpy.firstCall.args[0]).to.be.equal(response);
});
describe('status code', () => {

View File

@@ -46,12 +46,12 @@ describe('DependenciesScanner', () => {
class TestModule {}
@Module({
imports: [undefined],
imports: [undefined!],
})
class UndefinedModule {}
@Module({
imports: [null],
imports: [null!],
})
class InvalidModule {}