Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions src/internal/testing/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,19 +637,25 @@ export class TestScheduler extends VirtualTimeScheduler {
}

/**
* The `run` method performs the test in 'run mode' - in which schedulers
* used within the test automatically delegate to the `TestScheduler`. That
* is, in 'run mode' there is no need to explicitly pass a `TestScheduler`
* instance to observable creators or operators.
* Delegates all global timer and animation providers to the current {@link TestScheduler} instance.
* Intended for manual usage outside of {@link run}, it enables deterministic control over scheduling
* and time-based operations in tests.
*
* @see {@link /guide/testing/marble-testing}
* Returns a `dispose` function that must be called to restore the original environment.
* Also implements `[Symbol.dispose]` for compatibility with the ECMAScript resource disposal protocol.
*/
run<T>(callback: (helpers: RunHelpers) => T): T {
delegate({
frameTimeFactor = TestScheduler.frameTimeFactor,
maxFrames = Infinity,
}: {
frameTimeFactor?: number;
maxFrames?: number;
} = {}) {
const prevFrameTimeFactor = TestScheduler.frameTimeFactor;
const prevMaxFrames = this.maxFrames;

TestScheduler.frameTimeFactor = 1;
this.maxFrames = Infinity;
TestScheduler.frameTimeFactor = frameTimeFactor;
this.maxFrames = maxFrames;
this.runMode = true;

const animator = this.createAnimator();
Expand All @@ -662,29 +668,56 @@ export class TestScheduler extends VirtualTimeScheduler {
timeoutProvider.delegate = delegates.timeout;
performanceTimestampProvider.delegate = this;

const dispose = () => {
TestScheduler.frameTimeFactor = prevFrameTimeFactor;
this.maxFrames = prevMaxFrames;
this.runMode = false;
animationFrameProvider.delegate = undefined;
dateTimestampProvider.delegate = undefined;
immediateProvider.delegate = undefined;
intervalProvider.delegate = undefined;
timeoutProvider.delegate = undefined;
performanceTimestampProvider.delegate = undefined;
};

return {
dispose,
// @ts-expect-error for compatibility with the ECMAScript resource disposal protocol
[Symbol.dispose ?? 'dispose']: dispose,
animate: animator.animate,
};
}

/**
* The `run` method performs the test in 'run mode' - in which schedulers
* used within the test automatically delegate to the `TestScheduler`. That
* is, in 'run mode' there is no need to explicitly pass a `TestScheduler`
* instance to observable creators or operators.
*
* @see {@link /guide/testing/marble-testing}
*/
run<T>(callback: (helpers: RunHelpers) => T): T {
const delegates = this.delegate({
frameTimeFactor: 1,
maxFrames: Infinity,
});

const helpers: RunHelpers = {
cold: this.createColdObservable.bind(this),
hot: this.createHotObservable.bind(this),
flush: this.flush.bind(this),
time: this.createTime.bind(this),
expectObservable: this.expectObservable.bind(this),
expectSubscriptions: this.expectSubscriptions.bind(this),
animate: animator.animate,
animate: delegates.animate,
};

try {
const ret = callback(helpers);
this.flush();
return ret;
} finally {
TestScheduler.frameTimeFactor = prevFrameTimeFactor;
this.maxFrames = prevMaxFrames;
this.runMode = false;
animationFrameProvider.delegate = undefined;
dateTimestampProvider.delegate = undefined;
immediateProvider.delegate = undefined;
intervalProvider.delegate = undefined;
timeoutProvider.delegate = undefined;
performanceTimestampProvider.delegate = undefined;
delegates.dispose();
}
}
}