diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts index 063f9b67..39da7cf6 100644 --- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts +++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts @@ -87,6 +87,7 @@ export type InitialEnvironmentState = { initialEnvID: string initialSelectedEnvs: Environment["variables"] initialEnvironmentIndex: SelectedEnvironmentIndex + initialEnvName: string initialEnvs: TestResult["envs"] & { temp: Environment["variables"] } @@ -115,6 +116,9 @@ export const captureInitialEnvironmentState = (): InitialEnvironmentState => { environmentsStore.value.selectedEnvironmentIndex ) + // Capture the initial environment name + const initialEnvName = getCurrentEnvironment().name + // Capture the initial script environment state (the environment passed to scripts) const initialEnvs = getCombinedEnvVariables() const initialEnvsForComparison: TestResult["envs"] = { @@ -127,6 +131,7 @@ export const captureInitialEnvironmentState = (): InitialEnvironmentState => { initialEnvID, initialSelectedEnvs, initialEnvironmentIndex, + initialEnvName, initialEnvs, initialEnvsForComparison, } @@ -489,6 +494,7 @@ export function runRESTRequest$( initialEnvID, initialSelectedEnvs, initialEnvironmentIndex, + initialEnvName, initialEnvs, initialEnvsForComparison, } = captureInitialEnvironmentState() @@ -610,6 +616,7 @@ export function runRESTRequest$( updateEnvsAfterTestScript( combinedResult, initialEnvironmentIndex, + initialEnvName, initialEnvID ) } @@ -666,6 +673,7 @@ export function runRESTRequest$( function updateEnvsAfterTestScript( runResult: E.Right, initialEnvironmentIndex: SelectedEnvironmentIndex, + initialEnvName: string, initialEnvID?: string ) { const globalEnvVariables = updateEnvironments( @@ -696,14 +704,14 @@ function updateEnvsAfterTestScript( variables: selectedEnvVariables, }) } else if (initialEnvironmentIndex.type === "TEAM_ENV") { - const env = getEnvironment({ - type: "TEAM_ENV", - }) + // Use the initial environment name to avoid issues when environment changes during request execution + // adding a fallback to current environment name just in case so it's not null + const envName = initialEnvName ?? getCurrentEnvironment().name pipe( updateTeamEnvironment( JSON.stringify(selectedEnvVariables), initialEnvironmentIndex.teamEnvID, - env.name + envName ) )() } @@ -801,6 +809,7 @@ export function runTestRunnerRequest( initialEnvID, initialSelectedEnvs, initialEnvironmentIndex, + initialEnvName, initialEnvs, initialEnvsForComparison, } = initialEnvironmentState @@ -897,6 +906,7 @@ export function runTestRunnerRequest( updateEnvsAfterTestScript( postRequestScriptResult, initialEnvironmentIndex, + initialEnvName, initialEnvID ) } diff --git a/packages/hoppscotch-common/src/services/test-runner/test-runner.service.ts b/packages/hoppscotch-common/src/services/test-runner/test-runner.service.ts index 9e62aadc..5a7ecf23 100644 --- a/packages/hoppscotch-common/src/services/test-runner/test-runner.service.ts +++ b/packages/hoppscotch-common/src/services/test-runner/test-runner.service.ts @@ -10,7 +10,6 @@ import { cloneDeep } from "lodash-es" import { Ref } from "vue" import { captureInitialEnvironmentState, - InitialEnvironmentState, runTestRunnerRequest, } from "~/helpers/RequestRunner" import { @@ -182,17 +181,13 @@ export class TestRunnerService extends Service { headers: [...inheritedHeaders, ...request.headers], } - // Capture the initial environment state for a test run so that it remains consistent and unchanged when current environment changes - const initialEnvironmentState = captureInitialEnvironmentState() - await this.runTestRequest( tab, finalRequest, collection, options, currentPath, - inheritedVariables, - initialEnvironmentState + inheritedVariables ) if (options.delay && options.delay > 0) { @@ -283,8 +278,7 @@ export class TestRunnerService extends Service { collection: HoppCollection, options: TestRunnerOptions, path: number[], - inheritedVariables: HoppCollectionVariable[] = [], - initialEnvironmentState: InitialEnvironmentState + inheritedVariables: HoppCollectionVariable[] = [] ) { if (options.stopRef?.value) { throw new Error("Test execution stopped") @@ -297,6 +291,9 @@ export class TestRunnerService extends Service { error: undefined, }) + // Capture the initial environment state for a test run so that it remains consistent and unchanged when current environment changes + const initialEnvironmentState = captureInitialEnvironmentState() + const results = await runTestRunnerRequest( request, options.keepVariableValues,