Node.js中TypeScript类的Jest单元测试没有覆盖大部分部分的测试范围
请找到我的节点js代码和相应的单元测试。 但obs.pipe的测试覆盖率显示,所有管线均未覆盖。 请就单元测试提供建议,以涵盖整个代码 这是TypeScript中的RxJSUtilsService类Node.js中TypeScript类的Jest单元测试没有覆盖大部分部分的测试范围,node.js,typescript,unit-testing,rxjs,jestjs,Node.js,Typescript,Unit Testing,Rxjs,Jestjs,请找到我的节点js代码和相应的单元测试。 但obs.pipe的测试覆盖率显示,所有管线均未覆盖。 请就单元测试提供建议,以涵盖整个代码 这是TypeScript中的RxJSUtilsService类 @Injectable() export class RxJSUtilsService { genericRetryStrategy( options: GenericRetryOptions = { maxRetryAttempts: 3, scalingDu
@Injectable()
export class RxJSUtilsService {
genericRetryStrategy(
options: GenericRetryOptions = {
maxRetryAttempts: 3,
scalingDuration: 1000,
excludedStatusCodes: [],
},
): (obs: Observable<any>) => Observable<any> {
return (obs: Observable<any>) =>
obs.pipe(
mergeMap((error, i: number) => {
i++;
if (
i > options.maxRetryAttempts ||
options.excludedStatusCodes.find(e => e === error.status)
) {
return throwError(error);
}
return timer(i * options.scalingDuration);
}),
);
}
}
@可注射()
导出类RxJSUtilsService{
一般系统战略(
选项:GenericRetryOptions={
最大尝试次数:3次,
缩放持续时间:1000,
排除状态代码:[],
},
):(obs:可观察)=>可观察{
返回(obs:可观察)=>
obs管道(
合并映射((错误,i:number)=>{
i++;
如果(
i>options.maxRetryAttempts||
options.excludedStatusCodes.find(e=>e==error.status)
) {
返回投掷器(错误);
}
返回计时器(i*options.scalingDuration);
}),
);
}
}
这是单元测试代码。未涵盖测试覆盖范围,从obs.pipe显示,未涵盖所有管线
describe('RxjsUtilsService', () => {
let rxJSUtilsService: RxJSUtilsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RxJSUtilsService],
}).compile();
rxJSUtilsService = module.get<RxJSUtilsService>(RxJSUtilsService);
});
it('should be defined', () => {
expect(rxJSUtilsService).toBeDefined();
});
it('GenericRetryStrategy should be called', () => {
const source = interval(100);
jest.spyOn(rxJSUtilsService, 'genericRetryStrategy');
source
.pipe(
mergeMap(val => {
return val < 2 ? throwError('divisible by six') : of(val);
}),
retryWhen(
rxJSUtilsService.genericRetryStrategy({
maxRetryAttempts: 10,
scalingDuration: 1000,
}),
),
)
.subscribe({
next: val => {
expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
},
});
});
it('GenericRetryStrategy should throw error', () => {
const source = interval(100);
jest.spyOn(rxJSUtilsService, 'genericRetryStrategy');
source
.pipe(
mergeMap(val => {
return val > 0 ? throwError('divisible by six') : of(val);
}),
retryWhen(
rxJSUtilsService.genericRetryStrategy({
maxRetryAttempts: 2,
scalingDuration: 1000,
}),
),
)
.subscribe({
error: err => {
expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
},
});
});
});
描述('RxjsUtilsService',()=>{
让rxJSUtilsService:rxJSUtilsService;
beforeach(异步()=>{
常量模块:TestingModule=等待测试。createTestingModule({
提供者:[RxJSUtilsService],
}).compile();
rxJSUtilsService=module.get(rxJSUtilsService);
});
它('应该定义',()=>{
expect(rxJSUtilsService).tobededefined();
});
it('应调用GenericRetryStrategy',()=>{
常数源=间隔(100);
jest.spyOn(rxJSUtilsService,“通用系统策略”);
来源
.烟斗(
合并映射(val=>{
返回值val<2?投掷者(‘可被六整除’):of(val);
}),
复述(
rxJSUtilsService.genericRetryStrategy({
最大尝试次数:10次,
缩放持续时间:1000,
}),
),
)
.订阅({
下一步:val=>{
expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
},
});
});
它('GenericRetryStrategy应该抛出错误',()=>{
常数源=间隔(100);
jest.spyOn(rxJSUtilsService,“通用系统策略”);
来源
.烟斗(
合并映射(val=>{
返回val>0?投掷者(“可被六整除”):of(val);
}),
复述(
rxJSUtilsService.genericRetryStrategy({
最大尝试次数:2次,
缩放持续时间:1000,
}),
),
)
.订阅({
错误:err=>{
expect(rxJSUtilsService.genericRetryStrategy).toBeCalled();
},
});
});
});