fix: display stack trace and code frame for findBy error (#581)

Inspired by https://github.com/testing-library/dom-testing-library/blob/master/src/wait-for.js
This commit is contained in:
Matthieu Gicquel
2020-12-09 12:05:03 +01:00
committed by GitHub
parent 45ee867882
commit b0244d9451
2 changed files with 15 additions and 1 deletions
+7
View File
@@ -39,6 +39,13 @@ export const createQueryByError = (error: Error, callsite: Function) => {
throw new ErrorWithStack(error.message, callsite);
};
export function copyStackTrace(target: Error, stackTraceSource: Error) {
target.stack = stackTraceSource.stack.replace(
stackTraceSource.message,
target.message
);
}
const warned = {};
export function printDeprecationWarning(functionName: string) {
+8 -1
View File
@@ -2,7 +2,11 @@
import * as React from 'react';
import act from './act';
import { throwRemovedFunctionError } from './helpers/errors';
import {
ErrorWithStack,
throwRemovedFunctionError,
copyStackTrace,
} from './helpers/errors';
const DEFAULT_TIMEOUT = 4500;
const DEFAULT_INTERVAL = 50;
@@ -26,10 +30,13 @@ function waitForInternal<T>(
const timeout = options?.timeout ?? DEFAULT_TIMEOUT;
const interval = options?.interval ?? DEFAULT_INTERVAL;
const startTime = Date.now();
// Being able to display a useful stack trace requires generating it before doing anything async
const stackTraceError = new ErrorWithStack('STACK_TRACE_ERROR', waitFor);
return new Promise((resolve, reject) => {
const rejectOrRerun = (error) => {
if (Date.now() - startTime >= timeout) {
copyStackTrace(error, stackTraceError);
reject(error);
return;
}