Skip to content

Commit b3052f3

Browse files
committed
fix(cypress): refactor API request handling and enhance logging in teardown processes
1 parent 723b1ef commit b3052f3

File tree

3 files changed

+22
-62
lines changed

3 files changed

+22
-62
lines changed

cypress/plugins/gitGateway.js

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,79 +33,40 @@ function getEnvs() {
3333

3434
const apiRoot = 'https://api.netlify.com/api/v1/';
3535

36-
async function get(netlifyApiToken, path) {
36+
async function fetchWithTimeout(netlifyApiToken, path, method = 'GET', payload = null, parseAs = 'json') {
3737
const controller = new AbortController();
3838
const timeout = setTimeout(() => controller.abort(), 10000); // 10 second timeout
3939

4040
try {
41-
const response = await fetch(`${apiRoot}${path}`, {
41+
const options = {
4242
signal: controller.signal,
43+
method,
4344
headers: {
4445
'Content-Type': 'application/json',
4546
Authorization: `Bearer ${netlifyApiToken}`,
4647
},
47-
}).then(res => res.json());
48-
clearTimeout(timeout);
49-
return response;
50-
} catch (error) {
51-
clearTimeout(timeout);
52-
if (error.name === 'AbortError') {
53-
console.error(`Netlify API GET timeout after 10s: ${path}`);
54-
throw new Error(`Netlify API GET request timeout: ${path}`);
48+
};
49+
50+
if (payload) {
51+
options.body = JSON.stringify(payload);
5552
}
56-
throw error;
57-
}
58-
}
59-
60-
async function post(netlifyApiToken, path, payload) {
61-
const controller = new AbortController();
62-
const timeout = setTimeout(() => controller.abort(), 10000); // 10 second timeout
63-
64-
try {
65-
const response = await fetch(`${apiRoot}${path}`, {
66-
signal: controller.signal,
67-
method: 'POST',
68-
headers: {
69-
'Content-Type': 'application/json',
70-
Authorization: `Bearer ${netlifyApiToken}`,
71-
},
72-
...(payload ? { body: JSON.stringify(payload) } : {}),
73-
}).then(res => res.json());
53+
54+
const response = await fetch(`${apiRoot}${path}`, options);
7455
clearTimeout(timeout);
75-
return response;
56+
57+
return parseAs === 'json' ? response.json() : response.text();
7658
} catch (error) {
7759
clearTimeout(timeout);
7860
if (error.name === 'AbortError') {
79-
console.error(`Netlify API POST timeout after 10s: ${path}`);
80-
throw new Error(`Netlify API POST request timeout: ${path}`);
61+
console.error(`Netlify API ${method} timeout after 10s: ${path}`);
62+
throw new Error(`Netlify API ${method} request timeout: ${path}`);
8163
}
8264
throw error;
8365
}
8466
}
8567

86-
async function del(netlifyApiToken, path) {
87-
const controller = new AbortController();
88-
const timeout = setTimeout(() => controller.abort(), 10000); // 10 second timeout
89-
90-
try {
91-
const response = await fetch(`${apiRoot}${path}`, {
92-
signal: controller.signal,
93-
method: 'DELETE',
94-
headers: {
95-
'Content-Type': 'application/json',
96-
Authorization: `Bearer ${netlifyApiToken}`,
97-
},
98-
}).then(res => res.text());
99-
clearTimeout(timeout);
100-
return response;
101-
} catch (error) {
102-
clearTimeout(timeout);
103-
if (error.name === 'AbortError') {
104-
console.error(`Netlify API DELETE timeout after 10s: ${path}`);
105-
throw new Error(`Netlify API DELETE request timeout: ${path}`);
106-
}
107-
throw error;
108-
}
68+
async function post(netlifyApiToken, path, payload) {
69+
return fetchWithTimeout(netlifyApiToken, path, 'POST', payload, 'json');
10970
}
11071

11172
async function createSite(netlifyApiToken, payload) {
@@ -130,12 +91,12 @@ async function enableLargeMedia(netlifyApiToken, siteId) {
13091
}
13192

13293
async function waitForDeploys(netlifyApiToken, siteId) {
133-
const maxRetries = 5; // Reduced from 10 to 5
134-
const retryDelayMs = 10 * 1000; // Reduced from 30s to 10s
94+
const maxRetries = 5;
95+
const retryDelayMs = 10 * 1000; // 10 seconds
13596

13697
for (let i = 0; i < maxRetries; i++) {
13798
try {
138-
const deploys = await get(netlifyApiToken, `sites/${siteId}/deploys`);
99+
const deploys = await fetchWithTimeout(netlifyApiToken, `sites/${siteId}/deploys`, 'GET', null, 'json');
139100

140101
if (deploys && deploys.some(deploy => deploy.state === 'ready')) {
141102
return;
@@ -331,7 +292,7 @@ async function teardownGitGateway(taskData) {
331292
const { netlifyApiToken } = getEnvs();
332293
const { site_id } = taskData;
333294
console.log('Deleting Netlify site:', site_id);
334-
await del(netlifyApiToken, `sites/${site_id}`);
295+
await fetchWithTimeout(netlifyApiToken, `sites/${site_id}`, 'DELETE', null, 'text');
335296

336297
const result = await methods[taskData.provider].teardown(taskData);
337298
return result;

cypress/plugins/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module.exports = async (on, config) => {
7171
},
7272
async teardownBackend(taskData) {
7373
const { backend } = taskData;
74+
console.log('Tearing down backend', backend);
7475

7576
switch (backend) {
7677
case 'github':

cypress/utils/steps.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ function assertNotification(message) {
4949
}
5050

5151
function assertColorOn(cssProperty, color, opts) {
52-
function assertion($el) {
53-
return expect($el).to.have.css(cssProperty, color);
54-
}
55-
5652
if (opts.type && opts.type === 'label') {
5753
(opts.scope ? opts.scope : cy).contains('label', opts.label).should($el => {
5854
expect($el).to.have.css(cssProperty, color);
5955
});
6056
} else if (opts.type && opts.type === 'field') {
57+
// eslint-disable-next-line func-style
58+
const assertion = $el => expect($el).to.have.css(cssProperty, color);
6159
if (opts.isMarkdown) {
6260
(opts.scope ? opts.scope : cy)
6361
.contains('label', opts.label)

0 commit comments

Comments
 (0)