微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么代码覆盖率报告中不包含 rxjs pipe() 运算符?

如何解决为什么代码覆盖率报告中不包含 rxjs pipe() 运算符?

问题

rxjs map() 未包含在伊斯坦布尔代码覆盖率报告中,尽管这些行正在测试中。与此相关的其他问题涵盖了 http 测试,这与本文无关。我需要找到一种方法来测试管道或从覆盖率报告中排除 pipe() 内的语句。

说明

我有一家 Akita 实体店,可以查询车辆。根据某些设置,车辆可以使用 frontendLocation(如果存在)或 CurrentLocation(命名不由我决定)。

vehicle.query.ts

export class VehicleQuery extends QueryEntity<VehicleState,Vehicle> {
    /** Returns an observable<number[]>,e.g. [50,50] */
    location$ = this.selectActive().pipe(
        filterNil,map((v) => v.frontendLocation || v.CurrentLocation),// This is shown as not covered 
        distinctUntilChanged(_.isEqual)
    );
    // ...constructor etc.
}

在我的规范文件中,我设置了一个带有 frontendLocation 的活动车辆并测试查询

  1. 获取车辆的frontendLocation(如果存在)
  2. 如果 frontendLocation 不存在,则获取车辆的 CurrentLocation。
describe('VehicleQuery',() => {
    const vehicle = { ...otherAttributes,frontendLocation: [50,50],CurrentLocation: [49,49] };
    let query: VehicleQuery;
    let store: VehicleStore;
    beforeEach(() => {
        Testbed.configureTestingModule({
            providers: [VehicleQuery,VehicleStore],});
        query = Testbed.get(VehicleQuery);
        store = Testbed.get(VehicleStore);
        store.add(vehicle);
        store.setActive(vehicle.Id);
    });
    describe('location',() => {
        it('Returns frontendLocation of current vehicle if it exists',() => {
            query.location$.subscribe((s) => {
                expect(s).toEqual([50,50]);
                expect(s).not.toEqual([49,49]);
            });
        });
        it("Returns CurrentLocation of current vehicle if frontendLocation doesn't exist",() => {
            store.updateActive({ frontendLocation: null });
            query.location$.subscribe((s) => {
                expect(s).toEqual([49,49]);
                expect(s).not.toEqual([50,50]);
            });
        });
    });

这涵盖了查询location$ 管道内的两个选项。两个测试都通过了,但查询第 4 行的 map() 仍然显示为未覆盖。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。