Skip to content

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

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

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/waitFor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down