@opra/testing 0.10.0 → 0.12.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.
@@ -2,9 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertFilter = exports.ApiExpectCollection = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const lodash_1 = require("lodash");
5
+ const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
6
+ const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
6
7
  const rule_judgment_1 = tslib_1.__importDefault(require("rule-judgment"));
7
8
  const common_1 = require("@opra/common");
9
+ // @ts-ignore
10
+ const ruleJudgment = typeof rule_judgment_1.default === 'object' ? rule_judgment_1.default.default : rule_judgment_1.default;
8
11
  class ApiExpectCollection {
9
12
  constructor(response, _isNot = false) {
10
13
  this.response = response;
@@ -14,13 +17,13 @@ class ApiExpectCollection {
14
17
  return new ApiExpectCollection(this.response, !this._isNot);
15
18
  }
16
19
  forEach(callbackfn) {
17
- this.response.data.forEach(callbackfn);
20
+ this.response.body.forEach(callbackfn);
18
21
  return this;
19
22
  }
20
23
  toMatch(expected) {
21
24
  try {
22
- const v = (0, lodash_1.omitBy)(expected, lodash_1.isNil);
23
- for (const item of this.response.data) {
25
+ const v = (0, lodash_omitby_1.default)(expected, lodash_isnil_1.default);
26
+ for (const item of this.response.body) {
24
27
  this._expect(item).toMatchObject(v);
25
28
  }
26
29
  }
@@ -32,7 +35,7 @@ class ApiExpectCollection {
32
35
  }
33
36
  toHaveFields(keys) {
34
37
  try {
35
- for (const item of this.response.data) {
38
+ for (const item of this.response.body) {
36
39
  this._expect(item).toHaveFields(keys);
37
40
  }
38
41
  }
@@ -44,7 +47,7 @@ class ApiExpectCollection {
44
47
  }
45
48
  toHaveFieldsOnly(keys) {
46
49
  try {
47
- for (const item of this.response.data) {
50
+ for (const item of this.response.body) {
48
51
  this._expect(item).toHaveFieldsOnly(keys);
49
52
  }
50
53
  }
@@ -69,7 +72,7 @@ class ApiExpectCollection {
69
72
  // }
70
73
  toBeSortedBy(...fields) {
71
74
  try {
72
- this._expect(this.response.data).toBeSortedBy(fields);
75
+ this._expect(this.response.body).toBeSortedBy(fields);
73
76
  }
74
77
  catch (e) {
75
78
  Error.captureStackTrace(e, this.toBeSortedBy);
@@ -80,10 +83,10 @@ class ApiExpectCollection {
80
83
  toBeFilteredBy(filter) {
81
84
  const f = convertFilter(filter);
82
85
  if (f) {
83
- const j = (0, rule_judgment_1.default)(f);
84
- const filtered = this.response.data.filter(j);
86
+ const j = ruleJudgment(f);
87
+ const filtered = this.response.body.filter(j);
85
88
  try {
86
- this._expect(this.response.data).toStrictEqual(filtered);
89
+ this._expect(this.response.body).toStrictEqual(filtered);
87
90
  }
88
91
  catch (e) {
89
92
  Error.captureStackTrace(e, this.toBeFilteredBy);
@@ -94,7 +97,7 @@ class ApiExpectCollection {
94
97
  }
95
98
  toHaveExactItems(expected) {
96
99
  try {
97
- this._expect(this.response.data).toHaveLength(expected);
100
+ this._expect(this.response.body).toHaveLength(expected);
98
101
  }
99
102
  catch (e) {
100
103
  Error.captureStackTrace(e, this.toHaveExactItems);
@@ -104,7 +107,7 @@ class ApiExpectCollection {
104
107
  }
105
108
  toHaveMaxItems(expected) {
106
109
  try {
107
- this._expect(this.response.data.length).toBeLessThanOrEqual(expected);
110
+ this._expect(this.response.body.length).toBeLessThanOrEqual(expected);
108
111
  }
109
112
  catch (e) {
110
113
  Error.captureStackTrace(e, this.toHaveMaxItems);
@@ -114,7 +117,7 @@ class ApiExpectCollection {
114
117
  }
115
118
  toHaveMinItems(expected) {
116
119
  try {
117
- this._expect(this.response.data.length).toBeGreaterThanOrEqual(expected);
120
+ this._expect(this.response.body.length).toBeGreaterThanOrEqual(expected);
118
121
  }
119
122
  catch (e) {
120
123
  Error.captureStackTrace(e, this.toHaveMinItems);
@@ -12,8 +12,9 @@ class ApiExpectError extends api_expect_object_js_1.ApiExpectObject {
12
12
  }
13
13
  toContainDetail(...matching) {
14
14
  try {
15
- expect(this.response.data.issues).toBeDefined();
16
- expect(this.response.data.issues).apiErrorDetailToContain(matching);
15
+ expect(this.response.body).toBeDefined();
16
+ expect(this.response.body.issues).toBeDefined();
17
+ expect(this.response.body.issues).apiErrorDetailToContain(matching);
17
18
  }
18
19
  catch (e) {
19
20
  Error.captureStackTrace(e, this.toContainDetail);
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiExpectObject = void 0;
4
- const lodash_1 = require("lodash");
4
+ const tslib_1 = require("tslib");
5
+ const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
6
+ const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
5
7
  class ApiExpectObject {
6
8
  constructor(response, _isNot = false) {
7
9
  this.response = response;
@@ -12,8 +14,8 @@ class ApiExpectObject {
12
14
  }
13
15
  toMatch(expected) {
14
16
  try {
15
- const v = (0, lodash_1.omitBy)(expected, lodash_1.isNil);
16
- this._expect(this.response.data).toMatchObject(v);
17
+ const v = (0, lodash_omitby_1.default)(expected, lodash_isnil_1.default);
18
+ this._expect(this.response.body).toMatchObject(v);
17
19
  }
18
20
  catch (e) {
19
21
  Error.captureStackTrace(e, this.toMatch);
@@ -23,7 +25,7 @@ class ApiExpectObject {
23
25
  }
24
26
  toHaveFields(fields) {
25
27
  try {
26
- this._expect(this.response.data).toHaveFields(fields);
28
+ this._expect(this.response.body).toHaveFields(fields);
27
29
  }
28
30
  catch (e) {
29
31
  Error.captureStackTrace(e, this.toHaveFields);
@@ -33,7 +35,7 @@ class ApiExpectObject {
33
35
  }
34
36
  toHaveFieldsOnly(fields) {
35
37
  try {
36
- this._expect(this.response.data).toHaveFieldsOnly(fields);
38
+ this._expect(this.response.body).toHaveFieldsOnly(fields);
37
39
  }
38
40
  catch (e) {
39
41
  Error.captureStackTrace(e, this.toHaveFieldsOnly);
@@ -11,7 +11,7 @@ class ApiExpectOperationResult {
11
11
  }
12
12
  toBeAffectedExact(expected) {
13
13
  try {
14
- this._expect(this.response.data.affected).toStrictEqual(expected);
14
+ this._expect(this.response.body.affected).toStrictEqual(expected);
15
15
  }
16
16
  catch (e) {
17
17
  Error.captureStackTrace(e, this.toBeAffectedExact);
@@ -21,7 +21,7 @@ class ApiExpectOperationResult {
21
21
  }
22
22
  toBeAffectedMin(expected) {
23
23
  try {
24
- this._expect(this.response.data.affected).toBeGreaterThanOrEqual(expected);
24
+ this._expect(this.response.body.affected).toBeGreaterThanOrEqual(expected);
25
25
  }
26
26
  catch (e) {
27
27
  Error.captureStackTrace(e, this.toBeAffectedMin);
@@ -31,7 +31,7 @@ class ApiExpectOperationResult {
31
31
  }
32
32
  toBeAffectedMax(expected) {
33
33
  try {
34
- this._expect(this.response.data.affected).toBeLessThanOrEqual(expected);
34
+ this._expect(this.response.body.affected).toBeLessThanOrEqual(expected);
35
35
  }
36
36
  catch (e) {
37
37
  Error.captureStackTrace(e, this.toBeAffectedMax);
@@ -39,8 +39,8 @@ class ApiExpect {
39
39
  expect(this.response.status).toBeLessThanOrEqual(599);
40
40
  }
41
41
  msg = 'Api did not returned "errors"';
42
- expect(this.response.data.errors).toBeArray();
43
- expect(this.response.data.errors.length).toBeGreaterThan(0);
42
+ expect(this.response.body.errors).toBeArray();
43
+ expect(this.response.body.errors.length).toBeGreaterThan(0);
44
44
  }
45
45
  catch (e) {
46
46
  if (msg)
@@ -54,9 +54,9 @@ class ApiExpect {
54
54
  let msg = '';
55
55
  try {
56
56
  msg = '"body" is empty';
57
- expect(this.response.data).toBeDefined();
57
+ expect(this.response.body).toBeDefined();
58
58
  msg = '"operation" property is empty';
59
- expect(this.response.data.operation).toBeDefined();
59
+ expect(this.response.body.operation).toBeDefined();
60
60
  }
61
61
  catch (e) {
62
62
  if (msg)
@@ -70,8 +70,8 @@ class ApiExpect {
70
70
  let msg = '';
71
71
  try {
72
72
  msg = '"body" is empty';
73
- expect(this.response.data).toBeDefined();
74
- expect(typeof this.response.data).toStrictEqual('object');
73
+ expect(this.response.body).toBeDefined();
74
+ expect(typeof this.response.body).toStrictEqual('object');
75
75
  }
76
76
  catch (e) {
77
77
  if (msg)
@@ -85,9 +85,9 @@ class ApiExpect {
85
85
  let msg = '';
86
86
  try {
87
87
  msg = '"body" is empty';
88
- expect(this.response.data).toBeDefined();
88
+ expect(this.response.body).toBeDefined();
89
89
  msg = '"body" is not an array';
90
- expect(this.response.data).toBeArray();
90
+ expect(this.response.body).toBeArray();
91
91
  }
92
92
  catch (e) {
93
93
  if (msg)
@@ -7,14 +7,13 @@ const common_1 = require("@opra/common");
7
7
  const node_client_1 = require("@opra/node-client");
8
8
  const api_expect_js_1 = require("./api-expect/api-expect.js");
9
9
  const is_absolute_url_util_js_1 = require("./utils/is-absolute-url.util.js");
10
- class OpraTestClient extends node_client_1.OpraHttpClient {
10
+ class OpraTestClient extends node_client_1.OpraHttpClientBase {
11
11
  constructor(app, options) {
12
12
  super('/', options);
13
13
  this._server = app instanceof http_1.Server ? app : (0, http_1.createServer)(app);
14
14
  }
15
- // @ts-ignore
16
- async _fetch(urlString, req) {
17
- const resp = await new Promise((resolve, reject) => {
15
+ async _fetch(urlString, req = {}) {
16
+ return new Promise((resolve, reject) => {
18
17
  urlString = (0, is_absolute_url_util_js_1.isAbsoluteUrl)(urlString) ? urlString : (0, common_1.joinPath)('http://opra.test', urlString);
19
18
  const url = new url_1.URL(urlString);
20
19
  // Set protocol to HTTP
@@ -36,21 +35,23 @@ class OpraTestClient extends node_client_1.OpraHttpClient {
36
35
  }).then(res => {
37
36
  if (!this._server.listening)
38
37
  return resolve(res);
39
- this._server.close(() => resolve(res));
38
+ this._server.once('close', () => resolve(res));
39
+ this._server.close();
40
+ this._server.unref();
40
41
  }).then()
41
42
  .catch(error => {
42
43
  if (!this._server.listening)
43
44
  return reject(error);
44
- this._server.close(() => reject(error));
45
+ this._server.once('close', () => reject(error));
46
+ this._server.close();
47
+ this._server.unref();
45
48
  });
46
49
  });
50
+ }
51
+ _createResponse(init) {
52
+ const resp = super._createResponse(init);
47
53
  resp.expect = new api_expect_js_1.ApiExpect(resp);
48
54
  return resp;
49
55
  }
50
- static async create(app, options) {
51
- const client = new this(app, options);
52
- await client.init();
53
- return client;
54
- }
55
56
  }
56
57
  exports.OpraTestClient = OpraTestClient;
@@ -1,5 +1,4 @@
1
- import { Expression } from '@opra/common';
2
- import { HttpResponse } from '@opra/node-client';
1
+ import { Expression, HttpResponse } from '@opra/common';
3
2
  export declare class ApiExpectCollection {
4
3
  readonly response: HttpResponse;
5
4
  protected _isNot: boolean;
@@ -1,6 +1,9 @@
1
- import { isNil, omitBy } from 'lodash';
2
- import ruleJudgment from 'rule-judgment';
1
+ import isNil from 'lodash.isnil';
2
+ import omitBy from 'lodash.omitby';
3
+ import ruleJudgmentLib from 'rule-judgment';
3
4
  import { ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, parseFilter, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/common';
5
+ // @ts-ignore
6
+ const ruleJudgment = typeof ruleJudgmentLib === 'object' ? ruleJudgmentLib.default : ruleJudgmentLib;
4
7
  export class ApiExpectCollection {
5
8
  constructor(response, _isNot = false) {
6
9
  this.response = response;
@@ -10,13 +13,13 @@ export class ApiExpectCollection {
10
13
  return new ApiExpectCollection(this.response, !this._isNot);
11
14
  }
12
15
  forEach(callbackfn) {
13
- this.response.data.forEach(callbackfn);
16
+ this.response.body.forEach(callbackfn);
14
17
  return this;
15
18
  }
16
19
  toMatch(expected) {
17
20
  try {
18
21
  const v = omitBy(expected, isNil);
19
- for (const item of this.response.data) {
22
+ for (const item of this.response.body) {
20
23
  this._expect(item).toMatchObject(v);
21
24
  }
22
25
  }
@@ -28,7 +31,7 @@ export class ApiExpectCollection {
28
31
  }
29
32
  toHaveFields(keys) {
30
33
  try {
31
- for (const item of this.response.data) {
34
+ for (const item of this.response.body) {
32
35
  this._expect(item).toHaveFields(keys);
33
36
  }
34
37
  }
@@ -40,7 +43,7 @@ export class ApiExpectCollection {
40
43
  }
41
44
  toHaveFieldsOnly(keys) {
42
45
  try {
43
- for (const item of this.response.data) {
46
+ for (const item of this.response.body) {
44
47
  this._expect(item).toHaveFieldsOnly(keys);
45
48
  }
46
49
  }
@@ -65,7 +68,7 @@ export class ApiExpectCollection {
65
68
  // }
66
69
  toBeSortedBy(...fields) {
67
70
  try {
68
- this._expect(this.response.data).toBeSortedBy(fields);
71
+ this._expect(this.response.body).toBeSortedBy(fields);
69
72
  }
70
73
  catch (e) {
71
74
  Error.captureStackTrace(e, this.toBeSortedBy);
@@ -77,9 +80,9 @@ export class ApiExpectCollection {
77
80
  const f = convertFilter(filter);
78
81
  if (f) {
79
82
  const j = ruleJudgment(f);
80
- const filtered = this.response.data.filter(j);
83
+ const filtered = this.response.body.filter(j);
81
84
  try {
82
- this._expect(this.response.data).toStrictEqual(filtered);
85
+ this._expect(this.response.body).toStrictEqual(filtered);
83
86
  }
84
87
  catch (e) {
85
88
  Error.captureStackTrace(e, this.toBeFilteredBy);
@@ -90,7 +93,7 @@ export class ApiExpectCollection {
90
93
  }
91
94
  toHaveExactItems(expected) {
92
95
  try {
93
- this._expect(this.response.data).toHaveLength(expected);
96
+ this._expect(this.response.body).toHaveLength(expected);
94
97
  }
95
98
  catch (e) {
96
99
  Error.captureStackTrace(e, this.toHaveExactItems);
@@ -100,7 +103,7 @@ export class ApiExpectCollection {
100
103
  }
101
104
  toHaveMaxItems(expected) {
102
105
  try {
103
- this._expect(this.response.data.length).toBeLessThanOrEqual(expected);
106
+ this._expect(this.response.body.length).toBeLessThanOrEqual(expected);
104
107
  }
105
108
  catch (e) {
106
109
  Error.captureStackTrace(e, this.toHaveMaxItems);
@@ -110,7 +113,7 @@ export class ApiExpectCollection {
110
113
  }
111
114
  toHaveMinItems(expected) {
112
115
  try {
113
- this._expect(this.response.data.length).toBeGreaterThanOrEqual(expected);
116
+ this._expect(this.response.body.length).toBeGreaterThanOrEqual(expected);
114
117
  }
115
118
  catch (e) {
116
119
  Error.captureStackTrace(e, this.toHaveMinItems);
@@ -1,4 +1,4 @@
1
- import { HttpResponse } from '@opra/node-client';
1
+ import { HttpResponse } from '@opra/common';
2
2
  import { ApiExpectObject } from './api-expect-object.js';
3
3
  export declare class ApiExpectError extends ApiExpectObject {
4
4
  readonly response: HttpResponse;
@@ -8,8 +8,9 @@ export class ApiExpectError extends ApiExpectObject {
8
8
  }
9
9
  toContainDetail(...matching) {
10
10
  try {
11
- expect(this.response.data.issues).toBeDefined();
12
- expect(this.response.data.issues).apiErrorDetailToContain(matching);
11
+ expect(this.response.body).toBeDefined();
12
+ expect(this.response.body.issues).toBeDefined();
13
+ expect(this.response.body.issues).apiErrorDetailToContain(matching);
13
14
  }
14
15
  catch (e) {
15
16
  Error.captureStackTrace(e, this.toContainDetail);
@@ -1,4 +1,4 @@
1
- import { HttpResponse } from '@opra/node-client';
1
+ import { HttpResponse } from '@opra/common';
2
2
  export declare class ApiExpectObject {
3
3
  readonly response: HttpResponse;
4
4
  protected _isNot: boolean;
@@ -1,4 +1,5 @@
1
- import { isNil, omitBy } from 'lodash';
1
+ import isNil from 'lodash.isnil';
2
+ import omitBy from 'lodash.omitby';
2
3
  export class ApiExpectObject {
3
4
  constructor(response, _isNot = false) {
4
5
  this.response = response;
@@ -10,7 +11,7 @@ export class ApiExpectObject {
10
11
  toMatch(expected) {
11
12
  try {
12
13
  const v = omitBy(expected, isNil);
13
- this._expect(this.response.data).toMatchObject(v);
14
+ this._expect(this.response.body).toMatchObject(v);
14
15
  }
15
16
  catch (e) {
16
17
  Error.captureStackTrace(e, this.toMatch);
@@ -20,7 +21,7 @@ export class ApiExpectObject {
20
21
  }
21
22
  toHaveFields(fields) {
22
23
  try {
23
- this._expect(this.response.data).toHaveFields(fields);
24
+ this._expect(this.response.body).toHaveFields(fields);
24
25
  }
25
26
  catch (e) {
26
27
  Error.captureStackTrace(e, this.toHaveFields);
@@ -30,7 +31,7 @@ export class ApiExpectObject {
30
31
  }
31
32
  toHaveFieldsOnly(fields) {
32
33
  try {
33
- this._expect(this.response.data).toHaveFieldsOnly(fields);
34
+ this._expect(this.response.body).toHaveFieldsOnly(fields);
34
35
  }
35
36
  catch (e) {
36
37
  Error.captureStackTrace(e, this.toHaveFieldsOnly);
@@ -1,4 +1,4 @@
1
- import { HttpResponse } from '@opra/node-client';
1
+ import { HttpResponse } from '@opra/common';
2
2
  export declare class ApiExpectOperationResult {
3
3
  readonly response: HttpResponse;
4
4
  protected _isNot: boolean;
@@ -8,7 +8,7 @@ export class ApiExpectOperationResult {
8
8
  }
9
9
  toBeAffectedExact(expected) {
10
10
  try {
11
- this._expect(this.response.data.affected).toStrictEqual(expected);
11
+ this._expect(this.response.body.affected).toStrictEqual(expected);
12
12
  }
13
13
  catch (e) {
14
14
  Error.captureStackTrace(e, this.toBeAffectedExact);
@@ -18,7 +18,7 @@ export class ApiExpectOperationResult {
18
18
  }
19
19
  toBeAffectedMin(expected) {
20
20
  try {
21
- this._expect(this.response.data.affected).toBeGreaterThanOrEqual(expected);
21
+ this._expect(this.response.body.affected).toBeGreaterThanOrEqual(expected);
22
22
  }
23
23
  catch (e) {
24
24
  Error.captureStackTrace(e, this.toBeAffectedMin);
@@ -28,7 +28,7 @@ export class ApiExpectOperationResult {
28
28
  }
29
29
  toBeAffectedMax(expected) {
30
30
  try {
31
- this._expect(this.response.data.affected).toBeLessThanOrEqual(expected);
31
+ this._expect(this.response.body.affected).toBeLessThanOrEqual(expected);
32
32
  }
33
33
  catch (e) {
34
34
  Error.captureStackTrace(e, this.toBeAffectedMax);
@@ -1,4 +1,4 @@
1
- import { HttpResponse } from '@opra/node-client';
1
+ import { HttpResponse } from '@opra/common';
2
2
  import { ApiExpectCollection } from './api-expect-collection.js';
3
3
  import { ApiExpectError } from './api-expect-error.js';
4
4
  import { ApiExpectObject } from './api-expect-object.js';
@@ -36,8 +36,8 @@ export class ApiExpect {
36
36
  expect(this.response.status).toBeLessThanOrEqual(599);
37
37
  }
38
38
  msg = 'Api did not returned "errors"';
39
- expect(this.response.data.errors).toBeArray();
40
- expect(this.response.data.errors.length).toBeGreaterThan(0);
39
+ expect(this.response.body.errors).toBeArray();
40
+ expect(this.response.body.errors.length).toBeGreaterThan(0);
41
41
  }
42
42
  catch (e) {
43
43
  if (msg)
@@ -51,9 +51,9 @@ export class ApiExpect {
51
51
  let msg = '';
52
52
  try {
53
53
  msg = '"body" is empty';
54
- expect(this.response.data).toBeDefined();
54
+ expect(this.response.body).toBeDefined();
55
55
  msg = '"operation" property is empty';
56
- expect(this.response.data.operation).toBeDefined();
56
+ expect(this.response.body.operation).toBeDefined();
57
57
  }
58
58
  catch (e) {
59
59
  if (msg)
@@ -67,8 +67,8 @@ export class ApiExpect {
67
67
  let msg = '';
68
68
  try {
69
69
  msg = '"body" is empty';
70
- expect(this.response.data).toBeDefined();
71
- expect(typeof this.response.data).toStrictEqual('object');
70
+ expect(this.response.body).toBeDefined();
71
+ expect(typeof this.response.body).toStrictEqual('object');
72
72
  }
73
73
  catch (e) {
74
74
  if (msg)
@@ -82,9 +82,9 @@ export class ApiExpect {
82
82
  let msg = '';
83
83
  try {
84
84
  msg = '"body" is empty';
85
- expect(this.response.data).toBeDefined();
85
+ expect(this.response.body).toBeDefined();
86
86
  msg = '"body" is not an array';
87
- expect(this.response.data).toBeArray();
87
+ expect(this.response.body).toBeArray();
88
88
  }
89
89
  catch (e) {
90
90
  if (msg)
@@ -1,20 +1,16 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, Server, ServerResponse } from 'http';
3
- import { Type } from 'ts-gems';
4
- import { BatchRequest, HttpCollectionService, HttpRequest, HttpResponse, HttpSingletonService, OpraHttpClient, OpraHttpClientOptions } from '@opra/node-client';
3
+ import { HttpResponse, HttpResponseInit } from '@opra/common';
4
+ import { OpraHttpClientBase, OpraHttpClientOptions } from '@opra/node-client';
5
5
  import { ApiExpect } from './api-expect/api-expect.js';
6
6
  declare type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
7
- declare type Handler = RequestListener | Server;
8
- export declare type TestHttpResponse<T = any> = HttpResponse<T> & {
7
+ export type ResponseExt = {
9
8
  expect: ApiExpect;
10
9
  };
11
- export declare class OpraTestClient extends OpraHttpClient {
10
+ export declare class OpraTestClient extends OpraHttpClientBase<ResponseExt> {
12
11
  protected _server: Server;
13
12
  constructor(app: Server | RequestListener, options?: OpraHttpClientOptions);
14
- batch<TResponse extends TestHttpResponse = TestHttpResponse>(requests: HttpRequest[]): BatchRequest<TResponse>;
15
- collection<T = any, TResponse extends TestHttpResponse<T> = TestHttpResponse<T>>(name: string): HttpCollectionService<T, TResponse>;
16
- singleton<T = any, TResponse extends TestHttpResponse<T> = TestHttpResponse<T>>(name: string): HttpSingletonService<T, TResponse>;
17
- protected _fetch<TResponse extends TestHttpResponse = TestHttpResponse>(urlString: string, req: RequestInit): Promise<TResponse>;
18
- static create<T extends OpraTestClient>(this: Type<T>, app: Handler, options?: OpraHttpClientOptions): Promise<T>;
13
+ protected _fetch(urlString: string, req?: RequestInit): Promise<Response>;
14
+ protected _createResponse(init?: HttpResponseInit): HttpResponse;
19
15
  }
20
16
  export {};
@@ -1,17 +1,18 @@
1
1
  import { createServer, Server } from 'http';
2
2
  import { URL } from 'url';
3
3
  import { joinPath } from '@opra/common';
4
- import { OpraHttpClient, } from '@opra/node-client';
4
+ import {
5
+ // BatchRequest,
6
+ OpraHttpClientBase, } from '@opra/node-client';
5
7
  import { ApiExpect } from './api-expect/api-expect.js';
6
8
  import { isAbsoluteUrl } from './utils/is-absolute-url.util.js';
7
- export class OpraTestClient extends OpraHttpClient {
9
+ export class OpraTestClient extends OpraHttpClientBase {
8
10
  constructor(app, options) {
9
11
  super('/', options);
10
12
  this._server = app instanceof Server ? app : createServer(app);
11
13
  }
12
- // @ts-ignore
13
- async _fetch(urlString, req) {
14
- const resp = await new Promise((resolve, reject) => {
14
+ async _fetch(urlString, req = {}) {
15
+ return new Promise((resolve, reject) => {
15
16
  urlString = isAbsoluteUrl(urlString) ? urlString : joinPath('http://opra.test', urlString);
16
17
  const url = new URL(urlString);
17
18
  // Set protocol to HTTP
@@ -33,20 +34,22 @@ export class OpraTestClient extends OpraHttpClient {
33
34
  }).then(res => {
34
35
  if (!this._server.listening)
35
36
  return resolve(res);
36
- this._server.close(() => resolve(res));
37
+ this._server.once('close', () => resolve(res));
38
+ this._server.close();
39
+ this._server.unref();
37
40
  }).then()
38
41
  .catch(error => {
39
42
  if (!this._server.listening)
40
43
  return reject(error);
41
- this._server.close(() => reject(error));
44
+ this._server.once('close', () => reject(error));
45
+ this._server.close();
46
+ this._server.unref();
42
47
  });
43
48
  });
49
+ }
50
+ _createResponse(init) {
51
+ const resp = super._createResponse(init);
44
52
  resp.expect = new ApiExpect(resp);
45
53
  return resp;
46
54
  }
47
- static async create(app, options) {
48
- const client = new this(app, options);
49
- await client.init();
50
- return client;
51
- }
52
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/testing",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Opra testing package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -17,24 +17,24 @@
17
17
  "build:esm": "tsc -b tsconfig-build-esm.json",
18
18
  "postbuild": "cp README.md package.json ../../LICENSE ../../build/testing && cp ../../package.cjs.json ../../build/testing/cjs/package.json",
19
19
  "lint": "eslint .",
20
- "test": "jest",
21
- "cover": "jest --collect-coverage",
20
+ "test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
21
+ "cover": "NODE_OPTIONS=--experimental-vm-modules npx jest --collect-coverage",
22
22
  "clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
23
- "clean:src": "ts-cleanup -s src --all",
23
+ "clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
24
24
  "clean:dist": "rimraf ../../build/testing",
25
25
  "clean:cover": "rimraf ../../coverage/testing"
26
26
  },
27
27
  "dependencies": {
28
- "@opra/node-client": "^0.10.0",
29
- "@opra/common": "^0.10.0",
28
+ "@opra/common": "^0.12.0",
29
+ "@opra/node-client": "^0.12.0",
30
30
  "ansi-colors": "^4.1.3",
31
- "lodash": "^4.17.21",
31
+ "lodash.isnil": "^4.0.0",
32
+ "lodash.omitby": "^4.6.0",
32
33
  "rule-judgment": "^1.1.5"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@types/supertest": "^2.0.12"
36
37
  },
37
- "peerDependencies": {},
38
38
  "type": "module",
39
39
  "types": "esm/index.d.ts",
40
40
  "exports": {