1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/utility-features/test-utils/src/run-with-thrown-mobx-reactions.ts
Janne Savolainen b830c8ea67
Introduce test utils for rendering and running with thrown mobx reactions
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2023-03-21 09:24:41 +02:00

39 lines
836 B
TypeScript

import { noop } from "lodash/fp";
import { _resetGlobalState, configure } from "mobx";
export const runWithThrownMobxReactions = (callback: () => void) => {
const originalConsoleWarn = console.warn;
console.warn = noop;
configure({
disableErrorBoundaries: true,
});
console.warn = originalConsoleWarn;
let error: any;
try {
callback();
} catch (e) {
error = e;
} finally {
configure({
disableErrorBoundaries: false,
});
// This is because when disableErrorBoundaries is true, MobX doesn't recover from the thrown
// errors, and its global state starts bleeding between tests making.
_resetGlobalState();
if (!error) {
throw new Error(
"Tried to run with thrown MobX reactions but nothing was thrown"
);
} else {
throw error;
}
}
};