Skip to content

Commit d240675

Browse files
committed
improve error stack traces
1 parent 2c649a5 commit d240675

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/__tests__/render-async.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,35 @@ test('renderAsync supports legacy rendering option', async () => {
6565
});
6666

6767
test('rerender function throws error when used with renderAsync', async () => {
68-
const result = await renderAsync(<Banana />);
68+
await renderAsync(<Banana />);
6969

70-
expect(() => result.rerender(<Banana />)).toThrowErrorMatchingInlineSnapshot(
71-
`"\`rerender(...)\` is not supported when using \`renderAsync\` use \`await rerenderAsync(...)\` instead"`,
70+
expect(() => screen.rerender(<Banana />)).toThrowErrorMatchingInlineSnapshot(
71+
`""rerender(...)" is not supported when using "renderAsync" use "await rerenderAsync(...)" instead"`,
7272
);
7373
});
7474

7575
test('rerenderAsync function updates component asynchronously', async () => {
7676
const fn = jest.fn();
77-
const result = await renderAsync(<Banana onUpdate={fn} />);
77+
await renderAsync(<Banana onUpdate={fn} />);
7878
expect(fn).toHaveBeenCalledTimes(0);
7979

80-
await result.rerenderAsync(<Banana onUpdate={fn} />);
80+
await screen.rerenderAsync(<Banana onUpdate={fn} />);
8181
expect(fn).toHaveBeenCalledTimes(1);
8282
});
8383

8484
test('unmount function throws error when used with renderAsync', async () => {
85-
const result = await renderAsync(<Banana />);
85+
await renderAsync(<Banana />);
8686

87-
expect(() => result.unmount()).toThrowErrorMatchingInlineSnapshot(
88-
`"\`unmount()\` is not supported when using \`renderAsync\` use \`await unmountAsync()\` instead"`,
87+
expect(() => screen.unmount()).toThrowErrorMatchingInlineSnapshot(
88+
`""unmount()" is not supported when using "renderAsync" use "await unmountAsync()" instead"`,
8989
);
9090
});
9191

9292
test('unmountAsync function unmounts component asynchronously', async () => {
9393
const fn = jest.fn();
94-
const result = await renderAsync(<Banana onUnmount={fn} />);
94+
await renderAsync(<Banana onUnmount={fn} />);
9595

96-
await result.unmountAsync();
96+
await screen.unmountAsync();
9797
expect(fn).toHaveBeenCalled();
9898
});
9999

src/render-async.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { debug } from './helpers/debug';
1414
import { renderWithAsyncAct } from './render-act';
1515
import { setRenderResult } from './screen';
1616
import { getQueriesForElement } from './within';
17+
import { ErrorWithStack } from './helpers/errors';
1718

1819
export interface RenderAsyncOptions {
1920
/**
@@ -63,20 +64,20 @@ function buildRenderResult(
6364
const instance = renderer.root;
6465

6566
const rerender = function (_component: React.ReactElement) {
66-
throw new Error(
67-
'`rerender(...)` is not supported when using `renderAsync` use `await rerenderAsync(...)` instead',
67+
throw new ErrorWithStack(
68+
'"rerender(...)" is not supported when using "renderAsync" use "await rerenderAsync(...)" instead',
69+
rerender,
6870
);
6971
};
7072
const rerenderAsync = async function (component: React.ReactElement) {
7173
// eslint-disable-next-line require-await
72-
await act(async () => {
73-
renderer.update(wrap(component));
74-
});
74+
await act(async () => { renderer.update(wrap(component)); });
7575
};
7676

7777
const unmount = () => {
78-
throw new Error(
79-
'`unmount()` is not supported when using `renderAsync` use `await unmountAsync()` instead',
78+
throw new ErrorWithStack(
79+
'"unmount()" is not supported when using "renderAsync" use "await unmountAsync()" instead',
80+
unmount,
8081
);
8182
};
8283
const unmountAsync = async () => {

0 commit comments

Comments
 (0)