Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Fix ApplicationInsights.unload to return a promise by default

Issue Summary

The unload function does not return a promise when called without parameters, even though the documentation states that isAsync defaults to true. The issue is that the function checks if (isAsync && !unloadComplete) which evaluates to false when isAsync is undefined.

Changes Made

  • Fix the unload function in AISKU/src/AISku.ts to use isAsync !== false instead of isAsync
  • Add comprehensive tests to verify that unload() without parameters returns a promise
  • Verify the fix matches the maintainer's suggested approach (minimal code size impact)
  • Manual verification of the logic
  • Updated tests to use isPromiseLike helper instead of explicit type checking
  • Fixed TypeScript compilation errors by using testCase instead of testCaseAsync
  • Removed unused import

Technical Details

Following the maintainer's suggestion, changed line 520 in AISku.ts from:

if (isAsync && !unloadComplete) {

to:

if (isAsync !== false && !unloadComplete) {

This ensures that when isAsync is 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 promise
  • unload(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:

  1. unload() without parameters returns a promise
  2. unload(true) returns a promise
  3. unload(false) does not return a promise (returns undefined)
  4. unload(true, callback) does not return a promise when callback is provided

All tests use the isPromiseLike helper for consistency and use testCase instead of testCaseAsync since 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 patterns

Summary

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

This section details on the original issue you should resolve

<issue_title>[BUG] ApplicationInsights.unload does not return a promise by default</issue_title>
<issue_description>Description/Screenshot

According to documentation, unload takes an isAsync param which is true by default. However the function does not return a promise unless isAsync is explicitly set to true

Steps to Reproduce

const promise = appInsights.unload(true); will return a promise object, whereas const promise = appInsights.unload(); returns undefined

  • OS/Browser: MacOS, React Native
  • SDK Version [e.g. 22]: 3.3.10
  • How you initialized the SDK:

Expected behavior

const promise = appInsights.unload(); should return a promise object

Additional context
In the distributed js code, the unload function is declared thusly:

_self.unload = function (isAsync, unloadComplete, cbTimeout) {
                    var unloadDone = false;
                    var result;
                    if (isAsync && !unloadComplete) {
                        result = createPromise(function (resolve) {
                            unloadComplete = resolve;
                        });
                    }
                    function _unloadCallback(unloadState) {
                        if (!unloadDone) {
                            unloadDone = true;
                            _initDefaults();
                            unloadComplete && unloadComplete(unloadState);
                        }
                    }
                    _self[_DYN_ONUNLOAD_FLUSH ](isAsync);
                    _removePageEventHandlers();
                    _core.unload && _core.unload(isAsync, _unloadCallback, cbTimeout);
                    return result;
                };

similar functions elsewhere in the code have a line such as if (isAsync === void 0) { isAsync = true; } to set isAsync to true, but unload does not do this and therefore returns undefined.</issue_description>

Comments on the Issue (you are @copilot in this section)

@MSNev Lets see how Co-Pilot fixes this.... There is an optimal way (from a code size perspective) by just changing

if (isAsync && !unloadComplete) {

to

if (isAsync !== false && !unloadComplete) {

As adding a default causes TypeScript to add lots of additional code</comment_new>


💡 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.

Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unload method to return a promise by default Fix unload() to return promise when called without parameters Nov 14, 2025
Copilot AI requested a review from MSNev November 14, 2025 17:41
Copy link
Collaborator

@MSNev MSNev left a 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>
Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

@copilot update the test cases to use the isPromiseLike helper instead of explicitly checking that the result is a function.

Updated all test cases to use the isPromiseLike helper instead of explicit type checking in commit b09c34f

Copilot AI requested a review from MSNev November 14, 2025 18:19
@MSNev
Copy link
Collaborator

MSNev commented Nov 18, 2025

@copilot
Ts-Plugin All Done - [object Array]: [{time: 7.473,isSuccess: false,errors: ["AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1024,17): error TS2322: Type '() => void | IPromise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.\r","AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1040,17): error TS2322: Type '() => void | IPromise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.\r","AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1065,17): error TS2322: Type '() => Promise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.\r","AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1077,29): error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?"]}]

  • AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1024,17): error TS2322: Type '() => void | IPromise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.
  • AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1040,17): error TS2322: Type '() => void | IPromise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.
  • AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1065,17): error TS2322: Type '() => Promise' is not assignable to type '(testContext?: ITestContext, pollingDoneCallback?: () => void) => boolean | void | StepResult'.
  • AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1077,29): error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?
    Warning: Task "ts:aisku-tests" failed.� Use --force to continue.

…eAsync

Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 18, 2025

@copilot
Ts-Plugin All Done - [object Array]: [{time: 7.473,isSuccess: false,errors: ["AISKU/Tests/Unit/src/SnippetInitialization.Tests.ts(1024,17): error TS2322: Type '() => void | IPromise<ITelemet...

Fixed TypeScript compilation errors by converting the async tests to use testCase instead of testCaseAsync. The tests now properly verify that unload() returns a promise-like object without returning promises from the test functions themselves, matching the pattern used in other similar tests (e.g., flush tests in Sender.tests.ts). Fixed in commit dc6afd7

Copy link
Collaborator

@MSNev MSNev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MSNev MSNev marked this pull request as ready for review November 18, 2025 18:49
@MSNev MSNev requested a review from a team as a code owner November 18, 2025 18:49
@MSNev MSNev enabled auto-merge (squash) November 18, 2025 18:49
Copy link
Member

@rads-1996 rads-1996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI review requested due to automatic review settings November 18, 2025 18:55
Copy link
Contributor

Copilot AI left a 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) to if (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";
Copy link

Copilot AI Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import strSubstring.

Suggested change
import { dumpObj, isPromiseLike, objHasOwnProperty, strSubstring } from "@nevware21/ts-utils";
import { dumpObj, isPromiseLike, objHasOwnProperty } from "@nevware21/ts-utils";

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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)

@MSNev
Copy link
Collaborator

MSNev commented Nov 21, 2025

@copilot rebase this PR

Co-authored-by: MSNev <54870357+MSNev@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot rebase this PR

Rebased successfully onto latest origin/main (223d8c2). The branch now includes the 2 new commits that were merged to main after this PR was created. However, I cannot force-push the rebased branch due to authentication constraints - the rebase is complete locally and the history has been updated to include commits: "Add a check to prevent non-string URLs..." (#2667) and "Fix static web CDN test assertion..." (#2670).

Copilot AI requested a review from MSNev November 21, 2025 22:57
@MSNev MSNev disabled auto-merge November 26, 2025 23:55
@MSNev MSNev enabled auto-merge (squash) November 26, 2025 23:55
@MSNev MSNev merged commit 9201907 into main Nov 27, 2025
15 checks passed
@MSNev MSNev deleted the copilot/fix-unload-promise-return branch November 27, 2025 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ApplicationInsights.unload does not return a promise by default

4 participants