• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MartinTichovsky / cypress-interceptor / 15779240630

20 Jun 2025 12:44PM UTC coverage: 94.016%. First build
15779240630

Pull #21

github

web-flow
Merge c4f8bc9a6 into bf3c5bc36
Pull Request #21: BREAKING CHANGE: network report

1032 of 1108 branches covered (93.14%)

Branch coverage included in aggregate %.

283 of 344 new or added lines in 9 files covered. (82.27%)

2110 of 2234 relevant lines covered (94.45%)

1367.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.1
/packages/interceptor/src/createFetchProxy.ts
1
import { convertInputBodyToString } from "../convert/convert";
2
import { WindowTypeOfRequestProxy } from "../Interceptor.types";
3
import { lineCalled } from "../test.unit";
4
import { emptyProxy, RequestProxy } from "./RequestProxy";
5
import { CallLineEnum } from "./test.enum";
6

7
export const createFetchProxy = (win: WindowTypeOfRequestProxy, requestProxy: RequestProxy) => {
22✔
8
    if (win.originFetch === undefined) {
1,115✔
9
        win.originFetch = win.fetch;
974✔
10
    }
11

12
    win.fetch = async (input: RequestInfo | URL, init: RequestInit | undefined = {}) => {
1,115✔
13
        const inputUrl =
14
            typeof input === "object" && !(input instanceof win.URL || input instanceof URL)
847✔
15
                ? input.url
16
                : input;
17
        const url =
18
            typeof inputUrl === "string" ? new win.URL(inputUrl, win.location.href) : inputUrl;
847✔
19

20
        if (input instanceof win.Request || input instanceof Request) {
847✔
21
            init.body = input.body;
4✔
22
            init.method = input.method;
4✔
23
            init.headers = input.headers;
4✔
24
        }
25

26
        const headers = Object.fromEntries(new Headers(init.headers).entries());
847✔
27

28
        const proxy = await requestProxy
847✔
29
            .requestStart(
30
                {
31
                    body: init.body,
32
                    headers,
33
                    method: init.method ?? "GET",
888✔
34
                    url
35
                },
36
                win,
37
                "fetch"
38
            )
39
            .catch(() => {
40
                lineCalled(CallLineEnum.n000001);
1✔
41

42
                return emptyProxy;
1✔
43
            });
44

45
        return new Promise((resolve, reject) => {
847✔
46
            const mock = proxy.mock;
847✔
47

48
            const hasResponseBodyMock = !(!mock?.body && !mock?.generateBody);
847✔
49

50
            const mockResponse = (responseText = "") => {
847✔
51
                convertInputBodyToString(init.body, win)
192✔
52
                    .then((convertedBody) => {
53
                        const body =
54
                            mock!.generateBody?.(
191✔
55
                                {
56
                                    body: convertedBody,
57
                                    headers,
58
                                    method: init.method ?? "GET",
41✔
59
                                    query: Object.fromEntries(url.searchParams)
60
                                },
61
                                () => {
62
                                    try {
5✔
63
                                        return JSON.parse(convertedBody);
5✔
64
                                    } catch {
65
                                        lineCalled(CallLineEnum.n000004);
1✔
66

67
                                        return convertedBody;
1✔
68
                                    }
69
                                }
70
                            ) ??
71
                            mock!.body ??
72
                            responseText;
73

74
                        const isObject = typeof body === "object";
187✔
75

76
                        const headersWithMock = { ...headers, ...mock!.headers };
187✔
77

78
                        if (
187✔
79
                            isObject &&
336✔
80
                            !Object.keys(headersWithMock).some(
81
                                (key) => key.toLowerCase() === "content-type"
152✔
82
                            )
83
                        ) {
84
                            headersWithMock["Content-Type"] = "application/json";
37✔
85
                        }
86

87
                        const response = new Response(
187✔
88
                            isObject ? JSON.stringify(body) : String(body),
187✔
89
                            {
90
                                headers: headersWithMock,
91
                                status: mock!.statusCode ?? 200,
222✔
92
                                statusText: mock!.statusText ?? "OK"
370✔
93
                            }
94
                        );
95

96
                        return proxy.done(response, () => resolve(response), true);
186✔
97
                    })
98
                    .catch((error) => {
99
                        lineCalled(CallLineEnum.n000002);
6✔
100

101
                        try {
6✔
102
                            proxy.error(error);
6✔
103
                        } catch {
104
                            lineCalled(CallLineEnum.n000003);
2✔
105
                        }
106

107
                        reject(error);
6✔
108
                    });
109
            };
110

111
            if (hasResponseBodyMock && !mock.allowHitTheNetwork) {
847✔
112
                return mockResponse();
149✔
113
            }
114

115
            win.originFetch!(input, init)
698✔
116
                .then((response) => {
117
                    // mock only headers or status
118
                    if (
654✔
119
                        hasResponseBodyMock ||
2,524✔
120
                        mock?.headers ||
121
                        mock?.statusCode ||
122
                        mock?.statusText
123
                    ) {
124
                        response
45✔
125
                            .text()
126
                            .then((responseText) => {
127
                                mockResponse(responseText);
43✔
128
                            })
129
                            .catch((error) => {
130
                                lineCalled(CallLineEnum.n000005);
2✔
131

132
                                try {
2✔
133
                                    proxy.error(error);
2✔
134
                                } catch {
135
                                    lineCalled(CallLineEnum.n000006);
1✔
136
                                }
137

138
                                resolve(response);
2✔
139
                            });
140
                    } else if (proxy) {
609!
141
                        try {
609✔
142
                            proxy.done(response, () => resolve(response));
609✔
143
                        } catch {
144
                            lineCalled(CallLineEnum.n000007);
1✔
145
                            resolve(response);
1✔
146
                        }
147
                    } else {
NEW
148
                        resolve(response);
×
149
                    }
150
                })
151
                .catch((error) => {
152
                    lineCalled(CallLineEnum.n000008);
44✔
153

154
                    try {
44✔
155
                        proxy.error(error);
44✔
156
                    } catch {
157
                        lineCalled(CallLineEnum.n000009);
1✔
158
                    }
159

160
                    reject(error);
44✔
161
                });
162
        });
163
    };
164
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc