Skip to content

Commit 3f29d33

Browse files
chore: fix setup
1 parent 924d3d5 commit 3f29d33

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

test/nuts/shared-setup.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { join } from 'node:path';
1818
import { readFileSync, writeFileSync } from 'node:fs';
1919
import { TestSession } from '@salesforce/cli-plugins-testkit';
20-
import { Connection, Org, User, UserFields } from '@salesforce/core';
20+
import { Connection, Org, User, UserFields, StateAggregator } from '@salesforce/core';
2121
import { ComponentSetBuilder } from '@salesforce/source-deploy-retrieve';
2222
import { genUniqueString } from '@salesforce/cli-plugins-testkit';
2323
import { expect } from 'chai';
@@ -84,15 +84,30 @@ export async function initializeSharedContext(): Promise<SharedTestContext> {
8484
}
8585
console.log(`Testing with username: ${username}`);
8686
} else {
87-
// Get devhub org from session
88-
const hubOrgInfo = session.orgs.get('hub');
89-
if (!hubOrgInfo?.username) {
90-
throw new Error('Failed to get devhub username from TestSession. Please ensure a devhub is authenticated.');
87+
// Get devhub org from StateAggregator (config)
88+
try {
89+
const stateAggregator = await StateAggregator.getInstance();
90+
const orgs = stateAggregator.orgs.getAll();
91+
92+
// Find the devhub org (has isDevHub: true)
93+
const devHubOrg = orgs.find((org) => org.isDevHub === true);
94+
95+
if (!devHubOrg?.username) {
96+
throw new Error('No devhub org found in config.');
97+
}
98+
99+
username = devHubOrg.username;
100+
defaultOrg = await Org.create({ aliasOrUsername: username });
101+
connection = defaultOrg.getConnection();
102+
console.log(`Using devhub for testing. Username: ${username}`);
103+
} catch (error) {
104+
throw new Error(
105+
'Failed to get authenticated devhub org. ' +
106+
'Please ensure a devhub is authenticated by running: sf org login web --alias <alias> --instance-url <url> --set-default-dev-hub ' +
107+
'or set TESTKIT_ORG_USERNAME environment variable to use a specific org.\n' +
108+
`Original error: ${error instanceof Error ? error.message : String(error)}`
109+
);
91110
}
92-
username = hubOrgInfo.username;
93-
defaultOrg = await Org.create({ aliasOrUsername: username });
94-
connection = defaultOrg.getConnection();
95-
console.log(`Using devhub for testing. Username: ${username}`);
96111
}
97112

98113
// assign the EinsteinGPTPromptTemplateManager to the scratch org admin user

0 commit comments

Comments
 (0)