使用 Jest 和 TestScheduler 测试无限可观察量

问题描述 投票:0回答:0

我目前正在为 Angular 项目内部的复杂可观察对象创建测试,但该测试根本不起作用(JavaScript 堆内存不足)。

基本上 Observable 每

n
秒从 REST 服务发出一个新值(轮询率,可以由用户设置)。如果轮询率是
0
Observable 只发射一次。在每次更新 FormControl 时,Observable 都应该重新发射。

我写的基本测试应该简单地检查 Observable 发出(至少)3 次,在两者之间总是有定义的轮询率的延迟。返回值与此测试无关。

可观察的:

// Angular FormControl to set the polling-rate
selectedPollingRateControl = new FormControl<number | null>(null);

healthInformation$ = this.selectedPollingRateControl.valueChanges.pipe(
    switchMap(rate => {
        if (!rate) {
            return of(0);
        }

        return timer(0, rate * 1000);
    }),
    switchMap(() => this.healthInformationService.getHealthInformation())
);

测试:

it('should be reloaded depending on the selected rate', () => {
    const selectedRate = 60;

    const testScheduler = new TestScheduler((actual, expected) => {
        expect(actual).toEqual(expected);
    });

    testScheduler.run(({ expectObservable }) => {
        const expected = `a ${selectedRate}s b ${selectedRate}s (c|)`;

        component.selectedPollingRateControl.setValue(selectedRate);

        expectObservable(component.healthInformation$).toBe(expected);
    });
});

注:

  • component
    引用包含 Observable 和 FormControl 的 Angular 组件
  • 我知道 marble-string 的时间可能还不正确,我会在测试至少失败而不是遇到内存问题时弄清楚

错误的堆栈跟踪:

 RUNS   infrastruktur-test  apps/infrastruktur-test/src/app/health-information/health-information.component.spec.ts

<--- Last few GCs --->

[16556:0000028EC2B9F240]    36546 ms: Scavenge 2039.1 (2078.5) -> 2037.4 (2083.0) MB, 2.7 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;
[16556:0000028EC2B9F240]    36552 ms: Scavenge 2041.2 (2083.0) -> 2037.7 (2083.5) MB, 2.5 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;
[16556:0000028EC2B9F240]    36560 ms: Scavenge 2041.6 (2083.5) -> 2038.3 (2083.5) MB, 3.0 / 0.0 ms  (average mu = 0.312, current mu = 0.258) allocation failure;


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF759741B7F node_api_throw_syntax_error+203775
 2: 00007FF7596C1556 v8::internal::wasm::WasmCode::safepoint_table_offset+63558
 3: 00007FF7596C28C2 v8::internal::wasm::WasmCode::safepoint_table_offset+68530
 4: 00007FF75A1647F4 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF75A14FB52 v8::Isolate::Exit+674
 6: 00007FF759FD1BBC v8::internal::EmbedderStackStateScope::ExplicitScopeForTesting+124
 7: 00007FF759FDEE9D v8::internal::Heap::PublishPendingAllocations+1117
 8: 00007FF759FDBF27 v8::internal::Heap::PageFlagsAreConsistent+3367
 9: 00007FF759FCE657 v8::internal::Heap::CollectGarbage+2039
10: 00007FF759FD6A35 v8::internal::Heap::GlobalSizeOfObjects+341
11: 00007FF75A0260AF v8::internal::StackGuard::HandleInterrupts+863
12: 00007FF759CE7F7F v8::internal::DateCache::Weekday+7327
13: 00007FF75A201E81 v8::internal::SetupIsolateDelegate::SetupHeap+558193
14: 00007FF6DAA18B8B

有人知道如何解决这个测试吗?

angular jestjs rxjs rxjs-test-scheduler
© www.soinside.com 2019 - 2024. All rights reserved.