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

panates / opra / 25123641991

29 Apr 2026 05:23PM UTC coverage: 75.475% (-6.1%) from 81.556%
25123641991

push

github

erayhanoglu
chore: Dev

3250 of 4211 branches covered (77.18%)

Branch coverage included in aggregate %.

30344 of 40299 relevant lines covered (75.3%)

214.81 hits per line

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

57.4
/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 = '';
61✔
27
    try {
61✔
28
      msg += `Status code do not match. `;
61✔
29
      if (status) {
61✔
30
        expect(this.response.status).toEqual(status);
12✔
31
      } else {
61✔
32
        expect(this.response.status).toBeGreaterThanOrEqual(200);
49✔
33
        expect(this.response.status).toBeLessThan(400);
49✔
34
      }
49✔
35
    } catch (e: any) {
61!
36
      e.message =
×
37
        "Request didn't succeeded as expected. " + msg + '\n\n' + e.message;
×
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;
61✔
47
  }
61✔
48

1✔
49
  /**
1✔
50
   * Asserts that the request failed (status code 400-599).
1✔
51
   *
1✔
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 = '';
×
58
    try {
×
59
      msg += `Status code do not match. `;
×
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

1✔
82
  /**
1✔
83
   * Asserts that the API returned a Collection.
1✔
84
   *
1✔
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 = '';
16✔
90
    try {
16✔
91
      msg = 'Content-Type header value is not valid. ';
16✔
92
      expect(this.response.contentType).toEqual(
16✔
93
        'application/opra.response+json',
16✔
94
      );
16✔
95

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

16✔
99
      msg = 'Type of "payload" is not an Array. ';
16✔
100
      const payload = this.response.body.payload;
16✔
101
      expect(Array.isArray(payload) ? 'array' : typeof payload).toEqual(
16!
102
        'array',
16✔
103
      );
16✔
104
    } catch (e: any) {
16!
105
      e.message =
×
106
        "Api didn't returned a Collection. " +
×
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);
16✔
120
  }
16✔
121

1✔
122
  /**
1✔
123
   * Asserts that the API returned an Object.
1✔
124
   *
1✔
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 = '';
35✔
131
    try {
35✔
132
      msg = 'Content-Type header value is not valid. ';
35✔
133
      expect(this.response.contentType).toEqual(
35✔
134
        contentType || MimeTypes.opra_response_json,
35✔
135
      );
35✔
136

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

35✔
140
      msg = 'Type of "payload" is not an Object. ';
35✔
141
      const payload = this.response.body.payload;
35✔
142
      expect(typeof payload).toEqual('object');
35✔
143
    } catch (e: any) {
35!
144
      e.message =
×
145
        "Api didn't returned an Object. " +
×
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);
35✔
157
  }
35✔
158

1✔
159
  /**
1✔
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 = '';
4✔
167
    try {
4✔
168
      msg = 'Content-Type header value is not valid. ';
4✔
169
      expect(this.response.contentType).toEqual(MimeTypes.opra_response_json);
4✔
170

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

4✔
174
      msg = 'The response has payload. ';
4✔
175
      const payload = this.response.body.payload;
4✔
176
      expect(typeof payload).toEqual('undefined');
4✔
177
    } catch (e: any) {
4!
178
      e.message =
×
179
        "Api didn't returned a OperationResult. " +
×
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);
4✔
194
  }
4✔
195

1✔
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
}
1✔
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