You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As mentioned here I think an auto exposing feature would be nice. I already have implemented such a feature but its adapted to our needs. Also its quite new and we havent used it much. For others: Use carefully (or not at all if you dont know what this is doing or why you might need it).
import'reflect-metadata';importtype{Type}from'@nestjs/common';import{PrismaService}from'#src/modules/prisma/services/prisma.service.js';import{typeSociableTestBedBuilder,TestBed}from'@suites/unit';import{ClsServiceasNestClsService}from'nestjs-cls';/** * Builds a sociable TestBed with transitive dependencies exposed up to boundaries. * Defaults: * - PrismaService is a stop boundary (kept mocked but reachable) * - NestClsService is excluded (kept mocked to avoid AsyncLocalStorage requirements) * * You can explicitly include either to override these defaults. */exportfunctionsociableAutoExpose<T>(sut: Type<T>,options?: {exclude?: Type<unknown>[];include?: Type<unknown>[];stopAt?: Type<unknown>[];},): SociableTestBedBuilder<T>{constdefaultStopAt=newSet<Type<unknown>>([PrismaService]);constdefaultExclude=newSet<Type<unknown>>([NestClsService]);constinclude=newSet<Type<unknown>>(options?.include??[]);if(include.has(PrismaService))defaultStopAt.delete(PrismaService);if(include.has(NestClsService))defaultExclude.delete(NestClsService);constmergedStopAt=newSet<Type<unknown>>([
...(options?.stopAt??[]),
...defaultStopAt,]);constmergedExclude=newSet<Type<unknown>>([
...(options?.exclude??[]),
...defaultExclude,]);consttoExpose=collectDepsTransitively(sut,{exclude: Array.from(mergedExclude),stopAt: Array.from(mergedStopAt),});for(constincofinclude){if(!toExpose.includes(inc))toExpose.push(inc);}if(toExpose.length===0){thrownewError(`sociableAutoExpose: No dependencies to expose were discovered for ${sut.name}. `+`Provide 'include' with at least one dependency, or adjust 'stopAt'/'exclude'.`,);}// Initial expose() needed! TS doesnt infer SociableTestBedBuilder<T> otherwiseletbuilder=TestBed.sociable(sut).expose(toExpose[0]);for(leti=1;i<toExpose.length;i++){builder=builder.expose(toExpose[i]);}returnbuilder;}/** * Collect constructor dependency classes transitively using design:paramtypes metadata. * Traversal stops when encountering any class in the stopAt set (these are boundaries to mock). * The SUT itself is not included in the returned list. */functioncollectDepsTransitively<T>(root: Type<T>,options?: {exclude?: Type<unknown>[];stopAt?: Type<unknown>[];},): Type<unknown>[]{conststopAt=newSet<Type<unknown>>(options?.stopAt??[]);constexclude=newSet<Type<unknown>>(options?.exclude??[]);consttraversed=newSet<Type<unknown>>();consttoTraverse: Type<unknown>[]=[root];while(toTraverse.length){constcurrentType=toTraverse.pop()!;if(traversed.has(currentType))continue;traversed.add(currentType);constdesignParamTypes: unknown=Reflect.getMetadata('design:paramtypes',currentType,);constparamTypes=(designParamTypes??[])asType<unknown>[];for(constdependencyTypeofparamTypes){if(typeofdependencyType!=='function')continue;if(stopAt.has(dependencyType))continue;if(exclude.has(dependencyType))continue;toTraverse.push(dependencyType);}}traversed.delete(root);for(constexofexclude){traversed.delete(ex);}returnArray.from(traversed);}
Yes with this we come really close to an integration test. But in our test setup we only test integration of modules and just the results. We dont have integration tests for services, resolvers, controllers, repositories, ... Our integration tests can be seen as a lightweight e2e test for a module testing only that module isolated. Tests always call resolver/controller function.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As mentioned here I think an auto exposing feature would be nice. I already have implemented such a feature but its adapted to our needs. Also its quite new and we havent used it much. For others: Use carefully (or not at all if you dont know what this is doing or why you might need it).
Usage:
Yes with this we come really close to an integration test. But in our test setup we only test integration of modules and just the results. We dont have integration tests for services, resolvers, controllers, repositories, ... Our integration tests can be seen as a lightweight e2e test for a module testing only that module isolated. Tests always call resolver/controller function.
Beta Was this translation helpful? Give feedback.
All reactions