Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(e2e): correct expectations & assertions #542

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading