@@ -17,51 +17,16 @@ import path from 'node:path';
1717import { TestSession } from '@salesforce/cli-plugins-testkit' ;
1818import { PROJECT_PATH } from './utils.js' ;
1919
20- const SESSION_RETRIES = 3 ;
21- const SESSION_RETRY_DELAY_MS = 5000 ;
2220let cachedSession : TestSession ;
2321
24- function isJwtAuthError ( error : unknown ) : boolean {
25- const message = error instanceof Error ? error . message : String ( error ) ;
26- const name = error instanceof Error ? ( error . constructor ?. name ?? '' ) : '' ;
27- return (
28- name . includes ( 'Jwt' ) ||
29- message . includes ( 'JWT' ) ||
30- message . includes ( 'client identifier invalid' ) ||
31- message . includes ( 'audience is invalid' )
32- ) ;
33- }
34-
35- /**
36- * Get or create the shared session. Reuses one TestSession per process so we only stub process.cwd once
37- * (avoids "Attempted to wrap cwd which is already wrapped" when multiple NUT files run in one Mocha process).
38- * JWT auth is flaky hence the retries on create.
39- *
40- * @returns The shared TestSession
41- */
4222export async function getSession ( ) : Promise < TestSession > {
43- if ( cachedSession ) {
44- return cachedSession ;
45- }
46- let lastError : unknown ;
47- for ( let attempt = 1 ; attempt <= SESSION_RETRIES ; attempt ++ ) {
48- try {
49- // eslint-disable-next-line no-await-in-loop
50- cachedSession = await TestSession . create ( {
51- devhubAuthStrategy : 'AUTO' ,
52- project : { sourceDir : PROJECT_PATH } ,
53- } ) ;
54- return cachedSession ;
55- } catch ( error ) {
56- lastError = error ;
57- if ( ! isJwtAuthError ( error ) || attempt === SESSION_RETRIES ) {
58- throw error ;
59- }
60- // eslint-disable-next-line no-await-in-loop
61- await new Promise ( ( resolve ) => setTimeout ( resolve , SESSION_RETRY_DELAY_MS ) ) ;
62- }
23+ if ( ! cachedSession ) {
24+ cachedSession = await TestSession . create ( {
25+ devhubAuthStrategy : 'AUTO' ,
26+ project : { sourceDir : PROJECT_PATH } ,
27+ } ) ;
6328 }
64- throw lastError ;
29+ return new Promise ( ( r ) => r ( cachedSession ) ) ;
6530}
6631
6732export function getComponentPath ( session : TestSession , componentName : string ) {
0 commit comments