-
Notifications
You must be signed in to change notification settings - Fork 226
Remove more instances of as unknown as
#2103
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa5bad6
Remove `as unknown as TextEditor/TextDocument`
koesie10 9b1ca51
Remove `as unknown as WorkspaceFolder`
koesie10 551ed95
Remove `as unknown as QueryHistoryManager`
koesie10 af167c6
Remove `as unknown as DatabaseManager`
koesie10 dd4df01
Remove `as unknown as ExtensionContext`
koesie10 fd2b91d
Remove casting to `QueryHistoryConfig`
koesie10 65e652b
Fix `mockResolvedValue` not working with mocked objects
koesie10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we undefine
thenfor all mocked objects, then their behaviour might be different than their original counterpart.Would we be able to check if the original object is
thenable?then(or allow the user to pass in a mocked method? that might be a stretch)undefinedlike you've done here.What do you think?
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.
I think it would be nice if we could detect whether something is thenable, but unfortunately I don't think we're able to do that. TypeScript doesn't expose the type information at runtime, so we can't get access to the original object. The only way we could do it is by passing in a
booleanthat matches whether the object is thenable or not.However, I don't see us mocking a thenable object anytime soon. The only thenable object which is used in our codebase is
Promiseand this should never be mocked since it can be constructed really easily usingPromise.resolveorPromise.reject. I would be in favour of not supporting this usecase and leaving the code as-is, always returningundefined.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.
It looks like there are a few more cases where we use
Thenableobjects: https://github.com/github/vscode-codeql/search?p=2&q=thenable.Also other libraries we're using could return
Thenableobjects. When we try to mock them, we might not be aware of the behaviour change.Yeah, I'm more thinking how this isn't a visible thing on mock objects and could therefore affect us without us realising it.
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 are references to the
Thenbleinterface which defines that there must be athenmethod on some object. It is not an implementation of this interface and in almost all cases it will be aPromise.If we're mocking other libraries, we should still be using
mockResolvedValuewhich will return aPromise. Even other libraries shouldn't implement a thenable object and should be usingPromise. I don't see a reason for a library to re-implement a promise, other than for compatibility with older JS versions which didn't support promise. However, even in that case we would still be usingmockResolvedValue.I think the risk here is very low, you'd need to be intentionally mocking a
PromiseorThenableusing something likemockedObject<Promise<AnotherObject>>ormockedObject<Thenable<AnotherObject>>. This would only work if you also pass an empty object to themockedObjectmethod since you wouldn't be able to mock any methods ofAnotherObject(those will give type errors).I'm open to suggestions on how we can make this work with promises, but I think it will unnecessarily complicate things because we can't actually detect whether something is thenable and would like to avoid adding another parameter which is always
false.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.
Yeah, I'm not sure I can suggest a better workaround tbh. Just wanted to understand what the implications are, which you've laid out clearly. 👍
I wasn't familiar with the differences between
ThenableandPromise. After reading your explanation, it makes sense that modern libraries would be using Promises.And we do mock promises intentionally to return what we want, so we have an escape hatch.