@opra/testing 0.0.9

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 (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/cjs/expect/api-expect-body.js +37 -0
  4. package/cjs/expect/api-expect-errors.js +17 -0
  5. package/cjs/expect/api-expect-list.js +32 -0
  6. package/cjs/expect/api-expect-resource.js +31 -0
  7. package/cjs/expect/api-expect.js +37 -0
  8. package/cjs/expect/jest-extend.js +92 -0
  9. package/cjs/index.js +29 -0
  10. package/cjs/package.json +3 -0
  11. package/cjs/testers/base-tester.js +23 -0
  12. package/cjs/testers/entity-create-tester.js +43 -0
  13. package/cjs/testers/entity-get-tester.js +44 -0
  14. package/cjs/testers/entity-tester.js +28 -0
  15. package/esm/expect/api-expect-body.d.ts +6 -0
  16. package/esm/expect/api-expect-body.js +32 -0
  17. package/esm/expect/api-expect-errors.d.ts +6 -0
  18. package/esm/expect/api-expect-errors.js +13 -0
  19. package/esm/expect/api-expect-list.d.ts +10 -0
  20. package/esm/expect/api-expect-list.js +28 -0
  21. package/esm/expect/api-expect-resource.d.ts +10 -0
  22. package/esm/expect/api-expect-resource.js +27 -0
  23. package/esm/expect/api-expect.d.ts +16 -0
  24. package/esm/expect/api-expect.js +33 -0
  25. package/esm/expect/jest-extend.d.ts +25 -0
  26. package/esm/expect/jest-extend.js +90 -0
  27. package/esm/index.d.ts +9 -0
  28. package/esm/index.js +23 -0
  29. package/esm/testers/base-tester.d.ts +13 -0
  30. package/esm/testers/base-tester.js +19 -0
  31. package/esm/testers/entity-create-tester.d.ts +17 -0
  32. package/esm/testers/entity-create-tester.js +38 -0
  33. package/esm/testers/entity-get-tester.d.ts +18 -0
  34. package/esm/testers/entity-get-tester.js +39 -0
  35. package/esm/testers/entity-tester.d.ts +14 -0
  36. package/esm/testers/entity-tester.js +24 -0
  37. package/package.json +63 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @opra/I18n
2
+
3
+ OPRA i18n package.
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpectBody = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
6
+ class ApiExpectBody {
7
+ _toMatchObject(actuals, expected) {
8
+ const v = lodash_1.default.omitBy(expected, lodash_1.default.isNil);
9
+ for (const actual of actuals)
10
+ expect(actual).toMatchObject(v);
11
+ return this;
12
+ }
13
+ _haveKeysOnly(actuals, keys) {
14
+ for (const actual of actuals)
15
+ expect(actual).toEqual(expect.objectHaveKeysOnly(keys));
16
+ return this;
17
+ }
18
+ _haveKeys(actuals, keys) {
19
+ const matcher = keys.reduce((a, k) => {
20
+ a[k] = expect.anything();
21
+ return a;
22
+ }, {});
23
+ for (const actual of actuals)
24
+ expect(actual).toEqual(expect.objectContaining(matcher));
25
+ return this;
26
+ }
27
+ _notHaveKeys(actuals, keys) {
28
+ const matcher = keys.reduce((a, k) => {
29
+ a[k] = expect.anything();
30
+ return a;
31
+ }, {});
32
+ for (const actual of actuals)
33
+ expect(actual).not.toEqual(expect.objectContaining(matcher));
34
+ return this;
35
+ }
36
+ }
37
+ exports.ApiExpectBody = ApiExpectBody;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpectErrors = void 0;
4
+ const api_expect_body_js_1 = require("./api-expect-body.js");
5
+ class ApiExpectErrors extends api_expect_body_js_1.ApiExpectBody {
6
+ _body;
7
+ constructor(_body) {
8
+ super();
9
+ this._body = _body;
10
+ }
11
+ toBeDefined() {
12
+ expect(this._body.errors).toBeDefined();
13
+ expect(this._body.errors.length).toBeGreaterThan(0);
14
+ return this;
15
+ }
16
+ }
17
+ exports.ApiExpectErrors = ApiExpectErrors;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpectList = void 0;
4
+ const api_expect_body_js_1 = require("./api-expect-body.js");
5
+ class ApiExpectList extends api_expect_body_js_1.ApiExpectBody {
6
+ _body;
7
+ constructor(_body) {
8
+ super();
9
+ this._body = _body;
10
+ }
11
+ toBeDefined() {
12
+ expect(this._body).toBeDefined();
13
+ expect(this._body.items).toBeDefined();
14
+ return this;
15
+ }
16
+ toMatchObject(value) {
17
+ return this._toMatchObject(this._body.items || [], value);
18
+ }
19
+ haveKeysOnly(keys) {
20
+ this._haveKeysOnly(this._body.items || [], keys);
21
+ return this;
22
+ }
23
+ haveKeys(keys) {
24
+ this._haveKeys(this._body.items || [], keys);
25
+ return this;
26
+ }
27
+ notHaveKeys(keys) {
28
+ this._notHaveKeys(this._body.items || [], keys);
29
+ return this;
30
+ }
31
+ }
32
+ exports.ApiExpectList = ApiExpectList;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpectResource = void 0;
4
+ const api_expect_body_js_1 = require("./api-expect-body.js");
5
+ class ApiExpectResource extends api_expect_body_js_1.ApiExpectBody {
6
+ _body;
7
+ constructor(_body) {
8
+ super();
9
+ this._body = _body;
10
+ }
11
+ toBeDefined() {
12
+ expect(this._body).toBeDefined();
13
+ return this;
14
+ }
15
+ toMatchObject(value) {
16
+ return this._toMatchObject([this._body], value);
17
+ }
18
+ haveKeysOnly(keys) {
19
+ this._haveKeysOnly([this._body], keys);
20
+ return this;
21
+ }
22
+ haveKeys(keys) {
23
+ this._haveKeys([this._body], keys);
24
+ return this;
25
+ }
26
+ notHaveKeys(keys) {
27
+ this._notHaveKeys([this._body], keys);
28
+ return this;
29
+ }
30
+ }
31
+ exports.ApiExpectResource = ApiExpectResource;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiExpect = void 0;
4
+ const api_expect_errors_js_1 = require("./api-expect-errors.js");
5
+ const api_expect_list_js_1 = require("./api-expect-list.js");
6
+ const api_expect_resource_js_1 = require("./api-expect-resource.js");
7
+ class ApiExpect {
8
+ response;
9
+ _bodyResource;
10
+ _bodyList;
11
+ _bodyErrors;
12
+ constructor(response) {
13
+ this.response = response;
14
+ }
15
+ status(value) {
16
+ expect(this.response.status).toEqual(value);
17
+ return this;
18
+ }
19
+ get bodyAsResource() {
20
+ return this._bodyResource =
21
+ this._bodyResource || new api_expect_resource_js_1.ApiExpectResource(this.response.body);
22
+ }
23
+ get bodyAsList() {
24
+ return this._bodyList =
25
+ this._bodyList || new api_expect_list_js_1.ApiExpectList(this.response.body);
26
+ }
27
+ toSuccess(status = 200) {
28
+ expect(this.response.body.errors).toStrictEqual(undefined);
29
+ expect(this.response.status).toEqual(status);
30
+ return this;
31
+ }
32
+ get errors() {
33
+ return this._bodyErrors =
34
+ this._bodyErrors || new api_expect_errors_js_1.ApiExpectErrors(this.response.body);
35
+ }
36
+ }
37
+ exports.ApiExpect = ApiExpect;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jest_matcher_utils_1 = require("jest-matcher-utils");
4
+ expect.extend({
5
+ objectHaveKeysOnly(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 (additionalKeys.length) {
10
+ return {
11
+ pass: false,
12
+ message: () => `Object contains unexpected additional keys (${additionalKeys})`
13
+ };
14
+ }
15
+ }
16
+ return { actual: received, pass: true, message: () => '' };
17
+ },
18
+ objectMatches(received, expected) {
19
+ if (typeof received === 'object') {
20
+ const keys = Object.keys(expected);
21
+ for (const k of keys) {
22
+ const v = k.split('.').reduce((a, b) => a[b], received);
23
+ try {
24
+ const matching = expected[k];
25
+ if (typeof matching === 'function')
26
+ matching(v);
27
+ else
28
+ expect(v).toEqual(matching);
29
+ }
30
+ catch (e) {
31
+ return {
32
+ pass: false,
33
+ message: () => `${k} does not match: ${e.message}`
34
+ };
35
+ }
36
+ }
37
+ }
38
+ return { actual: received, pass: true, message: () => '' };
39
+ },
40
+ toBeSorted(received, compareFn) {
41
+ let pass = Array.isArray(received);
42
+ let message;
43
+ if (pass) {
44
+ const sorted = [...received];
45
+ sorted.sort(compareFn);
46
+ try {
47
+ expect(received).toEqual(sorted);
48
+ }
49
+ catch (e) {
50
+ pass = false;
51
+ message = () => 'Array items is not sorted as expected';
52
+ }
53
+ }
54
+ return {
55
+ actual: received,
56
+ message,
57
+ pass
58
+ };
59
+ },
60
+ toBeGreaterThanAny(received, expected) {
61
+ return compare('toBeGreaterThan', {
62
+ isNot: this.isNot,
63
+ promise: this.promise
64
+ }, received, expected, '>', () => received > expected);
65
+ },
66
+ toBeGreaterThanOrEqualAny(received, expected) {
67
+ return compare('toBeGreaterThanOrEqual', {
68
+ isNot: this.isNot,
69
+ promise: this.promise
70
+ }, received, expected, '>=', () => received >= expected);
71
+ },
72
+ toBeLessThanAny(received, expected) {
73
+ return compare('toBeLessThan', {
74
+ isNot: this.isNot,
75
+ promise: this.promise
76
+ }, received, expected, '<', () => received < expected);
77
+ },
78
+ toBeLessThanOrEqualAny(received, expected) {
79
+ return compare('toBeLessThanOrEqual', {
80
+ isNot: this.isNot,
81
+ promise: this.promise
82
+ }, received, expected, '<=', () => received <= expected);
83
+ }
84
+ });
85
+ function compare(matcherName, options, received, expected, operator, fn) {
86
+ const pass = fn(received, expected);
87
+ const message = () => (0, jest_matcher_utils_1.matcherHint)(matcherName, undefined, undefined, options) +
88
+ '\n\n' +
89
+ `Expected:${options.isNot ? ' not' : ''} ${operator} ${(0, jest_matcher_utils_1.printExpected)(expected)}\n` +
90
+ `Received:${options.isNot ? ' ' : ''} ${(0, jest_matcher_utils_1.printReceived)(received)}`;
91
+ return { message, pass };
92
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraTester = exports.apiExpect = exports.opraTest = void 0;
4
+ const api_expect_js_1 = require("./expect/api-expect.js");
5
+ const base_tester_js_1 = require("./testers/base-tester.js");
6
+ const entity_tester_js_1 = require("./testers/entity-tester.js");
7
+ function opraTest(app, options) {
8
+ return new OpraTester({
9
+ app,
10
+ ...options,
11
+ prefix: options?.prefix || '',
12
+ headers: options?.headers || {}
13
+ });
14
+ }
15
+ exports.opraTest = opraTest;
16
+ function apiExpect(response) {
17
+ return new api_expect_js_1.ApiExpect(response);
18
+ }
19
+ exports.apiExpect = apiExpect;
20
+ class OpraTester extends base_tester_js_1.BaseTester {
21
+ entity(path) {
22
+ return new entity_tester_js_1.OpraEntityTester({
23
+ ...this._params,
24
+ headers: { ...this._params.headers },
25
+ path
26
+ });
27
+ }
28
+ }
29
+ exports.OpraTester = OpraTester;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseTester = void 0;
4
+ class BaseTester {
5
+ _params;
6
+ constructor(params) {
7
+ this._params = params;
8
+ }
9
+ prefix(value) {
10
+ this._params.prefix = value;
11
+ return this;
12
+ }
13
+ header(name, value) {
14
+ this._params.headers = this._params.headers || {};
15
+ this._params.headers[name] = value;
16
+ }
17
+ _prepare(test) {
18
+ const headers = this._params.headers;
19
+ if (headers)
20
+ Object.keys(headers).forEach(k => test.set(k, headers[k]));
21
+ }
22
+ }
23
+ exports.BaseTester = BaseTester;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraEntityCreateTester = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
6
+ const url_1 = require("@opra/url");
7
+ const base_tester_js_1 = require("./base-tester.js");
8
+ class OpraEntityCreateTester extends base_tester_js_1.BaseTester {
9
+ constructor(params) {
10
+ super(params);
11
+ }
12
+ async send() {
13
+ const url = new url_1.OpraURL(this._params.path);
14
+ url.pathPrefix = this._params.prefix;
15
+ if (this._params.options.include)
16
+ url.searchParams.set('$include', this._params.options.include);
17
+ if (this._params.options.pick)
18
+ url.searchParams.set('$pick', this._params.options.pick);
19
+ if (this._params.options.omit)
20
+ url.searchParams.set('$omit', this._params.options.omit);
21
+ const req = (0, supertest_1.default)(this._params.app);
22
+ const test = req.post(url.toString());
23
+ this._prepare(test);
24
+ return test.send(this._params.data);
25
+ }
26
+ data(data) {
27
+ this._params.data = data;
28
+ return this;
29
+ }
30
+ omit(...fields) {
31
+ this._params.options.omit = fields.flat();
32
+ return this;
33
+ }
34
+ pick(...fields) {
35
+ this._params.options.pick = fields.flat();
36
+ return this;
37
+ }
38
+ include(...fields) {
39
+ this._params.options.include = fields.flat();
40
+ return this;
41
+ }
42
+ }
43
+ exports.OpraEntityCreateTester = OpraEntityCreateTester;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraEntityGetTester = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
6
+ const url_1 = require("@opra/url");
7
+ const base_tester_js_1 = require("./base-tester.js");
8
+ class OpraEntityGetTester extends base_tester_js_1.BaseTester {
9
+ constructor(params) {
10
+ super(params);
11
+ }
12
+ async send() {
13
+ const url = new url_1.OpraURL(this._params.path);
14
+ url.pathPrefix = this._params.prefix;
15
+ url.path.get(url.path.size - 1).key = this._params.keyValue;
16
+ if (this._params.options.include)
17
+ url.searchParams.set('$include', this._params.options.include);
18
+ if (this._params.options.pick)
19
+ url.searchParams.set('$pick', this._params.options.pick);
20
+ if (this._params.options.omit)
21
+ url.searchParams.set('$omit', this._params.options.omit);
22
+ const req = (0, supertest_1.default)(this._params.app);
23
+ const test = req.get(url.toString());
24
+ this._prepare(test);
25
+ return test.send();
26
+ }
27
+ keyValue(value) {
28
+ this._params.keyValue = value;
29
+ return this;
30
+ }
31
+ omit(...fields) {
32
+ this._params.options.omit = fields.flat();
33
+ return this;
34
+ }
35
+ pick(...fields) {
36
+ this._params.options.pick = fields.flat();
37
+ return this;
38
+ }
39
+ include(...fields) {
40
+ this._params.options.include = fields.flat();
41
+ return this;
42
+ }
43
+ }
44
+ exports.OpraEntityGetTester = OpraEntityGetTester;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpraEntityTester = void 0;
4
+ const base_tester_js_1 = require("./base-tester.js");
5
+ const entity_create_tester_js_1 = require("./entity-create-tester.js");
6
+ const entity_get_tester_js_1 = require("./entity-get-tester.js");
7
+ class OpraEntityTester extends base_tester_js_1.BaseTester {
8
+ constructor(params) {
9
+ super(params);
10
+ }
11
+ get(keyValue, options = {}) {
12
+ return new entity_get_tester_js_1.OpraEntityGetTester({
13
+ ...this._params,
14
+ headers: { ...this._params.headers },
15
+ keyValue,
16
+ options
17
+ });
18
+ }
19
+ create(data, options = {}) {
20
+ return new entity_create_tester_js_1.OpraEntityCreateTester({
21
+ ...this._params,
22
+ headers: { ...this._params.headers },
23
+ data,
24
+ options
25
+ });
26
+ }
27
+ }
28
+ exports.OpraEntityTester = OpraEntityTester;
@@ -0,0 +1,6 @@
1
+ export declare class ApiExpectBody {
2
+ protected _toMatchObject<T extends {}>(actuals: any[], expected: T): this;
3
+ protected _haveKeysOnly(actuals: any[], keys: string[]): this;
4
+ protected _haveKeys(actuals: any[], keys: string[]): this;
5
+ protected _notHaveKeys(actuals: any[], keys: string[]): this;
6
+ }
@@ -0,0 +1,32 @@
1
+ import _ from 'lodash';
2
+ export class ApiExpectBody {
3
+ _toMatchObject(actuals, expected) {
4
+ const v = _.omitBy(expected, _.isNil);
5
+ for (const actual of actuals)
6
+ expect(actual).toMatchObject(v);
7
+ return this;
8
+ }
9
+ _haveKeysOnly(actuals, keys) {
10
+ for (const actual of actuals)
11
+ expect(actual).toEqual(expect.objectHaveKeysOnly(keys));
12
+ return this;
13
+ }
14
+ _haveKeys(actuals, keys) {
15
+ const matcher = keys.reduce((a, k) => {
16
+ a[k] = expect.anything();
17
+ return a;
18
+ }, {});
19
+ for (const actual of actuals)
20
+ expect(actual).toEqual(expect.objectContaining(matcher));
21
+ return this;
22
+ }
23
+ _notHaveKeys(actuals, keys) {
24
+ const matcher = keys.reduce((a, k) => {
25
+ a[k] = expect.anything();
26
+ return a;
27
+ }, {});
28
+ for (const actual of actuals)
29
+ expect(actual).not.toEqual(expect.objectContaining(matcher));
30
+ return this;
31
+ }
32
+ }
@@ -0,0 +1,6 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export declare class ApiExpectErrors extends ApiExpectBody {
3
+ protected _body: any;
4
+ constructor(_body: any);
5
+ toBeDefined(): this;
6
+ }
@@ -0,0 +1,13 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export class ApiExpectErrors extends ApiExpectBody {
3
+ _body;
4
+ constructor(_body) {
5
+ super();
6
+ this._body = _body;
7
+ }
8
+ toBeDefined() {
9
+ expect(this._body.errors).toBeDefined();
10
+ expect(this._body.errors.length).toBeGreaterThan(0);
11
+ return this;
12
+ }
13
+ }
@@ -0,0 +1,10 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export declare class ApiExpectList extends ApiExpectBody {
3
+ protected _body: any;
4
+ constructor(_body: any);
5
+ toBeDefined(): this;
6
+ toMatchObject<T extends {}>(value: T): this;
7
+ haveKeysOnly(keys: string[]): this;
8
+ haveKeys(keys: string[]): this;
9
+ notHaveKeys(keys: string[]): this;
10
+ }
@@ -0,0 +1,28 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export class ApiExpectList extends ApiExpectBody {
3
+ _body;
4
+ constructor(_body) {
5
+ super();
6
+ this._body = _body;
7
+ }
8
+ toBeDefined() {
9
+ expect(this._body).toBeDefined();
10
+ expect(this._body.items).toBeDefined();
11
+ return this;
12
+ }
13
+ toMatchObject(value) {
14
+ return this._toMatchObject(this._body.items || [], value);
15
+ }
16
+ haveKeysOnly(keys) {
17
+ this._haveKeysOnly(this._body.items || [], keys);
18
+ return this;
19
+ }
20
+ haveKeys(keys) {
21
+ this._haveKeys(this._body.items || [], keys);
22
+ return this;
23
+ }
24
+ notHaveKeys(keys) {
25
+ this._notHaveKeys(this._body.items || [], keys);
26
+ return this;
27
+ }
28
+ }
@@ -0,0 +1,10 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export declare class ApiExpectResource extends ApiExpectBody {
3
+ protected _body: any;
4
+ constructor(_body: any);
5
+ toBeDefined(): this;
6
+ toMatchObject<T extends {}>(value: T): this;
7
+ haveKeysOnly(keys: string[]): this;
8
+ haveKeys(keys: string[]): this;
9
+ notHaveKeys(keys: string[]): this;
10
+ }
@@ -0,0 +1,27 @@
1
+ import { ApiExpectBody } from './api-expect-body.js';
2
+ export class ApiExpectResource extends ApiExpectBody {
3
+ _body;
4
+ constructor(_body) {
5
+ super();
6
+ this._body = _body;
7
+ }
8
+ toBeDefined() {
9
+ expect(this._body).toBeDefined();
10
+ return this;
11
+ }
12
+ toMatchObject(value) {
13
+ return this._toMatchObject([this._body], value);
14
+ }
15
+ haveKeysOnly(keys) {
16
+ this._haveKeysOnly([this._body], keys);
17
+ return this;
18
+ }
19
+ haveKeys(keys) {
20
+ this._haveKeys([this._body], keys);
21
+ return this;
22
+ }
23
+ notHaveKeys(keys) {
24
+ this._notHaveKeys([this._body], keys);
25
+ return this;
26
+ }
27
+ }
@@ -0,0 +1,16 @@
1
+ import { Response } from 'supertest';
2
+ import { ApiExpectErrors } from './api-expect-errors.js';
3
+ import { ApiExpectList } from './api-expect-list.js';
4
+ import { ApiExpectResource } from './api-expect-resource.js';
5
+ export declare class ApiExpect {
6
+ readonly response: Response;
7
+ protected _bodyResource?: ApiExpectResource;
8
+ protected _bodyList?: ApiExpectList;
9
+ protected _bodyErrors?: ApiExpectErrors;
10
+ constructor(response: Response);
11
+ status(value: number): this;
12
+ get bodyAsResource(): ApiExpectResource;
13
+ get bodyAsList(): ApiExpectList;
14
+ toSuccess(status?: number): this;
15
+ get errors(): ApiExpectErrors;
16
+ }
@@ -0,0 +1,33 @@
1
+ import { ApiExpectErrors } from './api-expect-errors.js';
2
+ import { ApiExpectList } from './api-expect-list.js';
3
+ import { ApiExpectResource } from './api-expect-resource.js';
4
+ export class ApiExpect {
5
+ response;
6
+ _bodyResource;
7
+ _bodyList;
8
+ _bodyErrors;
9
+ constructor(response) {
10
+ this.response = response;
11
+ }
12
+ status(value) {
13
+ expect(this.response.status).toEqual(value);
14
+ return this;
15
+ }
16
+ get bodyAsResource() {
17
+ return this._bodyResource =
18
+ this._bodyResource || new ApiExpectResource(this.response.body);
19
+ }
20
+ get bodyAsList() {
21
+ return this._bodyList =
22
+ this._bodyList || new ApiExpectList(this.response.body);
23
+ }
24
+ toSuccess(status = 200) {
25
+ expect(this.response.body.errors).toStrictEqual(undefined);
26
+ expect(this.response.status).toEqual(status);
27
+ return this;
28
+ }
29
+ get errors() {
30
+ return this._bodyErrors =
31
+ this._bodyErrors || new ApiExpectErrors(this.response.body);
32
+ }
33
+ }
@@ -0,0 +1,25 @@
1
+ declare global {
2
+ namespace jest {
3
+ interface Expect {
4
+ objectHaveKeysOnly(expected: string[]): any;
5
+ objectMatches(expected: Record<string, any>): any;
6
+ }
7
+ interface Matchers<R> {
8
+ toBeSorted(compareFn?: (a: any, b: any) => number): any;
9
+ toBeGreaterThanAny(expected: number | bigint | string | Date): any;
10
+ toBeGreaterThanOrEqualAny(expected: number | bigint | string | Date): any;
11
+ toBeLessThanAny(expected: number | bigint | string | Date): any;
12
+ toBeLessThanOrEqualAny(expected: number | bigint | string | Date): any;
13
+ }
14
+ interface InverseAsymmetricMatchers {
15
+ objectHaveKeysOnly(expected: string[]): any;
16
+ objectMatches(expected: Record<string, any>): any;
17
+ toBeSorted(compareFn?: (a: any, b: any) => number): any;
18
+ toBeGreaterThanAny(expected: number | bigint | string | Date): any;
19
+ toBeGreaterThanOrEqualAny(expected: number | bigint | string | Date): any;
20
+ toBeLessThanAny(expected: number | bigint | string | Date): any;
21
+ toBeLessThanOrEqualAny(expected: number | bigint | string | Date): any;
22
+ }
23
+ }
24
+ }
25
+ export {};
@@ -0,0 +1,90 @@
1
+ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
2
+ expect.extend({
3
+ objectHaveKeysOnly(received, expected) {
4
+ if (typeof received === 'object') {
5
+ const keys = Array.isArray(expected) ? expected : Object.keys(expected);
6
+ const additionalKeys = Object.keys(received).filter(x => !keys.includes(x));
7
+ if (additionalKeys.length) {
8
+ return {
9
+ pass: false,
10
+ message: () => `Object contains unexpected additional keys (${additionalKeys})`
11
+ };
12
+ }
13
+ }
14
+ return { actual: received, pass: true, message: () => '' };
15
+ },
16
+ objectMatches(received, expected) {
17
+ if (typeof received === 'object') {
18
+ const keys = Object.keys(expected);
19
+ for (const k of keys) {
20
+ const v = k.split('.').reduce((a, b) => a[b], received);
21
+ try {
22
+ const matching = expected[k];
23
+ if (typeof matching === 'function')
24
+ matching(v);
25
+ else
26
+ expect(v).toEqual(matching);
27
+ }
28
+ catch (e) {
29
+ return {
30
+ pass: false,
31
+ message: () => `${k} does not match: ${e.message}`
32
+ };
33
+ }
34
+ }
35
+ }
36
+ return { actual: received, pass: true, message: () => '' };
37
+ },
38
+ toBeSorted(received, compareFn) {
39
+ let pass = Array.isArray(received);
40
+ let message;
41
+ if (pass) {
42
+ const sorted = [...received];
43
+ sorted.sort(compareFn);
44
+ try {
45
+ expect(received).toEqual(sorted);
46
+ }
47
+ catch (e) {
48
+ pass = false;
49
+ message = () => 'Array items is not sorted as expected';
50
+ }
51
+ }
52
+ return {
53
+ actual: received,
54
+ message,
55
+ pass
56
+ };
57
+ },
58
+ toBeGreaterThanAny(received, expected) {
59
+ return compare('toBeGreaterThan', {
60
+ isNot: this.isNot,
61
+ promise: this.promise
62
+ }, received, expected, '>', () => received > expected);
63
+ },
64
+ toBeGreaterThanOrEqualAny(received, expected) {
65
+ return compare('toBeGreaterThanOrEqual', {
66
+ isNot: this.isNot,
67
+ promise: this.promise
68
+ }, received, expected, '>=', () => received >= expected);
69
+ },
70
+ toBeLessThanAny(received, expected) {
71
+ return compare('toBeLessThan', {
72
+ isNot: this.isNot,
73
+ promise: this.promise
74
+ }, received, expected, '<', () => received < expected);
75
+ },
76
+ toBeLessThanOrEqualAny(received, expected) {
77
+ return compare('toBeLessThanOrEqual', {
78
+ isNot: this.isNot,
79
+ promise: this.promise
80
+ }, received, expected, '<=', () => received <= expected);
81
+ }
82
+ });
83
+ function compare(matcherName, options, received, expected, operator, fn) {
84
+ const pass = fn(received, expected);
85
+ const message = () => matcherHint(matcherName, undefined, undefined, options) +
86
+ '\n\n' +
87
+ `Expected:${options.isNot ? ' not' : ''} ${operator} ${printExpected(expected)}\n` +
88
+ `Received:${options.isNot ? ' ' : ''} ${printReceived(received)}`;
89
+ return { message, pass };
90
+ }
package/esm/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Response } from 'supertest';
2
+ import { ApiExpect } from './expect/api-expect.js';
3
+ import { BaseTester, OpraTesterParams } from './testers/base-tester.js';
4
+ import { OpraEntityTester } from './testers/entity-tester.js';
5
+ export declare function opraTest(app: any, options?: Partial<Omit<OpraTesterParams, 'app'>>): OpraTester;
6
+ export declare function apiExpect(response: Response): ApiExpect;
7
+ export declare class OpraTester extends BaseTester {
8
+ entity(path: string): OpraEntityTester;
9
+ }
package/esm/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import { ApiExpect } from './expect/api-expect.js';
2
+ import { BaseTester } from './testers/base-tester.js';
3
+ import { OpraEntityTester } from './testers/entity-tester.js';
4
+ export function opraTest(app, options) {
5
+ return new OpraTester({
6
+ app,
7
+ ...options,
8
+ prefix: options?.prefix || '',
9
+ headers: options?.headers || {}
10
+ });
11
+ }
12
+ export function apiExpect(response) {
13
+ return new ApiExpect(response);
14
+ }
15
+ export class OpraTester extends BaseTester {
16
+ entity(path) {
17
+ return new OpraEntityTester({
18
+ ...this._params,
19
+ headers: { ...this._params.headers },
20
+ path
21
+ });
22
+ }
23
+ }
@@ -0,0 +1,13 @@
1
+ import { Test } from 'supertest';
2
+ export interface OpraTesterParams {
3
+ app: any;
4
+ prefix: string;
5
+ headers: Record<string, string>;
6
+ }
7
+ export declare class BaseTester {
8
+ protected readonly _params: OpraTesterParams;
9
+ constructor(params: OpraTesterParams);
10
+ prefix(value: string): this;
11
+ header(name: string, value: string): void;
12
+ protected _prepare(test: Test): void;
13
+ }
@@ -0,0 +1,19 @@
1
+ export class BaseTester {
2
+ _params;
3
+ constructor(params) {
4
+ this._params = params;
5
+ }
6
+ prefix(value) {
7
+ this._params.prefix = value;
8
+ return this;
9
+ }
10
+ header(name, value) {
11
+ this._params.headers = this._params.headers || {};
12
+ this._params.headers[name] = value;
13
+ }
14
+ _prepare(test) {
15
+ const headers = this._params.headers;
16
+ if (headers)
17
+ Object.keys(headers).forEach(k => test.set(k, headers[k]));
18
+ }
19
+ }
@@ -0,0 +1,17 @@
1
+ import { Response } from 'supertest';
2
+ import { CreateQueryOptions } from '@opra/core';
3
+ import { BaseTester } from './base-tester.js';
4
+ import type { OpraEntityTesterParams } from './entity-tester.js';
5
+ export declare type OpraEntityCreateTesterParams = OpraEntityTesterParams & {
6
+ data: {};
7
+ options: CreateQueryOptions;
8
+ };
9
+ export declare class OpraEntityCreateTester extends BaseTester {
10
+ protected readonly _params: OpraEntityCreateTesterParams;
11
+ constructor(params: OpraEntityCreateTesterParams);
12
+ send(): Promise<Response>;
13
+ data(data: {}): this;
14
+ omit(...fields: (string | string[])[]): this;
15
+ pick(...fields: (string | string[])[]): this;
16
+ include(...fields: (string | string[])[]): this;
17
+ }
@@ -0,0 +1,38 @@
1
+ import request from 'supertest';
2
+ import { OpraURL } from '@opra/url';
3
+ import { BaseTester } from './base-tester.js';
4
+ export class OpraEntityCreateTester extends BaseTester {
5
+ constructor(params) {
6
+ super(params);
7
+ }
8
+ async send() {
9
+ const url = new OpraURL(this._params.path);
10
+ url.pathPrefix = this._params.prefix;
11
+ if (this._params.options.include)
12
+ url.searchParams.set('$include', this._params.options.include);
13
+ if (this._params.options.pick)
14
+ url.searchParams.set('$pick', this._params.options.pick);
15
+ if (this._params.options.omit)
16
+ url.searchParams.set('$omit', this._params.options.omit);
17
+ const req = request(this._params.app);
18
+ const test = req.post(url.toString());
19
+ this._prepare(test);
20
+ return test.send(this._params.data);
21
+ }
22
+ data(data) {
23
+ this._params.data = data;
24
+ return this;
25
+ }
26
+ omit(...fields) {
27
+ this._params.options.omit = fields.flat();
28
+ return this;
29
+ }
30
+ pick(...fields) {
31
+ this._params.options.pick = fields.flat();
32
+ return this;
33
+ }
34
+ include(...fields) {
35
+ this._params.options.include = fields.flat();
36
+ return this;
37
+ }
38
+ }
@@ -0,0 +1,18 @@
1
+ import { Response } from 'supertest';
2
+ import { GetQueryOptions } from '@opra/core';
3
+ import { ResourceKey } from '@opra/url';
4
+ import { BaseTester } from './base-tester.js';
5
+ import type { OpraEntityTesterParams } from './entity-tester';
6
+ export declare type OpraEntityGetTesterParams = OpraEntityTesterParams & {
7
+ keyValue: ResourceKey;
8
+ options: GetQueryOptions;
9
+ };
10
+ export declare class OpraEntityGetTester extends BaseTester {
11
+ protected readonly _params: OpraEntityGetTesterParams;
12
+ constructor(params: OpraEntityGetTesterParams);
13
+ send(): Promise<Response>;
14
+ keyValue(value: ResourceKey): this;
15
+ omit(...fields: (string | string[])[]): this;
16
+ pick(...fields: (string | string[])[]): this;
17
+ include(...fields: (string | string[])[]): this;
18
+ }
@@ -0,0 +1,39 @@
1
+ import request from 'supertest';
2
+ import { OpraURL } from '@opra/url';
3
+ import { BaseTester } from './base-tester.js';
4
+ export class OpraEntityGetTester extends BaseTester {
5
+ constructor(params) {
6
+ super(params);
7
+ }
8
+ async send() {
9
+ const url = new OpraURL(this._params.path);
10
+ url.pathPrefix = this._params.prefix;
11
+ url.path.get(url.path.size - 1).key = this._params.keyValue;
12
+ if (this._params.options.include)
13
+ url.searchParams.set('$include', this._params.options.include);
14
+ if (this._params.options.pick)
15
+ url.searchParams.set('$pick', this._params.options.pick);
16
+ if (this._params.options.omit)
17
+ url.searchParams.set('$omit', this._params.options.omit);
18
+ const req = request(this._params.app);
19
+ const test = req.get(url.toString());
20
+ this._prepare(test);
21
+ return test.send();
22
+ }
23
+ keyValue(value) {
24
+ this._params.keyValue = value;
25
+ return this;
26
+ }
27
+ omit(...fields) {
28
+ this._params.options.omit = fields.flat();
29
+ return this;
30
+ }
31
+ pick(...fields) {
32
+ this._params.options.pick = fields.flat();
33
+ return this;
34
+ }
35
+ include(...fields) {
36
+ this._params.options.include = fields.flat();
37
+ return this;
38
+ }
39
+ }
@@ -0,0 +1,14 @@
1
+ import { CreateQueryOptions, GetQueryOptions } from '@opra/core';
2
+ import { ResourceKey } from '@opra/url';
3
+ import { BaseTester, OpraTesterParams } from './base-tester.js';
4
+ import { OpraEntityCreateTester } from './entity-create-tester.js';
5
+ import { OpraEntityGetTester } from './entity-get-tester.js';
6
+ export declare type OpraEntityTesterParams = OpraTesterParams & {
7
+ path: string;
8
+ };
9
+ export declare class OpraEntityTester extends BaseTester {
10
+ protected readonly _params: OpraEntityTesterParams;
11
+ constructor(params: OpraEntityTesterParams);
12
+ get(keyValue: ResourceKey, options?: GetQueryOptions): OpraEntityGetTester;
13
+ create(data: {}, options?: CreateQueryOptions): OpraEntityCreateTester;
14
+ }
@@ -0,0 +1,24 @@
1
+ import { BaseTester } from './base-tester.js';
2
+ import { OpraEntityCreateTester } from './entity-create-tester.js';
3
+ import { OpraEntityGetTester } from './entity-get-tester.js';
4
+ export class OpraEntityTester extends BaseTester {
5
+ constructor(params) {
6
+ super(params);
7
+ }
8
+ get(keyValue, options = {}) {
9
+ return new OpraEntityGetTester({
10
+ ...this._params,
11
+ headers: { ...this._params.headers },
12
+ keyValue,
13
+ options
14
+ });
15
+ }
16
+ create(data, options = {}) {
17
+ return new OpraEntityCreateTester({
18
+ ...this._params,
19
+ headers: { ...this._params.headers },
20
+ data,
21
+ options
22
+ });
23
+ }
24
+ }
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@opra/testing",
3
+ "version": "0.0.9",
4
+ "description": "Opra testing package",
5
+ "author": "Panates",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/panates/opra.git",
10
+ "directory": "packages/testing"
11
+ },
12
+ "scripts": {
13
+ "compile": "tsc",
14
+ "prebuild": "npm run lint && npm run clean",
15
+ "build": "npm run build:cjs && npm run build:esm",
16
+ "build:cjs": "tsc -b tsconfig-build-cjs.json",
17
+ "build:esm": "tsc -b tsconfig-build-esm.json",
18
+ "postbuild": "cp README.md package.json ../../LICENSE ../../build/testing && cp ../../package.cjs.json ../../build/testing/cjs/package.json",
19
+ "lint": "eslint .",
20
+ "test": "jest",
21
+ "cover": "jest --collect-coverage",
22
+ "clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
23
+ "clean:src": "ts-cleanup -s src --all",
24
+ "clean:dist": "rimraf ../../build/testing",
25
+ "clean:cover": "rimraf ../../coverage/testing"
26
+ },
27
+ "dependencies": {
28
+ "@opra/core": "^0.0.9",
29
+ "@opra/url": "^0.0.9",
30
+ "lodash": "^4.17.21",
31
+ "supertest": "^6.2.4"
32
+ },
33
+ "devDependencies": {
34
+ "@types/supertest": "^2.0.12"
35
+ },
36
+ "type": "module",
37
+ "main": "cjs/index.js",
38
+ "module": "esm/index.js",
39
+ "types": "esm/index.d.ts",
40
+ "exports": {
41
+ ".": {
42
+ "require": "./cjs/index.js",
43
+ "default": "./esm/index.js"
44
+ },
45
+ "./cjs": "./cjs/index.js",
46
+ "./esm": "./esm/index.js"
47
+ },
48
+ "engines": {
49
+ "node": ">=16.0",
50
+ "npm": ">=7.0.0"
51
+ },
52
+ "files": [
53
+ "bin/",
54
+ "cjs/",
55
+ "esm/",
56
+ "LICENSE",
57
+ "README.md"
58
+ ],
59
+ "keywords": [
60
+ "opra",
61
+ "testing"
62
+ ]
63
+ }