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

test: add coverage for error apis #12729

Closed
wants to merge 3 commits into from
Closed

Conversation

mhdawson
Copy link
Member

Add coverage for N-API functions related to
throwing and creating errors. A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

Checklist

  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows [commit guidelines]
Affected core subsystem(s)

test, n-api

Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.
@nodejs-github-bot nodejs-github-bot added node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests. labels Apr 28, 2017

error = test_error.createTypeError();
assert.ok(error instanceof TypeError,
'expected error to be an instance of TypeeError');
Copy link
Contributor

Choose a reason for hiding this comment

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

TypeError

@@ -55,3 +55,61 @@ assert.strictEqual(test_error.checkError({}), false,
// Test that non-error primitive is correctly classed
assert.strictEqual(test_error.checkError('non-object'), false,
'Non-error primitive correctly classed by napi_is_error');

try {
test_error.throwExistingError();
Copy link
Contributor

Choose a reason for hiding this comment

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

If throwExistingError() and similar calls below don't actually throw an error, then this test would incorrectly pass, right?

Copy link
Member

Choose a reason for hiding this comment

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

Right. I'd suggest using assert.throws() to verify expected exceptions. It will make cleaner code anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed. assert.throws() is a much better choice here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, changing.

NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message));
NAPI_CALL(env, napi_create_error(env, message, &error));
NAPI_CALL(env, napi_throw(env, error));
return nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

Just noting for the future: it would be excellent if these n-api thrown errors followed the same conventions as the internal/errors module. That is, include a code property and the [{code}]: in the name.

Copy link
Member

Choose a reason for hiding this comment

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

The napi_create_error() and napi_throw_error() and similar APIs are intended to be used by native add-on modules to return and/or throw errors to JavaScript. So, it would be up to the native add-on code to supply meaningful values for the code property. But, N-API could encourage conformance to that convention by providing APIs such as napi_create_error_with_code(), napi_throw_error_with_code(). I'll open a new issue to track that.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@jasnell @cjihrig @jasongin pushed commit to address comments. There are lots of different ways of formatting the assert.throws throughout the existing tests. This seemed the most succinct for this case.

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

LGTM with a nit.


assert.throws(() => {
test_error.throwExistingError();
}, /Error: existing error/);
Copy link
Contributor

Choose a reason for hiding this comment

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

We've been moving toward adding ^ and $ to make the regular expression stricter.

@mhdawson
Copy link
Member Author

mhdawson commented May 3, 2017

Thanks for the review, pushed commit to address comments:

CI run: https://ci.nodejs.org/job/node-test-pull-request/7837/

@mhdawson
Copy link
Member Author

mhdawson commented May 5, 2017

CI good landing.

@mhdawson
Copy link
Member Author

mhdawson commented May 5, 2017

landed as 94a120c

@mhdawson mhdawson closed this May 5, 2017
mhdawson added a commit that referenced this pull request May 5, 2017
Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

PR-URL: #12729
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
anchnk pushed a commit to anchnk/node that referenced this pull request May 6, 2017
Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

PR-URL: nodejs#12729
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@jasnell jasnell mentioned this pull request May 11, 2017
@mhdawson mhdawson deleted the throwCoverage branch June 28, 2017 19:24
gabrielschulhof pushed a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018
Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

PR-URL: nodejs#12729
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Apr 16, 2018
Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

Backport-PR-URL: #19447
PR-URL: #12729
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Apr 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants