Skip to content
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

Fix stack traces handling when calling user.* methods #290

Merged
merged 3 commits into from
Nov 9, 2021
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
5 changes: 5 additions & 0 deletions .changeset/two-dingos-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'pleasantest': patch
---

Fix stack frames handling when calling user.\* methods
10 changes: 8 additions & 2 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ const wrapWithForgotAwait = (
for (const key of Object.keys(user) as (keyof PleasantestUser)[]) {
const original = user[key];
// eslint-disable-next-line @cloudfour/unicorn/consistent-function-scoping
const wrapper = (...args: any[]) =>
asyncHookTracker.addHook<any>(() => (original as any)(...args), wrapper);
const wrapper = async (...args: any[]) =>
// The await is necessary so `wrapper` is in the stack trace,
// so captureStackTrace works correctly
// eslint-disable-next-line no-return-await
await asyncHookTracker.addHook<any>(
() => (original as any)(...args),
wrapper,
);

user[key] = wrapper;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/user/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { withBrowser } from 'pleasantest';
import { printErrorFrames } from '../test-utils';

test(
'Stack frames are correct',
withBrowser(async ({ user, utils, screen }) => {
await utils.injectHTML('<button style="visibility:hidden">Hi</button>');
const button = await screen.getByText(/hi/i);
const error = await user.click(button).catch((error) => error);
expect(await printErrorFrames(error)).toMatchInlineSnapshot(`
"Error: Cannot perform action on element that is not visible (it has visibility:hidden):
<button style=\\"visibility:hidden\\">Hi</button>
-------------------------------------------------------
tests/user/general.test.ts

const error = await user.click(button).catch((error) => error);
^
-------------------------------------------------------
dist/cjs/index.cjs"
`);
}),
);