Skip to content

Commit 9fa11cd

Browse files
authored
test(e2e): correct expectations & assertions (#542)
1 parent 4d07181 commit 9fa11cd

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

tests/Commands/repeater.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ describe('Repeater Command', () => {
117117
});
118118

119119
// assert
120-
await expect(act).rejects.toThrow(
121-
'429 - "The repeater used for the scan is not connected. Connect the repeater and restart the scan."'
122-
);
120+
await expect(act).rejects.toThrow('Request failed with status code 429');
121+
await expect(act).rejects.toMatchObject({
122+
response: {
123+
status: 429,
124+
data: 'The repeater used for the scan is not connected. Connect the repeater and restart the scan.'
125+
}
126+
});
123127
}, 10000);
124128
});
125129

tests/Setup/api.ts

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios, { Axios } from 'axios';
2+
import { setTimeout } from 'node:timers/promises';
23

34
export interface ApiOptions {
45
baseUrl: string;
@@ -30,56 +31,54 @@ export class Api {
3031
headers: { authorization: `api-key ${options.apiKey}` }
3132
});
3233

33-
this.client.interceptors.request.use((request) => {
34-
// eslint-disable-next-line no-console
35-
console.log('Request:', {
36-
method: request.method,
37-
url: request.url,
38-
headers: request.headers,
39-
body: request.data
40-
});
41-
42-
return request;
43-
});
34+
const isGithubRunnerDebugMode =
35+
process.env.ACTIONS_STEP_DEBUG === 'true' ||
36+
process.env.ACTIONS_RUNNER_DEBUG === 'true';
4437

45-
this.client.interceptors.response.use(
46-
(response) => {
38+
if (isGithubRunnerDebugMode) {
39+
this.client.interceptors.request.use((request) => {
4740
// eslint-disable-next-line no-console
48-
console.log('Response:', {
49-
status: response.status,
50-
statusText: response.statusText,
51-
headers: response.headers,
52-
body: response.data
41+
console.log('Request:', {
42+
method: request.method,
43+
url: request.url,
44+
headers: request.headers,
45+
body: request.data
5346
});
5447

55-
return response;
56-
},
57-
(error) => {
58-
if (axios.isAxiosError(error)) {
59-
if (error.response) {
48+
return request;
49+
});
50+
51+
this.client.interceptors.response.use(
52+
(response) => {
53+
// eslint-disable-next-line no-console
54+
console.log('Response:', {
55+
status: response.status,
56+
statusText: response.statusText,
57+
headers: response.headers,
58+
body: response.data
59+
});
60+
61+
return response;
62+
},
63+
(error) => {
64+
if (axios.isAxiosError(error)) {
65+
if (error.response) {
66+
// eslint-disable-next-line no-console
67+
console.log('Response:', {
68+
status: error.response.status,
69+
statusText: error.response.statusText,
70+
headers: error.response.headers,
71+
body: error.response.data
72+
});
73+
}
74+
} else {
6075
// eslint-disable-next-line no-console
61-
console.log('Response:', {
62-
status: error.response.status,
63-
statusText: error.response.statusText,
64-
headers: error.response.headers,
65-
body: error.response.data
66-
});
76+
console.log('Response Error:', error);
6777
}
68-
} else {
69-
// eslint-disable-next-line no-console
70-
console.log('Response Error:', error);
71-
}
7278

73-
return Promise.reject(error);
74-
}
75-
);
76-
77-
const isGithubRunnerDebugMode =
78-
process.env.ACTIONS_STEP_DEBUG === 'true' ||
79-
process.env.ACTIONS_RUNNER_DEBUG === 'true';
80-
81-
if (!isGithubRunnerDebugMode) {
82-
this.client.interceptors.request.clear();
79+
return Promise.reject(error);
80+
}
81+
);
8382
}
8483
}
8584

@@ -136,7 +135,7 @@ export class Api {
136135
`Repeater ${repeaterId} is not connected after ${maxAttempts} checks`
137136
);
138137
} else {
139-
await this.sleep(timeout);
138+
await setTimeout(timeout);
140139
}
141140
}
142141
}
@@ -162,12 +161,8 @@ export class Api {
162161
`Scan ${scanId} couldn't finish after ${maxAttempts} checks`
163162
);
164163
} else {
165-
await this.sleep(timeout);
164+
await setTimeout(timeout);
166165
}
167166
}
168167
}
169-
170-
private async sleep(time: number) {
171-
return new Promise((resolve) => setTimeout(resolve, time));
172-
}
173168
}

0 commit comments

Comments
 (0)