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

Better errors from visitable #640

Merged
merged 5 commits into from
Mar 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions addon/src/-private/better-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function throwBetterError(node, key, error, { selector } = {}) {
const wrapperError = new PageObjectError(message, {
cause: {
message,
error: error.cause,
key,
node,
selector,
Expand Down
10 changes: 7 additions & 3 deletions addon/src/properties/visitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ function appendQueryParams(path, queryParams) {
* @param {string} path - Full path of the route to visit
* @return {Descriptor}
*
* @throws Will throw an error if dynamic segments are not filled
* @throws Will throw an error if dynamic segments are not filled.
* Note: An error instance may contain a `cause.error` property
* with the original error thrown by an underlying test helper.
*/
export function visitable(path) {
return action(function (dynamicSegmentsAndQueryParams = {}) {
Expand All @@ -138,8 +140,10 @@ export function visitable(path) {

return getAdapter()
.visit(fullPath)
.catch(() => {
throw new Error(`Failed to visit URL '${fullPath}'`);
.catch((e) => {
throw new Error(`Failed to visit URL '${fullPath}': ${e.toString()}`, {
cause: e,
});
});
});
}
12 changes: 11 additions & 1 deletion test-app/tests/unit/-private/properties/visitable-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@ module('visitable', function (hooks) {
} catch (e) {
assert.strictEqual(
e?.toString(),
`Error: Failed to visit URL '/non-existing-url/value'
`Error: Failed to visit URL '/non-existing-url/value': UnrecognizedURLError: /non-existing-url/value

PageObject: 'page.foo("[object Object]")'
Selector: '.scope'`
);

const originalError = (e as any).cause.error;
assert.true(
originalError instanceof Error,
'`cause.error` is an instance of `Error`'
);

assert.strictEqual(originalError.name, 'UnrecognizedURLError');

assert.strictEqual(originalError.message, '/non-existing-url/value');
}
});
});
1 change: 1 addition & 0 deletions test-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "@tsconfig/ember/tsconfig.json",
"compilerOptions": {
"target": "es2022",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering if this was needed for some reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, right... this was needed to make TS understand that Error#cause is a thing. Was getting type errors without it when trying to access .cause.

// The combination of `baseUrl` with `paths` allows Ember's classic package
// layout, which is not resolvable with the Node resolution algorithm, to
// work with TypeScript.
Expand Down