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

♻️ Attach firstElement and messageArray to assert errors #32887

Merged
merged 3 commits into from
Feb 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export class UserError extends Error {

/**
* Throws a provided error if the second argument isn't trueish.
*
* Supports argument substitution into the message via %s placeholders.
*
* Throws an error object that has two extra properties:
* - associatedElement: This is the first element provided in the var args.
* It can be used for improved display of error messages.
* - messageArray: The elements of the substituted message as non-stringified
* elements in an array. When e.g. passed to console.error this yields
* native displays of things like HTML elements.
* @param {Object} errorCls
* @param {T} shouldBeTruthy
* @param {string} opt_message
Expand All @@ -65,16 +74,31 @@ function assertion(errorCls, shouldBeTruthy, opt_message, var_args) {
return shouldBeTruthy;
}

// Skip the first 3 arguments to isolate format params
const messageArgs = Array.prototype.slice.call(arguments, 3);
const messageArray = [];
let firstElement;

// Substitute provided values into format string in message
const message = Array.prototype.slice
// Skip the first 3 arguments to isolate format params
.call(arguments, 3)
.reduce(
(msg, subValue) => msg.replace('%s', subValue),
opt_message || 'Assertion failed'
);
const message = (opt_message || 'Assertion failed').replace(/%s/g, () => {
const subValue = messageArgs.shift();

if (subValue != '') {
messageArray.push(subValue);
}

// If an element is provided, add it to the error object
if (!firstElement && subValue?.nodeType == 1) {
firstElement = subValue;
}

return subValue;
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rsimha Do you know why this might thing these lines were not covered by tests? Every line is indeed covered

Copy link
Contributor

@rsimha rsimha Feb 25, 2021

Choose a reason for hiding this comment

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

Not sure why it claimed zero coverage.

From CI logs, unit test coverage was successfully uploaded for the last commit in this PR: https://app.circleci.com/pipelines/github/ampproject/amphtml/2836/workflows/8eda1669-cac3-4370-a4f3-548652ffaf9f/jobs/32913/parallel-runs/0/steps/0-110

And codecov shows that the lines added by this PR are indeed covered (from running tests after this landed on master): https://codecov.io/gh/ampproject/amphtml/commit/ee8ac1edf18b76d00158ef949587b7e272fdad8a/ (link needs GH login)

Perhaps there was a blip during the PR build's coverage processing? Not sure. Can keep an eye, but can't think of any action items now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless... you changed the code in one commit and added tests in a follow up commit, and these statuses are from the first commit that didn't have the coverage?


throw new errorCls(message);
const error = new errorCls(message);
error.messageArray = messageArray;
error.associatedElement = firstElement;
throw error;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/unit/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ describes.sandboxed('assertions', {}, () => {
`1 a 2 b 3${USER_ERROR_SENTINEL}`
);
});

it('should add element and message info', () => {
const div = document.createElement('div');
let error;
try {
devAssert(false, '%s a %s b %s', div, 2, 3);
} catch (e) {
error = e;
}

expect(error.associatedElement).to.equal(div);
expect(error.messageArray).to.deep.equal([div, 2, 3]);
});
});