Skip to content

Commit

Permalink
test(e2e): correct expectations & assertions (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk committed Apr 18, 2024
1 parent 4d07181 commit 9fa11cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
10 changes: 7 additions & 3 deletions tests/Commands/repeater.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ describe('Repeater Command', () => {
});

// assert
await expect(act).rejects.toThrow(
'429 - "The repeater used for the scan is not connected. Connect the repeater and restart the scan."'
);
await expect(act).rejects.toThrow('Request failed with status code 429');
await expect(act).rejects.toMatchObject({
response: {
status: 429,
data: 'The repeater used for the scan is not connected. Connect the repeater and restart the scan.'
}
});
}, 10000);
});

Expand Down
93 changes: 44 additions & 49 deletions tests/Setup/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { Axios } from 'axios';
import { setTimeout } from 'node:timers/promises';

export interface ApiOptions {
baseUrl: string;
Expand Down Expand Up @@ -30,56 +31,54 @@ export class Api {
headers: { authorization: `api-key ${options.apiKey}` }
});

this.client.interceptors.request.use((request) => {
// eslint-disable-next-line no-console
console.log('Request:', {
method: request.method,
url: request.url,
headers: request.headers,
body: request.data
});

return request;
});
const isGithubRunnerDebugMode =
process.env.ACTIONS_STEP_DEBUG === 'true' ||
process.env.ACTIONS_RUNNER_DEBUG === 'true';

this.client.interceptors.response.use(
(response) => {
if (isGithubRunnerDebugMode) {
this.client.interceptors.request.use((request) => {
// eslint-disable-next-line no-console
console.log('Response:', {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: response.data
console.log('Request:', {
method: request.method,
url: request.url,
headers: request.headers,
body: request.data
});

return response;
},
(error) => {
if (axios.isAxiosError(error)) {
if (error.response) {
return request;
});

this.client.interceptors.response.use(
(response) => {
// eslint-disable-next-line no-console
console.log('Response:', {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: response.data
});

return response;
},
(error) => {
if (axios.isAxiosError(error)) {
if (error.response) {
// eslint-disable-next-line no-console
console.log('Response:', {
status: error.response.status,
statusText: error.response.statusText,
headers: error.response.headers,
body: error.response.data
});
}
} else {
// eslint-disable-next-line no-console
console.log('Response:', {
status: error.response.status,
statusText: error.response.statusText,
headers: error.response.headers,
body: error.response.data
});
console.log('Response Error:', error);
}
} else {
// eslint-disable-next-line no-console
console.log('Response Error:', error);
}

return Promise.reject(error);
}
);

const isGithubRunnerDebugMode =
process.env.ACTIONS_STEP_DEBUG === 'true' ||
process.env.ACTIONS_RUNNER_DEBUG === 'true';

if (!isGithubRunnerDebugMode) {
this.client.interceptors.request.clear();
return Promise.reject(error);
}
);
}
}

Expand Down Expand Up @@ -136,7 +135,7 @@ export class Api {
`Repeater ${repeaterId} is not connected after ${maxAttempts} checks`
);
} else {
await this.sleep(timeout);
await setTimeout(timeout);
}
}
}
Expand All @@ -162,12 +161,8 @@ export class Api {
`Scan ${scanId} couldn't finish after ${maxAttempts} checks`
);
} else {
await this.sleep(timeout);
await setTimeout(timeout);
}
}
}

private async sleep(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
}

0 comments on commit 9fa11cd

Please sign in to comment.