-
Notifications
You must be signed in to change notification settings - Fork 30k
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
lib: added isNativeError check to assert.js #51250
Conversation
I want to ask what kinds of tests can be written for this fix? The actual problem was occuring for frameworks such as Jest. Native code works just fine. |
Look for how other tests that utilize isNativeError are testing this behavior, which will give you a hint on how to create an Error that doesn't pass an instanceof check but passes isNativeError check. Here's one helpful link: node/test/parallel/test-util.js Line 159 in aa7de74
|
The test for this is already being covered in the following: Where if the code is now run with Jest, instead of an Do I still need to add any tests? |
You need a test in Node's test suite that would fail w/ the old version of the code and succeed w/ the new one. Right now that doesn't exist. Running it with Jest isn't relevant since we don't run our test suite w/ Jest. Writing tests is quite straightforward: https://github.com/nodejs/node/blob/main/doc/contributing/writing-tests.md |
I am a little confused @apapirovski. If you go through the discussion of the issue, you can see that the problem was never with NodeJS directly. The code was working fine when running with node even without the change I have made. The problem was occuring with frameworks such as Jest who have some issue with the |
Tests for Node.js are meant to reproduce the issue that we saw in another library. Here's a line of code that would fail on the Node.js before this change and one that will pass now: assert.ok(false, new (context('Error'))('Custom Error')) Pre-and-post output looks like this:
Post:
|
ae91ee2
to
2d3421c
Compare
@apapirovski Thank you so much for everything |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are all passing in already released Node.js versions and therefore indicate that it's not testing the right thing.
What we need is something like this:
const context = vm.createContext();
const error = vm.runInContext('new TypeError("foo")', context);
assert.throws(() => assert(false, error), {
message: 'foo',
name: 'TypeError'
});
Please always try to focus on what case is not yet covered and how to recreate that situation.
@BridgeAR I will make the changes, however can you please tell me if I am able to understand the reason for this? My current commit, it checks for e to be an Do I understand? |
Added the function for compliance with frameworks such as Jest Fixes: nodejs#50780
Added tests to check if any previous behaviour where instanceof was not throwing a custom error is working with the isNativeError change in assert.ok()
Changed the tests to check for correct throwing of errors and combined if/else into one in assert.js to make code concise.
2d3421c
to
1de2680
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I left some suggestions to improve the code. We already have a utility function for testing this and we already use it in the file and one test case is redundant.
@@ -393,7 +394,7 @@ function innerOk(fn, argLen, value, message) { | |||
} else if (message == null) { | |||
generatedMessage = true; | |||
message = getErrMessage(message, fn); | |||
} else if (message instanceof Error) { | |||
} else if (isNativeError(message) || message instanceof Error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else if (isNativeError(message) || message instanceof Error) { | |
} else if (isError(message)) { |
@@ -74,6 +74,7 @@ const { | |||
validateFunction, | |||
} = require('internal/validators'); | |||
const { fileURLToPath } = require('internal/url'); | |||
const { isNativeError } = internalBinding('types'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const { isNativeError } = internalBinding('types'); |
@@ -38,7 +38,7 @@ const start = 'Expected values to be strictly deep-equal:'; | |||
const actExp = '+ actual - expected'; | |||
|
|||
assert.ok(a.AssertionError.prototype instanceof Error, | |||
'a.AssertionError instanceof Error'); | |||
'a.AssertionError instanceof Error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'a.AssertionError instanceof Error'); | |
'a.AssertionError instanceof Error'); |
// Thrown error should be the passed through error instance of the native error | ||
{ | ||
const context = vm.createContext(); | ||
const error = vm.runInContext('new TypeError("custom error")', context); | ||
|
||
assert.throws(() => assert(false, error), { | ||
message: 'custom error', | ||
name: 'TypeError' | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Thrown error should be the passed through error instance of the native error | |
{ | |
const context = vm.createContext(); | |
const error = vm.runInContext('new TypeError("custom error")', context); | |
assert.throws(() => assert(false, error), { | |
message: 'custom error', | |
name: 'TypeError' | |
}); | |
} |
}); | ||
} | ||
|
||
// Thrown error should be the passed through error instance of the native error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Thrown error should be the passed through error instance of the native error | |
// Errors created in different contexts are handled as any other custom error |
Linter is failing with |
Closed by #54776 |
Added the function for compliance with
frameworks such as Jest
Fixes: #50780