@opra/testing 0.3.0 → 0.5.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.
Files changed (72) hide show
  1. package/cjs/{expect/api-expect-array.js → api-expect/api-expect-collection.js} +48 -44
  2. package/cjs/{expect → api-expect}/api-expect-error.js +3 -4
  3. package/cjs/api-expect/api-expect-object.js +54 -0
  4. package/cjs/{expect → api-expect}/api-expect-operation-result.js +14 -6
  5. package/cjs/{expect → api-expect}/api-expect.js +30 -23
  6. package/cjs/constants.js +4 -0
  7. package/cjs/index.js +3 -22
  8. package/cjs/{expect/jest-extend → jest-extend}/common.extend.js +23 -22
  9. package/cjs/test-client.js +34 -0
  10. package/cjs/{expect/utils → utils}/object-matches.util.js +0 -0
  11. package/esm/api-expect/api-expect-collection.d.ts +19 -0
  12. package/esm/{expect/api-expect-array.js → api-expect/api-expect-collection.js} +46 -42
  13. package/esm/{expect → api-expect}/api-expect-error.d.ts +3 -3
  14. package/esm/{expect → api-expect}/api-expect-error.js +3 -4
  15. package/esm/api-expect/api-expect-object.d.ts +11 -0
  16. package/esm/api-expect/api-expect-object.js +49 -0
  17. package/esm/api-expect/api-expect-operation-result.d.ts +11 -0
  18. package/esm/{expect → api-expect}/api-expect-operation-result.js +14 -6
  19. package/esm/{expect → api-expect}/api-expect.d.ts +8 -7
  20. package/esm/{expect → api-expect}/api-expect.js +30 -23
  21. package/esm/constants.d.ts +1 -0
  22. package/esm/constants.js +1 -0
  23. package/esm/index.d.ts +2 -6
  24. package/esm/index.js +2 -19
  25. package/esm/{expect/jest-extend → jest-extend}/common.extend.d.ts +2 -2
  26. package/esm/{expect/jest-extend → jest-extend}/common.extend.js +22 -22
  27. package/esm/test-client.d.ts +22 -0
  28. package/esm/test-client.js +29 -0
  29. package/esm/{expect/utils → utils}/object-matches.util.d.ts +0 -0
  30. package/esm/{expect/utils → utils}/object-matches.util.js +0 -0
  31. package/package.json +6 -4
  32. package/cjs/api-response.js +0 -22
  33. package/cjs/expect/api-expect-object.js +0 -76
  34. package/cjs/expect/utils/print-errors.util.js +0 -15
  35. package/cjs/testers/base-operation-tester.js +0 -24
  36. package/cjs/testers/base-tester.js +0 -23
  37. package/cjs/testers/entity-create-tester.js +0 -43
  38. package/cjs/testers/entity-delete-many-tester.js +0 -27
  39. package/cjs/testers/entity-delete-tester.js +0 -24
  40. package/cjs/testers/entity-get-tester.js +0 -46
  41. package/cjs/testers/entity-search-tester.js +0 -75
  42. package/cjs/testers/entity-tester.js +0 -72
  43. package/cjs/testers/entity-update-many-tester.js +0 -31
  44. package/cjs/testers/entity-update-tester.js +0 -46
  45. package/esm/api-response.d.ts +0 -12
  46. package/esm/api-response.js +0 -18
  47. package/esm/expect/api-expect-array.d.ts +0 -17
  48. package/esm/expect/api-expect-object.d.ts +0 -12
  49. package/esm/expect/api-expect-object.js +0 -71
  50. package/esm/expect/api-expect-operation-result.d.ts +0 -9
  51. package/esm/expect/utils/print-errors.util.d.ts +0 -1
  52. package/esm/expect/utils/print-errors.util.js +0 -11
  53. package/esm/testers/base-operation-tester.d.ts +0 -8
  54. package/esm/testers/base-operation-tester.js +0 -20
  55. package/esm/testers/base-tester.d.ts +0 -13
  56. package/esm/testers/base-tester.js +0 -19
  57. package/esm/testers/entity-create-tester.d.ts +0 -17
  58. package/esm/testers/entity-create-tester.js +0 -38
  59. package/esm/testers/entity-delete-many-tester.d.ts +0 -14
  60. package/esm/testers/entity-delete-many-tester.js +0 -22
  61. package/esm/testers/entity-delete-tester.d.ts +0 -14
  62. package/esm/testers/entity-delete-tester.js +0 -19
  63. package/esm/testers/entity-get-tester.d.ts +0 -18
  64. package/esm/testers/entity-get-tester.js +0 -41
  65. package/esm/testers/entity-search-tester.d.ts +0 -22
  66. package/esm/testers/entity-search-tester.js +0 -70
  67. package/esm/testers/entity-tester.d.ts +0 -24
  68. package/esm/testers/entity-tester.js +0 -68
  69. package/esm/testers/entity-update-many-tester.d.ts +0 -16
  70. package/esm/testers/entity-update-many-tester.js +0 -26
  71. package/esm/testers/entity-update-tester.d.ts +0 -19
  72. package/esm/testers/entity-update-tester.js +0 -41
