Uh oh!
There was an error while loading. Please reload this page.
lib: added isNativeError check to assert.js - #51250
Conversation
NiharPhansalkar
commented
Dec 21, 2023
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. |
apapirovski
commented
Dec 22, 2023
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 |
NiharPhansalkar
commented
Dec 22, 2023
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
2d3421cCompareNiharPhansalkar
commented
Dec 24, 2023
@apapirovski Thank you so much for everything |
BridgeAR
left a comment
There was a problem hiding this comment.
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:
constcontext=vm.createContext();consterror=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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
NiharPhansalkar
commented
Dec 28, 2023
@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
1de2680Compare
BridgeAR
left a comment
There was a problem hiding this comment.
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.
| 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.
| }elseif(isNativeError(message)||messageinstanceofError){ | |
| }elseif(isError(message)){ |
| validateFunction, | ||
| } = require('internal/validators'); | ||
| const { fileURLToPath } = require('internal/url'); | ||
| const { isNativeError } = internalBinding('types'); |
There was a problem hiding this comment.
| const { isNativeError } = internalBinding('types'); |
| assert.ok(a.AssertionError.prototype instanceof Error, | ||
| 'a.AssertionError instanceof Error'); | ||
| 'a.AssertionError instanceof Error'); |
There was a problem hiding this comment.
| '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.
| // Thrown error should be the passed through error instance of the native error | |
| { | |
| constcontext=vm.createContext(); | |
| consterror=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.
| // 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 |
nodejs-github-bot
commented
May 4, 2024
nodejs-github-bot
commented
May 7, 2024
aduh95
commented
May 11, 2024
Linter is failing with |
Ceres6
commented
Nov 6, 2024
Closed by #54776 |
Added the function for compliance with
frameworks such as Jest
Fixes: #50780