@opra/testing 1.26.3 → 1.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,26 @@
1
- # @opra/I18n
1
+ # @opra/testing
2
2
 
3
- OPRA i18n package.
3
+ [![NPM Version][npm-image]][npm-url]
4
+ [![NPM Downloads][downloads-image]][downloads-url]
5
+ [![CI Tests][ci-test-image]][ci-test-url]
6
+ [![Test Coverage][coveralls-image]][coveralls-url]
7
+
8
+
9
+ ## Support
10
+ You can report bugs and discuss features on the [GitHub issues](https://github.com/panates/opra/issues) page.
11
+
12
+ ## Node Compatibility
13
+ - node >= 20.x
14
+
15
+
16
+ ## License
17
+ Available under [MIT](LICENSE) license.
18
+
19
+ [npm-image]: https://img.shields.io/npm/v/@opra/testing
20
+ [npm-url]: https://npmjs.org/package/@opra/testing
21
+ [downloads-image]: https://img.shields.io/npm/dm/@opra/testing.svg
22
+ [downloads-url]: https://npmjs.org/package/@opra/testing
23
+ [ci-test-image]: https://github.com/panates/opra/actions/workflows/test.yml/badge.svg
24
+ [ci-test-url]: https://github.com/panates/opra/actions/workflows/test.yml
25
+ [coveralls-image]: https://coveralls.io/repos/github/panates/opra/badge.svg?branch=main
26
+ [coveralls-url]: https://coveralls.io/github/panates/opra?branch=main
@@ -4,27 +4,48 @@ import { ApiExpectCollection } from './api-expect-collection.js';
4
4
  import { ApiExpectError } from './api-expect-error.js';
5
5
  import { ApiExpectObject } from './api-expect-object.js';
6
6
  import { ApiExpectOperationResult } from './api-expect-operation-result.js';
7
+ /**
8
+ * Main entry point for API assertions.
9
+ *
10
+ * @class ApiExpect
11
+ */
7
12
  export declare class ApiExpect extends ApiExpectBase {
8
13
  /**
9
- * Tests if request succeeded
10
- * @param status Status code number between 200-299
14
+ * Asserts that the request was successful (status code 200-399).
15
+ *
16
+ * @param status Optional expected status code.
17
+ * @returns The current ApiExpect instance.
18
+ * @throws {@link Error} if the assertion fails.
11
19
  */
12
20
  toSuccess(status?: number): this;
13
21
  /**
14
- * Tests if request failed
15
- * @param status Status code number between 400-599
22
+ * Asserts that the request failed (status code 400-599).
23
+ *
24
+ * @param status Optional expected status code.
25
+ * @returns An {@link ApiExpectError} instance.
26
+ * @throws {@link Error} if the assertion fails.
16
27
  */
17
28
  toFail(status?: number): ApiExpectError;
18
29
  /**
19
- * Tests if API returns a Collection
30
+ * Asserts that the API returned a Collection.
31
+ *
32
+ * @returns An {@link ApiExpectCollection} instance.
33
+ * @throws {@link Error} if the assertion fails.
20
34
  */
21
35
  toReturnCollection(): ApiExpectCollection;
22
36
  /**
23
- * Tests if API returns an Object
37
+ * Asserts that the API returned an Object.
38
+ *
39
+ * @param contentType Optional expected Content-Type header value.
40
+ * @returns An {@link ApiExpectObject} instance.
41
+ * @throws {@link Error} if the assertion fails.
24
42
  */
25
43
  toReturnObject(contentType?: string): ApiExpectObject;
26
44
  /**
27
- * Tests if API returns an OperationResult
45
+ * Asserts that the API returned an OperationResult.
46
+ *
47
+ * @returns An {@link ApiExpectOperationResult} instance.
48
+ * @throws {@link Error} if the assertion fails.
28
49
  */
29
50
  toReturnOperationResult(): ApiExpectOperationResult;
30
51
  }
@@ -8,10 +8,18 @@ import { ApiExpectCollection } from './api-expect-collection.js';
8
8
  import { ApiExpectError } from './api-expect-error.js';
9
9
  import { ApiExpectObject } from './api-expect-object.js';
10
10
  import { ApiExpectOperationResult } from './api-expect-operation-result.js';
11
+ /**
12
+ * Main entry point for API assertions.
13
+ *
14
+ * @class ApiExpect
15
+ */
11
16
  export class ApiExpect extends ApiExpectBase {
12
17
  /**
13
- * Tests if request succeeded
14
- * @param status Status code number between 200-299
18
+ * Asserts that the request was successful (status code 200-399).
19
+ *
20
+ * @param status Optional expected status code.
21
+ * @returns The current ApiExpect instance.
22
+ * @throws {@link Error} if the assertion fails.
15
23
  */
16
24
  toSuccess(status) {
17
25
  let msg = '';
@@ -53,8 +61,11 @@ export class ApiExpect extends ApiExpectBase {
53
61
  return this;
54
62
  }
55
63
  /**
56
- * Tests if request failed
57
- * @param status Status code number between 400-599
64
+ * Asserts that the request failed (status code 400-599).
65
+ *
66
+ * @param status Optional expected status code.
67
+ * @returns An {@link ApiExpectError} instance.
68
+ * @throws {@link Error} if the assertion fails.
58
69
  */
59
70
  toFail(status) {
60
71
  let msg = '';
@@ -96,7 +107,10 @@ export class ApiExpect extends ApiExpectBase {
96
107
  return new ApiExpectError(this.response);
97
108
  }
98
109
  /**
99
- * Tests if API returns a Collection
110
+ * Asserts that the API returned a Collection.
111
+ *
112
+ * @returns An {@link ApiExpectCollection} instance.
113
+ * @throws {@link Error} if the assertion fails.
100
114
  */
101
115
  toReturnCollection() {
102
116
  let msg = '';
@@ -123,7 +137,11 @@ export class ApiExpect extends ApiExpectBase {
123
137
  return new ApiExpectCollection(this.response);
124
138
  }
125
139
  /**
126
- * Tests if API returns an Object
140
+ * Asserts that the API returned an Object.
141
+ *
142
+ * @param contentType Optional expected Content-Type header value.
143
+ * @returns An {@link ApiExpectObject} instance.
144
+ * @throws {@link Error} if the assertion fails.
127
145
  */
128
146
  toReturnObject(contentType) {
129
147
  let msg = '';
@@ -149,7 +167,10 @@ export class ApiExpect extends ApiExpectBase {
149
167
  return new ApiExpectObject(this.response);
150
168
  }
151
169
  /**
152
- * Tests if API returns an OperationResult
170
+ * Asserts that the API returned an OperationResult.
171
+ *
172
+ * @returns An {@link ApiExpectOperationResult} instance.
173
+ * @throws {@link Error} if the assertion fails.
153
174
  */
154
175
  toReturnOperationResult() {
155
176
  let msg = '';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/testing",
3
- "version": "1.26.3",
3
+ "version": "1.27.0",
4
4
  "description": "Opra testing package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -12,8 +12,8 @@
12
12
  "tslib": "^2.8.1"
13
13
  },
14
14
  "peerDependencies": {
15
- "@opra/client": "^1.26.3",
16
- "@opra/common": "^1.26.3",
15
+ "@opra/client": "^1.27.0",
16
+ "@opra/common": "^1.27.0",
17
17
  "expect": "^29.0.0 || ^30.0.0",
18
18
  "jest-matcher-utils": "^29.0.0 || ^30.0.0"
19
19
  },
package/test-backend.d.ts CHANGED
@@ -7,19 +7,43 @@ export type ResponseExt = {
7
7
  expect: ApiExpect;
8
8
  };
9
9
  /**
10
+ * Test specific implementation of {@link FetchBackend} for API testing.
10
11
  *
11
12
  * @class TestBackend
12
13
  */
13
14
  export declare class TestBackend extends FetchBackend {
14
15
  protected _server: Server;
16
+ /**
17
+ * Creates a new instance of TestBackend.
18
+ *
19
+ * @param app The server or request listener to test.
20
+ * @param options Configuration options.
21
+ */
15
22
  constructor(app: Server | RequestListener, options?: OpraTestClient.Options);
23
+ /**
24
+ * Sends the actual HTTP request by starting the server if necessary.
25
+ *
26
+ * @param req The request to send.
27
+ * @returns A promise that resolves to the response.
28
+ * @protected
29
+ */
16
30
  protected send(req: Request): Promise<Response>;
31
+ /**
32
+ * Creates a {@link HttpResponse} instance with an added `expect` property.
33
+ *
34
+ * @param init The response initiator parameters.
35
+ * @returns A new HttpResponse instance with ApiExpect.
36
+ * @protected
37
+ */
17
38
  protected createResponse(init: HttpResponse.Initiator): HttpResponse<any> & ResponseExt;
18
39
  }
19
40
  /**
41
+ * Namespace for {@link TestBackend} related types and interfaces.
42
+ *
20
43
  * @namespace TestBackend
21
44
  */
22
45
  export declare namespace TestBackend {
46
+ /** Configuration options for TestBackend */
23
47
  interface Options extends FetchBackend.Options {
24
48
  }
25
49
  }
package/test-backend.js CHANGED
@@ -3,17 +3,31 @@ import * as path from 'node:path';
3
3
  import { FetchBackend, HttpResponse } from '@opra/client';
4
4
  import { ApiExpect } from './api-expect/api-expect.js';
5
5
  /**
6
+ * Test specific implementation of {@link FetchBackend} for API testing.
6
7
  *
7
8
  * @class TestBackend
8
9
  */
9
10
  export class TestBackend extends FetchBackend {
10
11
  _server;
12
+ /**
13
+ * Creates a new instance of TestBackend.
14
+ *
15
+ * @param app The server or request listener to test.
16
+ * @param options Configuration options.
17
+ */
11
18
  constructor(app, options) {
12
19
  super(options?.basePath
13
20
  ? path.posix.join('http://tempuri.org', options.basePath)
14
21
  : 'http://tempuri.org', options);
15
22
  this._server = app instanceof Server ? app : createServer(app);
16
23
  }
24
+ /**
25
+ * Sends the actual HTTP request by starting the server if necessary.
26
+ *
27
+ * @param req The request to send.
28
+ * @returns A promise that resolves to the response.
29
+ * @protected
30
+ */
17
31
  send(req) {
18
32
  return new Promise((resolve, reject) => {
19
33
  const url = new URL(req.url);
@@ -51,6 +65,13 @@ export class TestBackend extends FetchBackend {
51
65
  });
52
66
  });
53
67
  }
68
+ /**
69
+ * Creates a {@link HttpResponse} instance with an added `expect` property.
70
+ *
71
+ * @param init The response initiator parameters.
72
+ * @returns A new HttpResponse instance with ApiExpect.
73
+ * @protected
74
+ */
54
75
  createResponse(init) {
55
76
  const response = new HttpResponse(init);
56
77
  response.expect = new ApiExpect(response);
package/test-client.d.ts CHANGED
@@ -12,8 +12,19 @@ export declare namespace OpraTestClient {
12
12
  basePath?: string;
13
13
  }
14
14
  }
15
+ /**
16
+ * Test specific implementation of {@link HttpClientBase} for API testing.
17
+ *
18
+ * @class OpraTestClient
19
+ */
15
20
  export declare class OpraTestClient extends HttpClientBase<FetchBackend.RequestOptions, ResponseExt> {
16
21
  [kBackend]: TestBackend;
22
+ /**
23
+ * Creates a new instance of OpraTestClient.
24
+ *
25
+ * @param app The server or request listener to test.
26
+ * @param options Configuration options.
27
+ */
17
28
  constructor(app: Server | RequestListener, options?: OpraTestClient.Options);
18
29
  }
19
30
  export {};
package/test-client.js CHANGED
@@ -1,7 +1,18 @@
1
1
  import { HttpClientBase } from '@opra/client';
2
2
  import { TestBackend } from './test-backend.js';
3
3
  export const kContext = Symbol.for('kContext');
4
+ /**
5
+ * Test specific implementation of {@link HttpClientBase} for API testing.
6
+ *
7
+ * @class OpraTestClient
8
+ */
4
9
  export class OpraTestClient extends HttpClientBase {
10
+ /**
11
+ * Creates a new instance of OpraTestClient.
12
+ *
13
+ * @param app The server or request listener to test.
14
+ * @param options Configuration options.
15
+ */
5
16
  constructor(app, options) {
6
17
  super(new TestBackend(app, options));
7
18
  }