@@ -1,79 +1,77 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertFilter = exports.ApiExpectArray = void 0;
3
+ exports.convertFilter = exports.ApiExpectCollection = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
6
  const rule_judgment_1 = tslib_1.__importDefault(require("rule-judgment"));
7
7
  const url_1 = require("@opra/url");
8
- class ApiExpectArray {
8
+ class ApiExpectCollection {
9
9
  response;
10
- constructor(response) {
10
+ _isNot;
11
+ constructor(response, _isNot = false) {
11
12
  this.response = response;
13
+ this._isNot = _isNot;
12
14
  }
13
- toMatch(expected) {
14
- try {
15
- const v = lodash_1.default.omitBy(expected, lodash_1.default.isNil);
16
- for (const item of this.response.body) {
17
- expect(item).toMatchObject(v);
18
- }
19
- }
20
- catch (e) {
21
- Error.captureStackTrace(e, this.toMatch);
22
- throw e;
23
- }
24
- return this;
15
+ get not() {
16
+ return new ApiExpectCollection(this.response, !this._isNot);
25
17
  }
26
- toContainAllKeys(keys) {
27
- try {
28
- for (const item of this.response.body) {
29
- expect(item).toContainAllKeys(keys);
30
- }
31
- }
32
- catch (e) {
33
- Error.captureStackTrace(e, this.toContainAllKeys);
34
- throw e;
35
- }
18
+ forEach(callbackfn) {
19
+ this.response.data.forEach(callbackfn);
36
20
  return this;
37
21
  }
38
- toContainKeys(keys) {
22
+ toMatch(expected) {
39
23
  try {
40
- for (const item of this.response.body) {
41
- expect(item).toContainKeys(keys);
24
+ const v = lodash_1.default.omitBy(expected, lodash_1.default.isNil);
25
+ for (const item of this.response.data) {
26
+ this._expect(item).toMatchObject(v);
42
27
  }
43
28
  }
44
29
  catch (e) {
45
- Error.captureStackTrace(e, this.toContainKeys);
30
+ Error.captureStackTrace(e, this.toMatch);
46
31
  throw e;
47
32
  }
48
33
  return this;
49
34
  }
50
- notToContainKeys(keys) {
35
+ toHaveFields(keys) {
51
36
  try {
52
- for (const item of this.response.body) {
53
- expect(item).not.toContainKeys(keys);
37
+ for (const item of this.response.data) {
38
+ this._expect(item).toHaveFields(keys);
54
39
  }
55
40
  }
56
41
  catch (e) {
57
- Error.captureStackTrace(e, this.notToContainKeys);
42
+ Error.captureStackTrace(e, this.toHaveFields);
58
43
  throw e;
59
44
  }
60
45
  return this;
61
46
  }
62
- toHaveProperty(keyPath, value) {
47
+ toHaveFieldsOnly(keys) {
63
48
  try {
64
- for (const item of this.response.body) {
65
- expect(item).toHaveProperty(keyPath, value);
49
+ for (const item of this.response.data) {
50
+ this._expect(item).toHaveFieldsOnly(keys);
66
51
  }
67
52
  }
68
53
  catch (e) {
69
- Error.captureStackTrace(e, this.toHaveProperty);
54
+ Error.captureStackTrace(e, this.toHaveFieldsOnly);
70
55
  throw e;
71
56
  }
72
57
  return this;
73
58
  }
59
+ //
60
+ // toHaveProperty(keyPath, value?): this {
61
+ // try {
62
+ // for (const item of this.response.data) {
63
+ // this._expect(item).toHaveProperty(keyPath, value);
64
+ // }
65
+ //
66
+ // } catch (e: any) {
67
+ // Error.captureStackTrace(e, this.toHaveProperty);
68
+ // throw e;
69
+ // }
70
+ // return this;
71
+ // }
74
72
  toBeSortedBy(...fields) {
75
73
  try {
76
- expect(this.response.body).toBeSortedBy(fields);
74
+ this._expect(this.response.data).toBeSortedBy(fields);
77
75
  }
78
76
  catch (e) {
79
77
  Error.captureStackTrace(e, this.toBeSortedBy);
@@ -85,9 +83,9 @@ class ApiExpectArray {
85
83
  const f = convertFilter(filter);
86
84
  if (f) {
87
85
  const j = (0, rule_judgment_1.default)(f);
88
- const filtered = this.response.body.filter(j);
86
+ const filtered = this.response.data.filter(j);
89
87
  try {
90
- expect(this.response.body).toStrictEqual(filtered);
88
+ this._expect(this.response.data).toStrictEqual(filtered);
91
89
  }
92
90
  catch (e) {
93
91
  Error.captureStackTrace(e, this.toBeFilteredBy);
@@ -98,7 +96,7 @@ class ApiExpectArray {
98
96
  }
99
97
  toHaveExactItems(expected) {
100
98
  try {
101
- expect(this.response.body).toHaveLength(expected);
99
+ this._expect(this.response.data).toHaveLength(expected);
102
100
  }
103
101
  catch (e) {
104
102
  Error.captureStackTrace(e, this.toHaveExactItems);
@@ -108,7 +106,7 @@ class ApiExpectArray {
108
106
  }
109
107
  toHaveMaxItems(expected) {
110
108
  try {
111
- expect(this.response.body.length).toBeLessThanOrEqual(expected);
109
+ this._expect(this.response.data.length).toBeLessThanOrEqual(expected);
112
110
  }
113
111
  catch (e) {
114
112
  Error.captureStackTrace(e, this.toHaveMaxItems);
@@ -118,7 +116,7 @@ class ApiExpectArray {
118
116
  }
119
117
  toHaveMinItems(expected) {
120
118
  try {
121
- expect(this.response.body.length).toBeGreaterThanOrEqual(expected);
119
+ this._expect(this.response.data.length).toBeGreaterThanOrEqual(expected);
122
120
  }
123
121
  catch (e) {
124
122
  Error.captureStackTrace(e, this.toHaveMinItems);
@@ -126,8 +124,14 @@ class ApiExpectArray {
126
124
  }
127
125
  return this;
128
126
  }
127
+ _expect(expected) {
128
+ const out = expect(expected);
129
+ if (this._isNot)
130
+ return out.not;
131
+ return out;
132
+ }
129
133
  }
130
- exports.ApiExpectArray = ApiExpectArray;
134
+ exports.ApiExpectCollection = ApiExpectCollection;
131
135
  function convertFilter(str) {
132
136
  const ast = typeof str === 'string' ? (0, url_1.$parse)(str) : str;
133
137
  if (!ast)
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiExpectError = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const jest_matcher_utils_1 = tslib_1.__importDefault(require("jest-matcher-utils"));
6
+ const object_matches_util_js_1 = require("../utils/object-matches.util.js");
6
7
  const api_expect_object_js_1 = require("./api-expect-object.js");
7
- const object_matches_util_js_1 = require("./utils/object-matches.util.js");
8
8
  class ApiExpectError extends api_expect_object_js_1.ApiExpectObject {
9
9
  response;
10
10
  constructor(response) {
@@ -13,9 +13,8 @@ class ApiExpectError extends api_expect_object_js_1.ApiExpectObject {
13
13
  }
14
14
  toContainDetail(...matching) {
15
15
  try {
16
- expect(this.response.body['@@Schema']).toStrictEqual('Opra:Exception');
17
- expect(this.response.body.issues).toBeDefined();
18
- expect(this.response.body.issues).apiErrorDetailToContain(matching);
16
+ expect(this.response.data.issues).toBeDefined();
17
+ expect(this.response.data.issues).apiErrorDetailToContain(matching);
19
18
  }
20
19
  catch (e) {
21
20
  Error.captureStackTrace(e, this.toContainDetail);
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpectObject = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
+ class ApiExpectObject {
7
+ response;
8
+ _isNot;
9
+ constructor(response, _isNot = false) {
10
+ this.response = response;
11
+ this._isNot = _isNot;
12
+ }
13
+ get not() {
14
+ return new ApiExpectObject(this.response, !this._isNot);
15
+ }
16
+ toMatch(expected) {
17
+ try {
18
+ const v = lodash_1.default.omitBy(expected, lodash_1.default.isNil);
19
+ this._expect(this.response.data).toMatchObject(v);
20
+ }
21
+ catch (e) {
22
+ Error.captureStackTrace(e, this.toMatch);
23
+ throw e;
24
+ }
25
+ return this;
26
+ }
27
+ toHaveFields(fields) {
28
+ try {
29
+ this._expect(this.response.data).toHaveFields(fields);
30
+ }
31
+ catch (e) {
32
+ Error.captureStackTrace(e, this.toHaveFields);
33
+ throw e;
34
+ }
35
+ return this;
36
+ }
37
+ toHaveFieldsOnly(fields) {
38
+ try {
39
+ this._expect(this.response.data).toHaveFieldsOnly(fields);
40
+ }
41
+ catch (e) {
42
+ Error.captureStackTrace(e, this.toHaveFieldsOnly);
43
+ throw e;
44
+ }
45
+ return this;
46
+ }
47
+ _expect(expected) {
48
+ const out = expect(expected);
49
+ if (this._isNot)
50
+ return out.not;
51
+ return out;
52
+ }
53
+ }
54
+ exports.ApiExpectObject = ApiExpectObject;
@@ -3,15 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiExpectOperationResult = void 0;
4
4
  class ApiExpectOperationResult {
5
5
  response;
6
- constructor(response) {
6
+ _isNot;
7
+ constructor(response, _isNot = false) {
7
8
  this.response = response;
9
+ this._isNot = _isNot;
8
10
  }
9
- get body() {
10
- return this.response.body;
11
+ get not() {
12
+ return new ApiExpectOperationResult(this.response, !this._isNot);
11
13
  }
12
14
  toBeAffectedExact(expected) {
13
15
  try {
14
- expect(this.response.body.affected).toStrictEqual(expected);
16
+ this._expect(this.response.data.affected).toStrictEqual(expected);
15
17
  }
16
18
  catch (e) {
17
19
  Error.captureStackTrace(e, this.toBeAffectedExact);
@@ -21,7 +23,7 @@ class ApiExpectOperationResult {
21
23
  }
22
24
  toBeAffectedMin(expected) {
23
25
  try {
24
- expect(this.response.body.affected).toBeGreaterThanOrEqual(expected);
26
+ this._expect(this.response.data.affected).toBeGreaterThanOrEqual(expected);
25
27
  }
26
28
  catch (e) {
27
29
  Error.captureStackTrace(e, this.toBeAffectedMin);
@@ -31,7 +33,7 @@ class ApiExpectOperationResult {
31
33
  }
32
34
  toBeAffectedMax(expected) {
33
35
  try {
34
- expect(this.response.body.affected).toBeLessThanOrEqual(expected);
36
+ this._expect(this.response.data.affected).toBeLessThanOrEqual(expected);
35
37
  }
36
38
  catch (e) {
37
39
  Error.captureStackTrace(e, this.toBeAffectedMax);
@@ -39,5 +41,11 @@ class ApiExpectOperationResult {
39
41
  }
40
42
  return this;
41
43
  }
44
+ _expect(expected) {
45
+ const out = expect(expected);
46
+ if (this._isNot)
47
+ return out.not;
48
+ return out;
49
+ }
42
50
  }
43
51
  exports.ApiExpectOperationResult = ApiExpectOperationResult;
@@ -1,24 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiExpect = void 0;
4
- require("./jest-extend/common.extend.js");
5
- const api_expect_array_js_1 = require("./api-expect-array.js");
4
+ const api_expect_collection_js_1 = require("./api-expect-collection.js");
6
5
  const api_expect_error_js_1 = require("./api-expect-error.js");
7
6
  const api_expect_object_js_1 = require("./api-expect-object.js");
8
7
  const api_expect_operation_result_js_1 = require("./api-expect-operation-result.js");
9
8
  class ApiExpect {
10
9
  response;
11
- constructor(response) {
10
+ _isNot;
11
+ constructor(response, _isNot = false) {
12
12
  this.response = response;
13
+ this._isNot = _isNot;
13
14
  }
14
- get body() {
15
- return this.response.body;
15
+ get not() {
16
+ return new ApiExpect(this.response, !this._isNot);
16
17
  }
17
18
  toSuccess(status = 200) {
18
- let msg = ';';
19
+ let msg = '';
19
20
  try {
20
- msg = 'Response "status" is not valid';
21
- expect(this.response.status).toStrictEqual(status);
21
+ msg = 'Unexpected "status" returned';
22
+ this._expect(this.response.status).toStrictEqual(status);
22
23
  }
23
24
  catch (e) {
24
25
  if (msg)
@@ -29,7 +30,7 @@ class ApiExpect {
29
30
  return this;
30
31
  }
31
32
  toFail(status = 400) {
32
- let msg = ';';
33
+ let msg = '';
33
34
  try {
34
35
  msg = 'Response "status" does not match';
35
36
  if (status) {
@@ -40,8 +41,8 @@ class ApiExpect {
40
41
  expect(this.response.status).toBeLessThanOrEqual(599);
41
42
  }
42
43
  msg = 'Api did not returned "errors"';
43
- expect(this.response.body.errors).toBeArray();
44
- expect(this.response.body.errors.length).toBeGreaterThan(0);
44
+ expect(this.response.data.errors).toBeArray();
45
+ expect(this.response.data.errors.length).toBeGreaterThan(0);
45
46
  }
46
47
  catch (e) {
47
48
  if (msg)
@@ -52,26 +53,26 @@ class ApiExpect {
52
53
  return new api_expect_error_js_1.ApiExpectError(this.response);
53
54
  }
54
55
  toReturnOperationResult() {
55
- let msg = ';';
56
+ let msg = '';
56
57
  try {
57
58
  msg = '"body" is empty';
58
- expect(this.response.body).toBeDefined();
59
+ expect(this.response.data).toBeDefined();
59
60
  msg = '"operation" property is empty';
60
- expect(this.response.body.operation).toBeDefined();
61
+ expect(this.response.data.operation).toBeDefined();
61
62
  }
62
63
  catch (e) {
63
64
  if (msg)
64
65
  e.message = msg + '\n\n' + e.message;
65
- Error.captureStackTrace(e, this.toReturnObject);
66
+ Error.captureStackTrace(e, this.toReturnOperationResult);
66
67
  throw e;
67
68
  }
68
69
  return new api_expect_operation_result_js_1.ApiExpectOperationResult(this.response);
69
70
  }
70
71
  toReturnObject() {
71
- let msg = ';';
72
+ let msg = '';
72
73
  try {
73
74
  msg = '"body" is empty';
74
- expect(this.response.body).toBeDefined();
75
+ expect(this.response.data).toBeDefined();
75
76
  }
76
77
  catch (e) {
77
78
  if (msg)
@@ -81,21 +82,27 @@ class ApiExpect {
81
82
  }
82
83
  return new api_expect_object_js_1.ApiExpectObject(this.response);
83
84
  }
84
- toReturnArray() {
85
- let msg = ';';
85
+ toReturnCollection() {
86
+ let msg = '';
86
87
  try {
87
88
  msg = '"body" is empty';
88
- expect(this.response.body).toBeDefined();
89
+ expect(this.response.data).toBeDefined();
89
90
  msg = '"body" is not an array';
90
- expect(this.response.body).toBeArray();
91
+ expect(this.response.data).toBeArray();
91
92
  }
92
93
  catch (e) {
93
94
  if (msg)
94
95
  e.message = msg + '\n\n' + e.message;
95
- Error.captureStackTrace(e, this.toReturnArray);
96
+ Error.captureStackTrace(e, this.toReturnCollection);
96
97
  throw e;
97
98
  }
98
- return new api_expect_array_js_1.ApiExpectArray(this.response);
99
+ return new api_expect_collection_js_1.ApiExpectCollection(this.response);
100
+ }
101
+ _expect(expected) {
102
+ const out = expect(expected);
103
+ if (this._isNot)
104
+ return out.not;
105
+ return out;
99
106
  }
100
107
  }
101
108
  exports.ApiExpect = ApiExpect;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expectSymbolTag = void 0;
4
+ exports.expectSymbolTag = Symbol('opra.test.expect');
package/cjs/index.js CHANGED
@@ -1,24 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraTester = exports.opraTestClient = void 0;
4
- const base_tester_js_1 = require("./testers/base-tester.js");
5
- const entity_tester_js_1 = require("./testers/entity-tester.js");
6
- function opraTestClient(app, options) {
7
- return new OpraTester({
8
- app,
9
- ...options,
10
- prefix: options?.prefix || '',
11
- headers: options?.headers || {}
12
- });
13
- }
14
- exports.opraTestClient = opraTestClient;
15
- class OpraTester extends base_tester_js_1.BaseTester {
16
- entity(path) {
17
- return new entity_tester_js_1.OpraEntityTester({
18
- ...this._params,
19
- headers: { ...this._params.headers },
20
- path
21
- });
22
- }
23
- }
24
- exports.OpraTester = OpraTester;
3
+ const tslib_1 = require("tslib");
4
+ require("./jest-extend/common.extend.js");
5
+ tslib_1.__exportStar(require("./test-client.js"), exports);
@@ -1,32 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
3
5
  const jest_matcher_utils_1 = require("jest-matcher-utils");
4
6
  expect.extend({
5
- toContainKeys(received, expected) {
6
- if (typeof received === 'object') {
7
- const keys = Array.isArray(expected) ? expected : Object.keys(expected);
8
- const additionalKeys = Object.keys(received).filter(x => !keys.includes(x));
9
- if (!Object.keys(received).find(x => keys.includes(x))) {
10
- return {
11
- pass: false,
12
- message: () => `Object contains unexpected additional keys (${additionalKeys})`
13
- };
14
- }
7
+ toHaveFields(received, expected) {
8
+ const expectedKeys = (Array.isArray(expected) ? expected : Object.keys(expected)).map(x => x.toLowerCase());
9
+ const objectKeys = Object.keys(received).map(x => x.toLowerCase());
10
+ const filteredKeys = expectedKeys.filter(x => !objectKeys.includes(x));
11
+ const pass = !filteredKeys.length === !this.isNot;
12
+ if (!pass) {
13
+ const message = () => `Expects keys to${this.isNot ? ' not' : ''} contain: ${ansi_colors_1.default.yellow('' + expectedKeys)}\n` +
14
+ `${this.isNot ? 'Unsolicited' : 'Missing'} fields: ${ansi_colors_1.default.yellow('' + filteredKeys)}\n`;
15
+ return { message, pass: !!this.isNot };
15
16
  }
16
- return { actual: received, pass: true, message: () => '' };
17
+ return { actual: received, pass: !this.isNot, message: () => '' };
17
18
  },
18
- toContainAllKeys(received, expected) {
19
- if (typeof received === 'object') {
20
- const keys = Array.isArray(expected) ? expected : Object.keys(expected);
21
- const additionalKeys = Object.keys(received).filter(x => !keys.includes(x));
22
- if (additionalKeys.length) {
23
- return {
24
- pass: false,
25
- message: () => `Object contains unexpected additional keys (${additionalKeys})`
26
- };
27
- }
19
+ toHaveFieldsOnly(received, expected) {
20
+ const expectedKeys = (Array.isArray(expected) ? expected : Object.keys(expected)).map(x => x.toLowerCase());
21
+ const objectKeys = Object.keys(received).map(x => x.toLowerCase());
22
+ const filteredKeys = objectKeys.filter(x => !expectedKeys.includes(x));
23
+ const pass = !filteredKeys.length === !this.isNot;
24
+ if (!pass) {
25
+ const message = () => `${!this.isNot ? 'Do not expects' : 'Expects'} additional keys other than: ${ansi_colors_1.default.yellow('' + expectedKeys)}\n` +
26
+ (filteredKeys ? `Additional keys received: ${ansi_colors_1.default.yellow('' + filteredKeys)}\n` :
27
+ 'No additional keys received\n');
28
+ return { message, pass };
28
29
  }
29
- return { actual: received, pass: true, message: () => '' };
30
+ return { actual: received, pass: !this.isNot, message: () => '' };
30
31
  },
31
32
  toBeArray(received) {
32
33
  if (Array.isArray(received)) {
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraTestClient = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const axiosist = tslib_1.__importStar(require("axiosist"));
6
+ const client_1 = require("@opra/client");
7
+ const api_expect_js_1 = require("./api-expect/api-expect.js");
8
+ class OpraTestClient extends client_1.OpraClient {
9
+ constructor(app, arg1, arg2) {
10
+ super('/', arg1, arg2);
11
+ this._axios.defaults.adapter = axiosist.createAdapter(app);
12
+ this._axios.defaults.validateStatus = () => true;
13
+ }
14
+ // @ts-ignore
15
+ collection(name) {
16
+ return super.collection(name);
17
+ }
18
+ // @ts-ignore
19
+ singleton(name) {
20
+ return super.singleton(name);
21
+ }
22
+ // @ts-ignore
23
+ async _send(req) {
24
+ const resp = (await super._send(req));
25
+ resp.expect = new api_expect_js_1.ApiExpect(resp);
26
+ return resp;
27
+ }
28
+ static async create(app, options) {
29
+ const client = new this(app, options);
30
+ await client.init();
31
+ return client;
32
+ }
33
+ }
34
+ exports.OpraTestClient = OpraTestClient;
@@ -0,0 +1,19 @@
1
+ import { ClientResponse } from '@opra/client';
2
+ import { Expression } from '@opra/url';
3
+ export declare class ApiExpectCollection {
4
+ readonly response: ClientResponse;
5
+ protected _isNot: boolean;
6
+ constructor(response: ClientResponse, _isNot?: boolean);
7
+ get not(): ApiExpectCollection;
8
+ forEach(callbackfn: (v: any) => void): this;
9
+ toMatch<T extends {}>(expected: T): this;
10
+ toHaveFields(keys: string[]): this;
11
+ toHaveFieldsOnly(keys: string[]): this;
12
+ toBeSortedBy(...fields: string[]): this;
13
+ toBeFilteredBy(filter: string | Expression): this;
14
+ toHaveExactItems(expected: number): this;
15
+ toHaveMaxItems(expected: number): this;
16
+ toHaveMinItems(expected: number): this;
17
+ protected _expect(expected: any): jest.Matchers<any>;
18
+ }
19
+ export declare function convertFilter(str: string | Expression | undefined): any;