Replies: 1 comment 1 reply
-
|
Hey @wms, I would assume that you're testing that your scoping/linking logic is working correctly and is resolving a reference to a specific AST node correctly. Is that right? For that we have built something like this in our projects in the past for vitest: // TypeScript declaration
declare module 'vitest' {
interface Assertion<T = any> {
toBeAstNode(expected: AstNode): void;
}
}
// Implementation
expect.extend({
toBeAstNode(received: AstNode, expected: AstNode, _options: unknown) {
const { isNot } = this;
return {
pass: (received !== expected) === Boolean(isNot),
message: () => {
const addressExpected = expected
? AstUtils.getDocument(expected).uri.toString() + '#' + sharedServices.workspace.AstNodeLocator.getAstNodePath(expected)
: '<<undefined>>';
const addressReceived = received
? AstUtils.getDocument(expected).uri.toString() + '#' + sharedServices.workspace.AstNodeLocator.getAstNodePath(received)
: '<<undefined>>';
return isNot
? `Received ${addressExpected} but expected another AstNode`
: `Expected ${addressExpected}, but got ${addressReceived}`;
}
}
},
});
// Usage
expect(refTarget).toBeAstNode(expectedTarget);That way, you circumvent the issue completely by generating a completely custom message that uses the AST node path (i.e. a unique identifier for each node of an AST) for the error message. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In my unit tests, I would like to assert that given some AST, the correct Node(s) are returned using something like
expect(aNode).toBe(bNode). When I get my implementation right, the nodes are identical and the tests pass. However, as is more often the case, the assertion will fail. In the process of trying to show me the inequality, my test runner (Vitest in my case, but can imagine Jest would behave in a similar fashion) will crash as it attempts to pretty-print a cyclical structure.Has anybody here had a similar experience and found some way of tweaking the test environment (or Services Module) so that the test runner does a shallower comparison/print-out when comparing AST Nodes?
Beta Was this translation helpful? Give feedback.
All reactions