-
Notifications
You must be signed in to change notification settings - Fork 255
Fix unload() to return promise when called without parameters #2666
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
Conversation
Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
MSNev
left a comment
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.
@copilot update the test cases to use the isPromiseLike helper instead of explicitly checking that the result is a function.
Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
|
@copilot
|
…eAsync Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
Fixed TypeScript compilation errors by converting the async tests to use |
MSNev
left a comment
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
rads-1996
left a comment
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
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.
Pull Request Overview
This PR fixes a bug where the unload() method did not return a promise when called without parameters, despite the documentation stating that isAsync defaults to true. The fix follows the maintainer's recommended approach for minimal code size impact.
- Changed the conditional check from
if (isAsync && !unloadComplete)toif (isAsync !== false && !unloadComplete) - Added comprehensive tests to verify the fix works correctly for all parameter combinations
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| AISKU/src/AISku.ts | Fixed unload method to treat undefined isAsync parameter as true by changing condition to isAsync !== false |
| AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts | Added 4 test cases covering all unload parameter combinations and imported isPromiseLike helper |
| import { getGlobal } from "@microsoft/applicationinsights-shims"; | ||
| import { TelemetryContext } from "@microsoft/applicationinsights-properties-js"; | ||
| import { dumpObj, objHasOwnProperty, strSubstring } from "@nevware21/ts-utils"; | ||
| import { dumpObj, isPromiseLike, objHasOwnProperty, strSubstring } from "@nevware21/ts-utils"; |
Copilot
AI
Nov 18, 2025
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.
Unused import strSubstring.
| import { dumpObj, isPromiseLike, objHasOwnProperty, strSubstring } from "@nevware21/ts-utils"; | |
| import { dumpObj, isPromiseLike, objHasOwnProperty } from "@nevware21/ts-utils"; |
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.
Removed unused strSubstring import in commit 7d74430 (before rebase) / fc76d343 (after rebase)
|
@copilot rebase this PR |
Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
Rebased successfully onto latest |
Fix ApplicationInsights.unload to return a promise by default
Issue Summary
The
unloadfunction does not return a promise when called without parameters, even though the documentation states thatisAsyncdefaults to true. The issue is that the function checksif (isAsync && !unloadComplete)which evaluates to false whenisAsyncis undefined.Changes Made
unloadfunction in AISKU/src/AISku.ts to useisAsync !== falseinstead ofisAsyncunload()without parameters returns a promiseisPromiseLikehelper instead of explicit type checkingtestCaseinstead oftestCaseAsyncTechnical Details
Following the maintainer's suggestion, changed line 520 in AISku.ts from:
to:
This ensures that when
isAsyncis undefined (not passed), it is treated as true, thus creating and returning a promise. This approach was specifically requested by the maintainer to avoid adding extra code that TypeScript generates for default parameters.Logic Verification
The change works correctly for all scenarios:
unload()(undefined) - NOW creates and returns a promiseunload(true)- creates and returns a promise (unchanged)unload(false)- does NOT create promise, returns undefined (unchanged)unload(true, callback)- does NOT create promise when callback provided (unchanged)Tests Added
Added 4 comprehensive test cases in
SnippetInitialization.Tests.ts:unload()without parameters returns a promiseunload(true)returns a promiseunload(false)does not return a promise (returns undefined)unload(true, callback)does not return a promise when callback is providedAll tests use the
isPromiseLikehelper for consistency and usetestCaseinstead oftestCaseAsyncsince they only verify the return type without waiting for promise resolution.Files Changed
AISKU/src/AISku.ts- 1 line changed (minimal change as requested)AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts- Tests added using proper test framework patternsSummary
This is a minimal, surgical fix that addresses the reported issue following the maintainer's specific guidance. The tests are now properly structured to avoid TypeScript compilation errors.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.