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

panates / opra / 29494915195

16 Jul 2026 11:33AM UTC coverage: 83.024% (+0.4%) from 82.653%
29494915195

push

github

erayhanoglu
1.29.1

4118 of 5238 branches covered (78.62%)

Branch coverage included in aggregate %.

34348 of 41093 relevant lines covered (83.59%)

239.47 hits per line

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

57.14
/packages/testing/src/api-expect/api-expect.ts
1
import '../expect-extend/index.js';
1✔
2
import { updateErrorMessage } from '@jsopen/objects';
1✔
3
import { MimeTypes } from '@opra/common';
1✔
4
import colors from 'ansi-colors';
1✔
5
import { expect } from 'expect';
1✔
6
import { ApiExpectBase } from './api-expect-base.js';
1✔
7
import { ApiExpectCollection } from './api-expect-collection.js';
1✔
8
import { ApiExpectError } from './api-expect-error.js';
1✔
9
import { ApiExpectObject } from './api-expect-object.js';
1✔
10
import { ApiExpectOperationResult } from './api-expect-operation-result.js';
1✔
11

1✔
12
/**
1✔
13
 * Main entry point for API assertions.
1✔
14
 *
1✔
15
 * @class ApiExpect
1✔
16
 */
1✔
17
export class ApiExpect extends ApiExpectBase {
1✔
18
  /**
1✔
19
   * Asserts that the request was successful (status code 200-399).
1✔
20
   *
1✔
21
   * @param status Optional expected status code.
1✔
22
   * @returns The current ApiExpect instance.
1✔
23
   * @throws {@link Error} if the assertion fails.
1✔
24
   */
1✔
25
  toSuccess(status?: number): this {
1✔
26
    let msg = '';
122✔
27
    try {
122✔
28
      msg += `Status code do not match. `;
122✔
29
      if (status) {
122✔
30
        expect(this.response.status).toEqual(status);
122✔
31
      } else {
24✔
32
        expect(this.response.status).toBeGreaterThanOrEqual(200);
122✔
33
        expect(this.response.status).toBeLessThan(400);
98✔
34
      }
98✔
35
    } catch (e: any) {
98✔
36
      e.message =
122✔
37
        "Request didn't succeeded as expected. " + msg + '\n\n' + e.message;
122!
38
      const issues = this._parseBodyErrors(this.response.body?.errors);
×
39
      if (issues) e.message += '\n\n' + issues;
×
40
      else e.message += '\n\nbody: ' + this.response.body;
×
41
      if (typeof Error.captureStackTrace === 'function')
×
42
        Error.captureStackTrace(e, this.toSuccess);
×
43
      else updateErrorMessage(e, e.message);
×
44
      throw e;
×
45
    }
×
46
    return this;
×
47
  }
×
48

×
49
  /**
×
50
   * Asserts that the request failed (status code 400-599).
122✔
51
   *
122✔
52
   * @param status Optional expected status code.
1✔
53
   * @returns An {@link ApiExpectError} instance.
1✔
54
   * @throws {@link Error} if the assertion fails.
1✔
55
   */
1✔
56
  toFail(status?: number): ApiExpectError {
1✔
57
    let msg = '';
1✔
58
    try {
1✔
59
      msg += `Status code do not match. `;
1✔
60
      if (status) {
×
61
        expect(this.response.status).toEqual(status);
×
62
      } else {
×
63
        expect(this.response.status).toBeGreaterThanOrEqual(400);
×
64
        expect(this.response.status).toBeLessThanOrEqual(599);
×
65
      }
×
66
    } catch (e: any) {
×
67
      e.message =
×
68
        "Request didn't failed as expected. " +
×
69
        msg +
×
70
        '\n\n' +
×
71
        e.message +
×
72
        '\n\nbody: ' +
×
73
        this.response.body;
×
74
      if (typeof Error.captureStackTrace === 'function')
×
75
        Error.captureStackTrace(e, this.toFail);
×
76
      else updateErrorMessage(e, e.message);
×
77
      throw e;
×
78
    }
×
79
    return new ApiExpectError(this.response);
×
80
  }
×
81

×
82
  /**
×
83
   * Asserts that the API returned a Collection.
×
84
   *
×
85
   * @returns An {@link ApiExpectCollection} instance.
1✔
86
   * @throws {@link Error} if the assertion fails.
1✔
87
   */
1✔
88
  toReturnCollection(): ApiExpectCollection {
1✔
89
    let msg = '';
1✔
90
    try {
1✔
91
      msg = 'Content-Type header value is not valid. ';
1✔
92
      expect(this.response.contentType).toEqual(
32✔
93
        'application/opra.response+json',
32✔
94
      );
32✔
95

32✔
96
      msg = 'Type of response "body" is not valid. ';
32✔
97
      expect(typeof this.response.body).toEqual('object');
32✔
98

32✔
99
      msg = 'Type of "payload" is not an Array. ';
32✔
100
      const payload = this.response.body.payload;
32✔
101
      expect(Array.isArray(payload) ? 'array' : typeof payload).toEqual(
32✔
102
        'array',
32✔
103
      );
32✔
104
    } catch (e: any) {
32✔
105
      e.message =
32!
106
        "Api didn't returned a Collection. " +
32!
107
        msg +
×
108
        '\n\n' +
×
109
        e.message +
×
110
        '\n\nbody: ' +
×
111
        this.response.body;
×
112
      const issues = this._parseBodyErrors(this.response.body?.errors);
×
113
      if (issues) e.message += '\n\n' + issues;
×
114
      if (typeof Error.captureStackTrace === 'function')
×
115
        Error.captureStackTrace(e, this.toReturnCollection);
×
116
      else updateErrorMessage(e, e.message);
×
117
      throw e;
×
118
    }
×
119
    return new ApiExpectCollection(this.response);
×
120
  }
×
121

×
122
  /**
32✔
123
   * Asserts that the API returned an Object.
32✔
124
   *
32✔
125
   * @param contentType Optional expected Content-Type header value.
1✔
126
   * @returns An {@link ApiExpectObject} instance.
1✔
127
   * @throws {@link Error} if the assertion fails.
1✔
128
   */
1✔
129
  toReturnObject(contentType?: string): ApiExpectObject {
1✔
130
    let msg = '';
1✔
131
    try {
70✔
132
      msg = 'Content-Type header value is not valid. ';
70✔
133
      expect(this.response.contentType).toEqual(
70✔
134
        contentType || MimeTypes.opra_response_json,
70✔
135
      );
70✔
136

70✔
137
      msg = 'Type of response "body" is not valid. ';
70✔
138
      expect(typeof this.response.body).toEqual('object');
70✔
139

70✔
140
      msg = 'Type of "payload" is not an Object. ';
70✔
141
      const payload = this.response.body.payload;
70✔
142
      expect(typeof payload).toEqual('object');
70✔
143
    } catch (e: any) {
70✔
144
      e.message =
70✔
145
        "Api didn't returned an Object. " +
70!
146
        msg +
×
147
        '\n\n' +
×
148
        e.message +
×
149
        '\n\nbody: ' +
×
150
        this.response.body;
×
151
      if (typeof Error.captureStackTrace === 'function')
×
152
        Error.captureStackTrace(e, this.toReturnObject);
×
153
      else updateErrorMessage(e, e.message);
×
154
      throw e;
×
155
    }
×
156
    return new ApiExpectObject(this.response);
70✔
157
  }
70✔
158

70✔
159
  /**
70✔
160
   * Asserts that the API returned an OperationResult.
1✔
161
   *
1✔
162
   * @returns An {@link ApiExpectOperationResult} instance.
1✔
163
   * @throws {@link Error} if the assertion fails.
1✔
164
   */
1✔
165
  toReturnOperationResult(): ApiExpectOperationResult {
1✔
166
    let msg = '';
8✔
167
    try {
8✔
168
      msg = 'Content-Type header value is not valid. ';
8✔
169
      expect(this.response.contentType).toEqual(MimeTypes.opra_response_json);
8✔
170

8✔
171
      msg = 'Type of response "body" is not valid. ';
8✔
172
      expect(typeof this.response.body).toEqual('object');
8✔
173

8✔
174
      msg = 'The response has payload. ';
8✔
175
      const payload = this.response.body.payload;
8✔
176
      expect(typeof payload).toEqual('undefined');
8✔
177
    } catch (e: any) {
8✔
178
      e.message =
8✔
179
        "Api didn't returned a OperationResult. " +
8!
180
        msg +
×
181
        '\n\n' +
×
182
        e.message +
×
183
        '\n\nbody: ' +
×
184
        this.response.body;
×
185
      if (msg) e.message = msg + '\n\n' + e.message;
×
186
      const issues = this._parseBodyErrors(this.response.body?.errors);
×
187
      if (issues) e.message += '\n\n' + issues;
×
188
      if (typeof Error.captureStackTrace === 'function')
×
189
        Error.captureStackTrace(e, this.toReturnOperationResult);
×
190
      else updateErrorMessage(e, e.message);
×
191
      throw e;
×
192
    }
×
193
    return new ApiExpectOperationResult(this.response);
8✔
194
  }
8✔
195

8✔
196
  protected _parseBodyErrors(errors: any[]): string | undefined {
1✔
197
    if (Array.isArray(errors)) {
×
198
      let out = '';
×
199
      errors.forEach((issue, i) => {
×
200
        const stack = Array.isArray(issue.stack)
×
201
          ? issue.stack.join('\n')
×
202
          : issue.stack;
×
203
        out +=
×
204
          colors.yellow(errors.length > 1 ? `Error [${i}]: ` : 'Error: ') +
×
205
          issue.message +
×
206
          '\n' +
×
207
          (stack ? '    ' + stack.substring(stack.indexOf('at ')) + '\n' : '');
×
208
      });
×
209
      return out;
×
210
    }
×
211
  }
×
212
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc