@opra/testing 0.16.2 → 0.17.1
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/esm/api-expect/api-expect-collection.js +22 -28
- package/esm/api-expect/api-expect-error.js +7 -12
- package/esm/api-expect/api-expect-object.js +5 -10
- package/esm/api-expect/api-expect-operation-result.js +2 -6
- package/esm/api-expect/api-expect.js +10 -14
- package/esm/index.js +2 -5
- package/esm/jest-extend/common.extend.js +9 -12
- package/esm/jest-extend/index.js +1 -4
- package/esm/test-client.js +13 -15
- package/esm/utils/is-absolute-url.util.js +3 -7
- package/esm/utils/object-matches.util.js +1 -5
- package/package.json +3 -3
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
|
|
7
|
-
const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
|
|
8
|
-
const rule_judgment_1 = tslib_1.__importDefault(require("rule-judgment"));
|
|
9
|
-
const common_1 = require("@opra/common");
|
|
1
|
+
import '../jest-extend/index.js';
|
|
2
|
+
import isNil from 'lodash.isnil';
|
|
3
|
+
import omitBy from 'lodash.omitby';
|
|
4
|
+
import ruleJudgmentLib from 'rule-judgment';
|
|
5
|
+
import { ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, parseFilter, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/common';
|
|
10
6
|
// @ts-ignore
|
|
11
|
-
const ruleJudgment = typeof
|
|
12
|
-
class ApiExpectCollection {
|
|
7
|
+
const ruleJudgment = typeof ruleJudgmentLib === 'object' ? ruleJudgmentLib.default : ruleJudgmentLib;
|
|
8
|
+
export class ApiExpectCollection {
|
|
13
9
|
constructor(response, _isNot = false) {
|
|
14
10
|
this.response = response;
|
|
15
11
|
this._isNot = _isNot;
|
|
@@ -23,7 +19,7 @@ class ApiExpectCollection {
|
|
|
23
19
|
}
|
|
24
20
|
toMatch(expected) {
|
|
25
21
|
try {
|
|
26
|
-
const v = (
|
|
22
|
+
const v = omitBy(expected, isNil);
|
|
27
23
|
for (const item of this.response.body) {
|
|
28
24
|
this._expect(item).toMatchObject(v);
|
|
29
25
|
}
|
|
@@ -133,12 +129,11 @@ class ApiExpectCollection {
|
|
|
133
129
|
return out;
|
|
134
130
|
}
|
|
135
131
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const ast = typeof str === 'string' ? (0, common_1.parseFilter)(str) : str;
|
|
132
|
+
export function convertFilter(str) {
|
|
133
|
+
const ast = typeof str === 'string' ? parseFilter(str) : str;
|
|
139
134
|
if (!ast)
|
|
140
135
|
return;
|
|
141
|
-
if (ast instanceof
|
|
136
|
+
if (ast instanceof ComparisonExpression) {
|
|
142
137
|
const left = convertFilter(ast.left);
|
|
143
138
|
const right = convertFilter(ast.right);
|
|
144
139
|
switch (ast.op) {
|
|
@@ -162,31 +157,30 @@ function convertFilter(str) {
|
|
|
162
157
|
throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
|
|
163
158
|
}
|
|
164
159
|
}
|
|
165
|
-
if (ast instanceof
|
|
160
|
+
if (ast instanceof QualifiedIdentifier) {
|
|
166
161
|
return ast.value;
|
|
167
162
|
}
|
|
168
|
-
if (ast instanceof
|
|
169
|
-
ast instanceof
|
|
170
|
-
ast instanceof
|
|
171
|
-
ast instanceof
|
|
172
|
-
ast instanceof
|
|
173
|
-
ast instanceof
|
|
163
|
+
if (ast instanceof NumberLiteral ||
|
|
164
|
+
ast instanceof StringLiteral ||
|
|
165
|
+
ast instanceof BooleanLiteral ||
|
|
166
|
+
ast instanceof NullLiteral ||
|
|
167
|
+
ast instanceof DateLiteral ||
|
|
168
|
+
ast instanceof TimeLiteral) {
|
|
174
169
|
return ast.value;
|
|
175
170
|
}
|
|
176
|
-
if (ast instanceof
|
|
171
|
+
if (ast instanceof ArrayExpression) {
|
|
177
172
|
return ast.items.map(convertFilter);
|
|
178
173
|
}
|
|
179
|
-
if (ast instanceof
|
|
174
|
+
if (ast instanceof LogicalExpression) {
|
|
180
175
|
if (ast.op === 'or')
|
|
181
176
|
return { $or: ast.items.map(convertFilter) };
|
|
182
177
|
return { $and: ast.items.map(convertFilter) };
|
|
183
178
|
}
|
|
184
|
-
if (ast instanceof
|
|
179
|
+
if (ast instanceof ArrayExpression) {
|
|
185
180
|
return ast.items.map(convertFilter);
|
|
186
181
|
}
|
|
187
|
-
if (ast instanceof
|
|
182
|
+
if (ast instanceof ParenthesesExpression) {
|
|
188
183
|
return convertFilter(ast.expression);
|
|
189
184
|
}
|
|
190
185
|
throw new Error(`${ast.kind} is not implemented yet`);
|
|
191
186
|
}
|
|
192
|
-
exports.convertFilter = convertFilter;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const jest_matcher_utils_1 = tslib_1.__importDefault(require("jest-matcher-utils"));
|
|
7
|
-
const object_matches_util_js_1 = require("../utils/object-matches.util.js");
|
|
8
|
-
const api_expect_object_js_1 = require("./api-expect-object.js");
|
|
9
|
-
class ApiExpectError extends api_expect_object_js_1.ApiExpectObject {
|
|
1
|
+
import '../jest-extend/index.js';
|
|
2
|
+
import matcherUtils from 'jest-matcher-utils';
|
|
3
|
+
import { objectMatches } from '../utils/object-matches.util.js';
|
|
4
|
+
import { ApiExpectObject } from './api-expect-object.js';
|
|
5
|
+
export class ApiExpectError extends ApiExpectObject {
|
|
10
6
|
constructor(response) {
|
|
11
7
|
super(response);
|
|
12
8
|
this.response = response;
|
|
@@ -23,7 +19,6 @@ class ApiExpectError extends api_expect_object_js_1.ApiExpectObject {
|
|
|
23
19
|
}
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
|
-
exports.ApiExpectError = ApiExpectError;
|
|
27
22
|
expect.extend({
|
|
28
23
|
apiErrorDetailToContain(received, issues) {
|
|
29
24
|
try {
|
|
@@ -41,7 +36,7 @@ expect.extend({
|
|
|
41
36
|
for (const detail of received) {
|
|
42
37
|
if (typeof m === 'object') {
|
|
43
38
|
try {
|
|
44
|
-
|
|
39
|
+
objectMatches(detail, m);
|
|
45
40
|
matched = true;
|
|
46
41
|
break;
|
|
47
42
|
}
|
|
@@ -53,7 +48,7 @@ expect.extend({
|
|
|
53
48
|
if (!matched) {
|
|
54
49
|
return {
|
|
55
50
|
pass: false,
|
|
56
|
-
message: () => `Object does not match: \n` +
|
|
51
|
+
message: () => `Object does not match: \n` + matcherUtils.stringify(m)
|
|
57
52
|
};
|
|
58
53
|
}
|
|
59
54
|
}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
require("../jest-extend/index.js");
|
|
6
|
-
const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
|
|
7
|
-
const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
|
|
8
|
-
class ApiExpectObject {
|
|
1
|
+
import '../jest-extend/index.js';
|
|
2
|
+
import isNil from 'lodash.isnil';
|
|
3
|
+
import omitBy from 'lodash.omitby';
|
|
4
|
+
export class ApiExpectObject {
|
|
9
5
|
constructor(response, _isNot = false) {
|
|
10
6
|
this.response = response;
|
|
11
7
|
this._isNot = _isNot;
|
|
@@ -15,7 +11,7 @@ class ApiExpectObject {
|
|
|
15
11
|
}
|
|
16
12
|
toMatch(expected) {
|
|
17
13
|
try {
|
|
18
|
-
const v = (
|
|
14
|
+
const v = omitBy(expected, isNil);
|
|
19
15
|
this._expect(this.response.body).toMatchObject(v);
|
|
20
16
|
}
|
|
21
17
|
catch (e) {
|
|
@@ -51,4 +47,3 @@ class ApiExpectObject {
|
|
|
51
47
|
return out;
|
|
52
48
|
}
|
|
53
49
|
}
|
|
54
|
-
exports.ApiExpectObject = ApiExpectObject;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.ApiExpectOperationResult = void 0;
|
|
4
|
-
require("../jest-extend/index.js");
|
|
5
|
-
class ApiExpectOperationResult {
|
|
1
|
+
import '../jest-extend/index.js';
|
|
2
|
+
export class ApiExpectOperationResult {
|
|
6
3
|
constructor(response, _isNot = false) {
|
|
7
4
|
this.response = response;
|
|
8
5
|
this._isNot = _isNot;
|
|
@@ -47,4 +44,3 @@ class ApiExpectOperationResult {
|
|
|
47
44
|
return out;
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
|
-
exports.ApiExpectOperationResult = ApiExpectOperationResult;
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const api_expect_object_js_1 = require("./api-expect-object.js");
|
|
8
|
-
const api_expect_operation_result_js_1 = require("./api-expect-operation-result.js");
|
|
9
|
-
class ApiExpect {
|
|
1
|
+
import '../jest-extend/index.js';
|
|
2
|
+
import { ApiExpectCollection } from './api-expect-collection.js';
|
|
3
|
+
import { ApiExpectError } from './api-expect-error.js';
|
|
4
|
+
import { ApiExpectObject } from './api-expect-object.js';
|
|
5
|
+
import { ApiExpectOperationResult } from './api-expect-operation-result.js';
|
|
6
|
+
export class ApiExpect {
|
|
10
7
|
constructor(response, _isNot = false) {
|
|
11
8
|
this.response = response;
|
|
12
9
|
this._isNot = _isNot;
|
|
@@ -51,7 +48,7 @@ class ApiExpect {
|
|
|
51
48
|
Error.captureStackTrace(e, this.toFail);
|
|
52
49
|
throw e;
|
|
53
50
|
}
|
|
54
|
-
return new
|
|
51
|
+
return new ApiExpectError(this.response);
|
|
55
52
|
}
|
|
56
53
|
toReturnOperationResult() {
|
|
57
54
|
let msg = '';
|
|
@@ -67,7 +64,7 @@ class ApiExpect {
|
|
|
67
64
|
Error.captureStackTrace(e, this.toReturnOperationResult);
|
|
68
65
|
throw e;
|
|
69
66
|
}
|
|
70
|
-
return new
|
|
67
|
+
return new ApiExpectOperationResult(this.response);
|
|
71
68
|
}
|
|
72
69
|
toReturnObject() {
|
|
73
70
|
let msg = '';
|
|
@@ -82,7 +79,7 @@ class ApiExpect {
|
|
|
82
79
|
Error.captureStackTrace(e, this.toReturnObject);
|
|
83
80
|
throw e;
|
|
84
81
|
}
|
|
85
|
-
return new
|
|
82
|
+
return new ApiExpectObject(this.response);
|
|
86
83
|
}
|
|
87
84
|
toReturnCollection() {
|
|
88
85
|
let msg = '';
|
|
@@ -98,7 +95,7 @@ class ApiExpect {
|
|
|
98
95
|
Error.captureStackTrace(e, this.toReturnCollection);
|
|
99
96
|
throw e;
|
|
100
97
|
}
|
|
101
|
-
return new
|
|
98
|
+
return new ApiExpectCollection(this.response);
|
|
102
99
|
}
|
|
103
100
|
_expect(expected) {
|
|
104
101
|
const out = expect(expected);
|
|
@@ -107,4 +104,3 @@ class ApiExpect {
|
|
|
107
104
|
return out;
|
|
108
105
|
}
|
|
109
106
|
}
|
|
110
|
-
exports.ApiExpect = ApiExpect;
|
package/esm/index.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
require("./jest-extend/index.js");
|
|
5
|
-
tslib_1.__exportStar(require("./test-client.js"), exports);
|
|
1
|
+
import './jest-extend/index.js';
|
|
2
|
+
export * from './test-client.js';
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
|
|
5
|
-
const jest_matcher_utils_1 = require("jest-matcher-utils");
|
|
1
|
+
import colors from "ansi-colors";
|
|
2
|
+
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
|
|
6
3
|
expect.extend({
|
|
7
4
|
toHaveFields(received, expected) {
|
|
8
5
|
const expectedKeys = (Array.isArray(expected) ? expected : Object.keys(expected)).map(x => x.toLowerCase());
|
|
@@ -10,8 +7,8 @@ expect.extend({
|
|
|
10
7
|
const filteredKeys = expectedKeys.filter(x => !objectKeys.includes(x));
|
|
11
8
|
const pass = !filteredKeys.length === !this.isNot;
|
|
12
9
|
if (!pass) {
|
|
13
|
-
const message = () => `Expects keys ${this.isNot ? 'not ' : ''}to contain: ${
|
|
14
|
-
`${this.isNot ? 'Unsolicited' : 'Missing'} fields: ${
|
|
10
|
+
const message = () => `Expects keys ${this.isNot ? 'not ' : ''}to contain: ${colors.yellow('' + expectedKeys)}\n` +
|
|
11
|
+
`${this.isNot ? 'Unsolicited' : 'Missing'} fields: ${colors.yellow('' + filteredKeys)}\n`;
|
|
15
12
|
return { message, pass: !!this.isNot };
|
|
16
13
|
}
|
|
17
14
|
return { actual: received, pass: !this.isNot, message: () => '' };
|
|
@@ -22,8 +19,8 @@ expect.extend({
|
|
|
22
19
|
const filteredKeys = objectKeys.filter(x => !expectedKeys.includes(x));
|
|
23
20
|
const pass = !filteredKeys.length === !this.isNot;
|
|
24
21
|
if (!pass) {
|
|
25
|
-
const message = () => `${!this.isNot ? 'Do not expects' : 'Expects'} additional keys other than: ${
|
|
26
|
-
(filteredKeys ? `Additional keys received: ${
|
|
22
|
+
const message = () => `${!this.isNot ? 'Do not expects' : 'Expects'} additional keys other than: ${colors.yellow('' + expectedKeys)}\n` +
|
|
23
|
+
(filteredKeys ? `Additional keys received: ${colors.yellow('' + filteredKeys)}\n` :
|
|
27
24
|
'No additional keys received\n');
|
|
28
25
|
return { message, pass };
|
|
29
26
|
}
|
|
@@ -124,9 +121,9 @@ expect.extend({
|
|
|
124
121
|
});
|
|
125
122
|
function compare(matcherName, options, received, expected, operator, fn) {
|
|
126
123
|
const pass = fn(received, expected);
|
|
127
|
-
const message = () =>
|
|
124
|
+
const message = () => matcherHint(matcherName, undefined, undefined, options) +
|
|
128
125
|
'\n\n' +
|
|
129
|
-
`Expected:${options.isNot ? ' not' : ''} ${operator} ${
|
|
130
|
-
`Received:${options.isNot ? ' ' : ''} ${
|
|
126
|
+
`Expected:${options.isNot ? ' not' : ''} ${operator} ${printExpected(expected)}\n` +
|
|
127
|
+
`Received:${options.isNot ? ' ' : ''} ${printReceived(received)}`;
|
|
131
128
|
return { message, pass };
|
|
132
129
|
}
|
package/esm/jest-extend/index.js
CHANGED
package/esm/test-client.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class OpraTestClient extends node_client_1.OpraHttpClient {
|
|
1
|
+
import { createServer, Server } from 'http';
|
|
2
|
+
import { URL } from 'url';
|
|
3
|
+
import { joinPath } from '@opra/common';
|
|
4
|
+
import {
|
|
5
|
+
// BatchRequest,
|
|
6
|
+
OpraHttpClient, } from '@opra/node-client';
|
|
7
|
+
import { ApiExpect } from './api-expect/api-expect.js';
|
|
8
|
+
import { isAbsoluteUrl } from './utils/is-absolute-url.util.js';
|
|
9
|
+
export class OpraTestClient extends OpraHttpClient {
|
|
11
10
|
constructor(app, options) {
|
|
12
11
|
super('/', options);
|
|
13
|
-
this._server = app instanceof
|
|
12
|
+
this._server = app instanceof Server ? app : createServer(app);
|
|
14
13
|
}
|
|
15
14
|
collection(resourceName) {
|
|
16
15
|
return super.collection(resourceName);
|
|
@@ -20,8 +19,8 @@ class OpraTestClient extends node_client_1.OpraHttpClient {
|
|
|
20
19
|
}
|
|
21
20
|
async _fetch(urlString, req = {}) {
|
|
22
21
|
return new Promise((resolve, reject) => {
|
|
23
|
-
urlString =
|
|
24
|
-
const url = new
|
|
22
|
+
urlString = isAbsoluteUrl(urlString) ? urlString : joinPath('http://opra.test', urlString);
|
|
23
|
+
const url = new URL(urlString);
|
|
25
24
|
// Set protocol to HTTP
|
|
26
25
|
url.protocol = 'http';
|
|
27
26
|
// Apply original host to request header
|
|
@@ -56,8 +55,7 @@ class OpraTestClient extends node_client_1.OpraHttpClient {
|
|
|
56
55
|
}
|
|
57
56
|
_createResponse(init) {
|
|
58
57
|
const resp = super._createResponse(init);
|
|
59
|
-
resp.expect = new
|
|
58
|
+
resp.expect = new ApiExpect(resp);
|
|
60
59
|
return resp;
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
|
-
exports.OpraTestClient = OpraTestClient;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const url_1 = require("url");
|
|
5
|
-
const isAbsoluteUrl = (urlString) => {
|
|
6
|
-
const url = new url_1.URL(urlString, 'http://opra.test/');
|
|
1
|
+
import { URL } from 'url';
|
|
2
|
+
export const isAbsoluteUrl = (urlString) => {
|
|
3
|
+
const url = new URL(urlString, 'http://opra.test/');
|
|
7
4
|
return url.host !== 'opra.test';
|
|
8
5
|
};
|
|
9
|
-
exports.isAbsoluteUrl = isAbsoluteUrl;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.objectMatches = void 0;
|
|
4
|
-
function objectMatches(received, expected) {
|
|
1
|
+
export function objectMatches(received, expected) {
|
|
5
2
|
_objectMatches(received, expected, '');
|
|
6
3
|
}
|
|
7
|
-
exports.objectMatches = objectMatches;
|
|
8
4
|
function _objectMatches(received, expected, path) {
|
|
9
5
|
if (typeof received !== typeof expected)
|
|
10
6
|
expect(typeof received).toStrictEqual('object');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.1",
|
|
4
4
|
"description": "Opra testing package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"clean:cover": "rimraf ../../coverage/testing"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@opra/common": "^0.
|
|
29
|
-
"@opra/node-client": "^0.
|
|
28
|
+
"@opra/common": "^0.17.1",
|
|
29
|
+
"@opra/node-client": "^0.17.1",
|
|
30
30
|
"ansi-colors": "^4.1.3",
|
|
31
31
|
"lodash.isnil": "^4.0.0",
|
|
32
32
|
"lodash.omitby": "^4.6.0",
|