From 02af88f3a91eb0c71f11bde400494efdfc8cb546 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Mon, 22 Nov 2021 22:18:52 -0800 Subject: [PATCH] fix: implement better error handling --- src/helpers.js | 75 +++++++++++++++++++++++++++++--------------------- src/pure.ts | 54 ++++++++++++++++++++++++++---------- 2 files changed, 82 insertions(+), 47 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index c62d731..81195cf 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,17 +1,17 @@ import {fireEvent} from "./events"; function jestFakeTimersAreEnabled() { - /* istanbul ignore else */ - if (typeof jest !== 'undefined' && jest !== null) { - return ( - // legacy timers - setTimeout._isMockFunction === true || - // modern timers - Object.prototype.hasOwnProperty.call(setTimeout, 'clock') - ) - } - // istanbul ignore next - return false + /* istanbul ignore else */ + if (typeof jest !== 'undefined' && jest !== null) { + return ( + // legacy timers + setTimeout._isMockFunction === true || + // modern timers + Object.prototype.hasOwnProperty.call(setTimeout, 'clock') + ) + } + // istanbul ignore next + return false } const instance = {current: undefined}; @@ -21,35 +21,46 @@ const instance = {current: undefined}; * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup */ afterEach(() => { - /** - * Behavior should be customizability like it is with `react-testing-library` - * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup - */ - if (instance.current) fireEvent.sigkill(instance.current); - instance.current = undefined; + /** + * Behavior should be customizability like it is with `react-testing-library` + * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup + */ + if (instance.current) fireEvent.sigkill(instance.current); + instance.current = undefined; }) function getCurrentInstance() { - /** - * Worth mentioning that this deviates from the upstream implementation - * of `dom-testing-library`'s `getDocument`, which throws an error whenever - * `window` is not defined. - * - * Admittedly, this is another way that `cli-testing-library` will need to figure out - * the right solution to this problem, since there is no omni-present parent `instance` - * in a CLI like there is in a browser. (although FWIW, "process" might work) - * - * Have ideas how to solve? Please let us know: - * https://github.com/crutchcorn/cli-testing-library/issues/2 - */ - return instance.current + /** + * Worth mentioning that this deviates from the upstream implementation + * of `dom-testing-library`'s `getDocument`, which throws an error whenever + * `window` is not defined. + * + * Admittedly, this is another way that `cli-testing-library` will need to figure out + * the right solution to this problem, since there is no omni-present parent `instance` + * in a CLI like there is in a browser. (although FWIW, "process" might work) + * + * Have ideas how to solve? Please let us know: + * https://github.com/crutchcorn/cli-testing-library/issues/2 + */ + return instance.current } // TODO: Does this need to be namespaced for each test that runs? // That way, we don't end up with a "singleton" that ends up wiped between // parallel tests. function setCurrentInstance(newInstance) { - instance.current = newInstance; + instance.current = newInstance; } -export {jestFakeTimersAreEnabled, setCurrentInstance, getCurrentInstance} +function debounce(func, timeout) { + let timer; + return (...args) => { + clearTimeout(timer); + timer = setTimeout(() => { + // eslint-disable-next-line @babel/no-invalid-this + func.apply(this, args); + }, timeout); + }; +} + +export {jestFakeTimersAreEnabled, setCurrentInstance, getCurrentInstance, debounce} diff --git a/src/pure.ts b/src/pure.ts index 2e6b63d..8dde47b 100644 --- a/src/pure.ts +++ b/src/pure.ts @@ -10,7 +10,8 @@ import {RenderOptions, RenderResult, TestInstance} from '../types/pure' import {_runObservers} from './mutation-observer' import {getQueriesForElement} from './get-queries-for-instance' import {getFireEventForElement} from './events' -import {setCurrentInstance} from "./helpers"; +import {debounce, setCurrentInstance} from "./helpers"; +import {getConfig} from "./config"; async function render( command: string, @@ -47,39 +48,62 @@ async function render( set stdoutStr(val: string) {}, } + /** + * This method does not throw errors after-the-fact, + * meaning that if post-render errors are thrown, + * they will be missed + * + * THINK: Should we should simply `throw` when this occurs? + * What about cleanup? + * What about interactive errors? + */ + let _errorHasOccured = false; + const _errors: Array = []; + exec.stdout.on('data', (result: string | Buffer) => { const resStr = stripFinalNewline(result as string); execOutputAPI.stdoutArr.push(resStr) _runObservers() - if (_readyPromiseInternals && !_isReadyResolved) { + if (!_errorHasOccured && _readyPromiseInternals && !_isReadyResolved) { _readyPromiseInternals.resolve() _isReadyResolved = true; } + // stdout might contain relevant error info + if (_errorHasOccured && _readyPromiseInternals && !_isReadyResolved) { + _errors.push(result); + } }) + const _onError = () => { + if (!_readyPromiseInternals) return; + _readyPromiseInternals.reject(new Error(_errors.join('\n'))) + _isReadyResolved = true; + } + + const config = getConfig() + // TODO: Migrate to new config option? + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call + const _throttledOnError = debounce(_onError, config.asyncUtilTimeout); + exec.stdout.on('error', result => { if (_readyPromiseInternals && !_isReadyResolved) { - _readyPromiseInternals.reject(result) - _isReadyResolved = true; + _errors.push(result); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call + _throttledOnError(); + _errorHasOccured = true; } }) exec.stderr.on('data', (result: string) => { if (_readyPromiseInternals && !_isReadyResolved) { - /** - * TODO: We're getting an error where "result" is only the first line of many drawn. - * Let's go ahead and set a timeout var in `config.js` and debounce the rejection - * - * Then, we'll set a boolean to not do any other kind of logging to stdout (or promise resolve) - * until that timeout/debounce has expired. - * - * Also merge with the `'error'` field above - */ - _readyPromiseInternals.reject(new Error(result)) - _isReadyResolved = true; + _errors.push(result); + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call + _throttledOnError(); + _errorHasOccured = true; } }) + // TODO: Replace with `debug()` function if (opts.debug) { exec.stdout.pipe(process.stdout) } -- knotmirror 0.0.1