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

oprajs / opra / 21668887407

04 Feb 2026 11:01AM UTC coverage: 81.74% (-0.04%) from 81.78%
21668887407

push

github

erayhanoglu
1.23.1

3713 of 4780 branches covered (77.68%)

Branch coverage included in aggregate %.

31853 of 38731 relevant lines covered (82.24%)

240.92 hits per line

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

54.69
/packages/testing/src/api-expect/api-expect.ts
1
import '../expect-extend/index.js';
1✔
2
import { updateErrorMessage } from '@jsopen/objects';
1✔
3
import { type ErrorIssue, 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
export class ApiExpect extends ApiExpectBase {
1✔
13
  /**
1✔
14
   * Tests if request succeeded
1✔
15
   * @param status Status code number between 200-299
1✔
16
   */
1✔
17
  toSuccess(status?: number): this {
1✔
18
    let msg = '';
122✔
19
    try {
122✔
20
      msg += `Status code do not match. `;
122✔
21
      if (status) {
122✔
22
        expect(this.response.status).toEqual(status);
24✔
23
      } else {
122✔
24
        expect(this.response.status).toBeGreaterThanOrEqual(200);
98✔
25
        expect(this.response.status).toBeLessThan(400);
98✔
26
      }
98✔
27
    } catch (e: any) {
122!
28
      e.message =
×
29
        "Request didn't succeeded as expected. " + msg + '\n\n' + e.message;
×
30

×
31
      const issues: ErrorIssue[] = this.response.body?.errors;
×
32
      if (issues) {
×
33
        e.message += '\n\n';
×
34
        issues.forEach((issue, i) => {
×
35
          const stack = Array.isArray(issue.stack)
×
36
            ? issue.stack.join('\n')
×
37
            : issue.stack;
×
38
          e.message +=
×
39
            colors.yellow(issues.length > 1 ? `Error [${i}]: ` : 'Error: ') +
×
40
            issue.message +
×
41
            '\n' +
×
42
            (stack
×
43
              ? '    ' + stack.substring(stack.indexOf('at ')) + '\n'
×
44
              : '');
×
45
        });
×
46
      }
×
47
      if (typeof Error.captureStackTrace === 'function')
×
48
        Error.captureStackTrace(e, this.toSuccess);
×
49
      else updateErrorMessage(e, e.message);
×
50
      throw e;
×
51
    }
×
52
    return this;
122✔
53
  }
122✔
54

1✔
55
  /**
1✔
56
   * Tests if request failed
1✔
57
   * @param status Status code number between 400-599
1✔
58
   */
1✔
59
  toFail(status?: number): ApiExpectError {
1✔
60
    let msg = '';
×
61
    try {
×
62
      msg += `Status code do not match. `;
×
63
      if (status) {
×
64
        expect(this.response.status).toEqual(status);
×
65
      } else {
×
66
        expect(this.response.status).toBeGreaterThanOrEqual(400);
×
67
        expect(this.response.status).toBeLessThanOrEqual(599);
×
68
      }
×
69
    } catch (e: any) {
×
70
      e.message =
×
71
        "Request didn't failed as expected. " + msg + '\n\n' + e.message;
×
72
      const issues = this.response.body?.errors;
×
73
      if (issues) {
×
74
        e.message += '\n\n';
×
75
        issues.forEach((issue, i) => {
×
76
          const stack = Array.isArray(issue.stack)
×
77
            ? issue.stack.join('\n')
×
78
            : issue.stack;
×
79
          e.message +=
×
80
            colors.yellow(issues.length > 1 ? `Error [${i}]: ` : 'Error: ') +
×
81
            issue.message +
×
82
            '\n' +
×
83
            (stack
×
84
              ? '    ' + stack.substring(stack.indexOf('at ')) + '\n'
×
85
              : '');
×
86
        });
×
87
      }
×
88
      if (typeof Error.captureStackTrace === 'function')
×
89
        Error.captureStackTrace(e, this.toFail);
×
90
      else updateErrorMessage(e, e.message);
×
91
      throw e;
×
92
    }
×
93
    return new ApiExpectError(this.response);
×
94
  }
×
95

1✔
96
  /**
1✔
97
   * Tests if API returns a Collection
1✔
98
   */
1✔
99
  toReturnCollection(): ApiExpectCollection {
1✔
100
    let msg = '';
32✔
101
    try {
32✔
102
      msg = 'Content-Type header value is not valid. ';
32✔
103
      expect(this.response.contentType).toEqual(
32✔
104
        'application/opra.response+json',
32✔
105
      );
32✔
106

32✔
107
      msg = 'Type of response "body" is not valid. ';
32✔
108
      expect(typeof this.response.body).toEqual('object');
32✔
109

32✔
110
      msg = 'Type of "payload" is not an Array. ';
32✔
111
      const payload = this.response.body.payload;
32✔
112
      expect(Array.isArray(payload) ? 'array' : typeof payload).toEqual(
32!
113
        'array',
32✔
114
      );
32✔
115
    } catch (e: any) {
32!
116
      e.message =
×
117
        "Api didn't returned a Collection. " + msg + '\n\n' + e.message;
×
118
      if (msg) e.message = msg + '\n\n' + e.message;
×
119
      if (typeof Error.captureStackTrace === 'function')
×
120
        Error.captureStackTrace(e, this.toReturnCollection);
×
121
      else updateErrorMessage(e, e.message);
×
122
      throw e;
×
123
    }
×
124
    return new ApiExpectCollection(this.response);
32✔
125
  }
32✔
126

1✔
127
  /**
1✔
128
   * Tests if API returns an Object
1✔
129
   */
1✔
130
  toReturnObject(contentType?: string): ApiExpectObject {
1✔
131
    let msg = '';
70✔
132
    try {
70✔
133
      msg = 'Content-Type header value is not valid. ';
70✔
134
      expect(this.response.contentType).toEqual(
70✔
135
        contentType || MimeTypes.opra_response_json,
70✔
136
      );
70✔
137

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

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

1✔
155
  /**
1✔
156
   * Tests if API returns an OperationResult
1✔
157
   */
1✔
158
  toReturnOperationResult(): ApiExpectOperationResult {
1✔
159
    let msg = '';
8✔
160
    try {
8✔
161
      msg = 'Content-Type header value is not valid. ';
8✔
162
      expect(this.response.contentType).toEqual(MimeTypes.opra_response_json);
8✔
163

8✔
164
      msg = 'Type of response "body" is not valid. ';
8✔
165
      expect(typeof this.response.body).toEqual('object');
8✔
166

8✔
167
      msg = 'The response has payload. ';
8✔
168
      const payload = this.response.body.payload;
8✔
169
      expect(typeof payload).toEqual('undefined');
8✔
170
    } catch (e: any) {
8!
171
      e.message =
×
172
        "Api didn't returned a OperationResult. " + msg + '\n\n' + e.message;
×
173
      if (msg) e.message = msg + '\n\n' + e.message;
×
174
      if (typeof Error.captureStackTrace === 'function')
×
175
        Error.captureStackTrace(e, this.toReturnOperationResult);
×
176
      else updateErrorMessage(e, e.message);
×
177
      throw e;
×
178
    }
×
179
    return new ApiExpectOperationResult(this.response);
8✔
180
  }
8✔
181
}
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