Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Fix spurious infinite timer issue #12174

Merged
merged 4 commits into from Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,9 @@
{
"type": "none",
"comment": "Add mock for Async to prevent spurious infinite timer issue.",
"packageName": "office-ui-fabric-react",
"email": "jagore@microsoft.com",
"commit": "5c78a6866573bcfa7096bc625c795c3205d69e89",
"dependentChangeType": "patch",
"date": "2020-03-03T23:35:01.016Z"
}
@@ -0,0 +1,33 @@
export * from '@uifabric/utilities';
import { Async } from '@uifabric/utilities';

// Known issue with jest's runAllTimers and debounce implementations resulting in
// "Ran 100000 timers, and there are still more! Assuming we've hit an infinite recursion and bailing out..."
// https://github.com/facebook/jest/issues/3465
// Mock impl inspired from issue.
class MockAsync extends Async {
public debounce(callback: Function, timeout: number) {
let timeoutId: number | null = null;
const debounced = (...args: any[]) => {
if (timeoutId) {
clearTimeout(timeoutId);
JasonGore marked this conversation as resolved.
Show resolved Hide resolved
}
// Callback functions throughout repo aren't binding properly, so we have to access
// Async's private _parent member and invoke callbacks the same way Async.debounce does.
const invokeFunction = () => callback.apply((this as any)._parent, args);
timeoutId = setTimeout(invokeFunction, timeout);
};

const cancel = () => {
if (timeoutId) {
clearTimeout(timeoutId);
}
};

(debounced as any).cancel = cancel;

return debounced as any;
}
}

export { MockAsync as Async };