@@ -76,62 +76,65 @@ export async function runAllTests(config) {
7676 }
7777}
7878
79- if ( process . env . CONFIG_FILE ) {
80- // If a config file is provided, read it and parse it
81- const configFilePath = path . resolve ( process . cwd ( ) , process . env . CONFIG_FILE ) ;
82- process . env . CONFIG = await fs . readFile ( configFilePath , 'utf8' ) ;
83- }
84-
85- if ( ! process . env . CONFIG ) {
86- // If no config file is provided, check if the config is passed as an environment variable
87- console . error ( 'No CONFIG or CONFIG_FILE environment variable provided. Exiting.' ) ;
88- process . exit ( 1 ) ;
89- }
79+ // Check if this file is being run directly
80+ if ( import . meta. url === `file://${ process . argv [ 1 ] } ` ) {
81+ if ( process . env . CONFIG_FILE ) {
82+ // If a config file is provided, read it and parse it
83+ const configFilePath = path . resolve ( process . cwd ( ) , process . env . CONFIG_FILE ) ;
84+ process . env . CONFIG = await fs . readFile ( configFilePath , 'utf8' ) ;
85+ }
9086
91- try {
92- parse ( process . env . CONFIG ) ;
93- } catch ( error ) {
94- console . error ( 'Invalid YAML configuration provided:' , error . message ) ;
95- process . exit ( 1 ) ;
96- }
87+ if ( ! process . env . CONFIG ) {
88+ // If no config file is provided, check if the config is passed as an environment variable
89+ console . error ( 'No CONFIG or CONFIG_FILE environment variable provided. Exiting.' ) ;
90+ process . exit ( 1 ) ;
91+ }
9792
98- // Check if the configuration is valid
99- if ( ! process . env . BASE_URL ) {
100- console . error ( 'Environment variable BASE_URL is required' ) ;
101- process . exit ( 1 ) ;
102- }
103- if ( ! process . env . AUTH_HEADER ) {
104- console . error ( 'Environment variable AUTH_HEADER is required' ) ;
105- process . exit ( 1 ) ;
106- }
93+ try {
94+ parse ( process . env . CONFIG ) ;
95+ } catch ( error ) {
96+ console . error ( 'Invalid YAML configuration provided:' , error . message ) ;
97+ process . exit ( 1 ) ;
98+ }
10799
108- // Run the tests by default when the file is imported
109- runAllTests (
110- {
111- baseURL : process . env . BASE_URL ,
112- authHeader : process . env . AUTH_HEADER ,
113- ...parse ( process . env . CONFIG ) ,
100+ // Check if the configuration is valid
101+ if ( ! process . env . BASE_URL ) {
102+ console . error ( 'Environment variable BASE_URL is required' ) ;
103+ process . exit ( 1 ) ;
114104 }
115- ) . then ( ( result ) => {
116- if ( ! result . success ) {
105+ if ( ! process . env . AUTH_HEADER ) {
106+ console . error ( 'Environment variable AUTH_HEADER is required' ) ;
117107 process . exit ( 1 ) ;
118108 }
119- } ) . catch ( ( error ) => {
120- console . error ( 'Error running tests:' , error ) ;
121- process . exit ( 1 ) ;
122- } ) . finally ( ( ) => {
123- if ( process . env . HAR_FILE_NAME ) {
124- // Make sure the HAR file has the correct extension
125- let harFileName = process . env . HAR_FILE_NAME ;
126- if ( ! harFileName . toLowerCase ( ) . endsWith ( '.har' ) ) {
127- harFileName += '.har' ;
109+
110+ // Run the tests by default when the file is imported
111+ runAllTests (
112+ {
113+ baseURL : process . env . BASE_URL ,
114+ authHeader : process . env . AUTH_HEADER ,
115+ ...parse ( process . env . CONFIG ) ,
128116 }
117+ ) . then ( ( result ) => {
118+ if ( ! result . success ) {
119+ process . exit ( 1 ) ;
120+ }
121+ } ) . catch ( ( error ) => {
122+ console . error ( 'Error running tests:' , error ) ;
123+ process . exit ( 1 ) ;
124+ } ) . finally ( ( ) => {
125+ if ( process . env . HAR_FILE_NAME ) {
126+ // Make sure the HAR file has the correct extension
127+ let harFileName = process . env . HAR_FILE_NAME ;
128+ if ( ! harFileName . toLowerCase ( ) . endsWith ( '.har' ) ) {
129+ harFileName += '.har' ;
130+ }
129131
130- console . log ( 'Writing HAR file:' , harFileName ) ;
132+ console . log ( 'Writing HAR file:' , harFileName ) ;
131133
132- writeHarFile ( harFileName ) ;
133- }
134+ writeHarFile ( harFileName ) ;
135+ }
134136
135- clearHarEntries ( ) ;
137+ clearHarEntries ( ) ;
136138
137- } ) ;
139+ } ) ;
140+ }
0 commit comments