@tryghost/express-test 0.5.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/index.js +8 -0
- package/lib/agent.js +22 -0
- package/lib/expect-request.js +8 -4
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -40,6 +40,16 @@ return await agent
|
|
|
40
40
|
This is an initial version for review. More docs coming if it works :)
|
|
41
41
|
|
|
42
42
|
|
|
43
|
+
### Assertion execution order
|
|
44
|
+
The order of *chained* assertion execution is NOT the order they were declared in. The framework follows the priority order of the assertion types:
|
|
45
|
+
1. `expect`
|
|
46
|
+
2. `expectHeader`
|
|
47
|
+
3. `expectStatus`
|
|
48
|
+
|
|
49
|
+
Meaning, any `expect` declarations would be asserted first following by `expectHeader` and `expectStatus` declarations.
|
|
50
|
+
|
|
51
|
+
The custom order is here to prioritize assertions with the most context first. The feature was added based on frustrations with frameworks like [superagent](https://github.com/visionmedia/superagent), which didn't provide enough information when the status code assertions failed.
|
|
52
|
+
|
|
43
53
|
## Develop
|
|
44
54
|
|
|
45
55
|
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
package/index.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
1
|
module.exports = require('./lib/agent');
|
|
2
|
+
|
|
3
|
+
// NOTE: exposing jest-snapshot as a part of express-test to avoid
|
|
4
|
+
// version missmatching on the client side
|
|
5
|
+
module.exports.snapshot = {
|
|
6
|
+
mochaHooks: require('@tryghost/jest-snapshot').mochaHooks,
|
|
7
|
+
any: require('@tryghost/jest-snapshot').any,
|
|
8
|
+
stringMatching: require('@tryghost/jest-snapshot').stringMatching
|
|
9
|
+
};
|
package/lib/agent.js
CHANGED
|
@@ -3,6 +3,14 @@ const ExpectRequest = require('./expect-request');
|
|
|
3
3
|
const {RequestOptions} = require('./request');
|
|
4
4
|
|
|
5
5
|
class Agent {
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} app instance of Express app
|
|
9
|
+
* @param {Object} [defaults]
|
|
10
|
+
* @param {string} [defaults.baseUrl]
|
|
11
|
+
* @param {Object} [defaults.headers]
|
|
12
|
+
* @param {Object} [defaults.queryParams] - custom query params to append to each request
|
|
13
|
+
*/
|
|
6
14
|
constructor(app, defaults = {}) {
|
|
7
15
|
this.app = app;
|
|
8
16
|
this.defaults = defaults;
|
|
@@ -15,6 +23,20 @@ class Agent {
|
|
|
15
23
|
url = `/${this.defaults.baseUrl}/${url}`.replace(/(^|[^:])\/\/+/g, '$1/');
|
|
16
24
|
}
|
|
17
25
|
|
|
26
|
+
if (this.defaults.queryParams) {
|
|
27
|
+
const searchParams = new URLSearchParams();
|
|
28
|
+
|
|
29
|
+
for (const key in this.defaults.queryParams) {
|
|
30
|
+
searchParams.append(key, this.defaults.queryParams[key]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (url.includes('?')) {
|
|
34
|
+
url = `${url}&${searchParams.toString()}`;
|
|
35
|
+
} else {
|
|
36
|
+
url = `${url}?${searchParams.toString()}`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
18
40
|
return url;
|
|
19
41
|
}
|
|
20
42
|
|
package/lib/expect-request.js
CHANGED
|
@@ -8,7 +8,7 @@ const {makeMessageFromMatchMessage} = require('./utils');
|
|
|
8
8
|
* @prop {function} fn - Wrapped assertion with response context
|
|
9
9
|
* @prop {string} [message] - The message to display if the assertion fails
|
|
10
10
|
* @prop {string} [method] - The method to call on the assertion object
|
|
11
|
-
* @prop {'header' | 'status'}
|
|
11
|
+
* @prop {'body' | 'header' | 'status'} type - The type of assertion, with following priority: body > header > status
|
|
12
12
|
* @prop {import('assert').AssertionError} [error] - The error to throw if the assertion fails
|
|
13
13
|
* @prop {any} [expected]
|
|
14
14
|
* @prop {any} [expectedValue]
|
|
@@ -43,7 +43,8 @@ class ExpectRequest extends Request {
|
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
const assertion = {
|
|
46
|
-
fn: wrapperFn
|
|
46
|
+
fn: wrapperFn,
|
|
47
|
+
type: 'body'
|
|
47
48
|
};
|
|
48
49
|
|
|
49
50
|
this._addAssertion(assertion);
|
|
@@ -80,7 +81,8 @@ class ExpectRequest extends Request {
|
|
|
80
81
|
let assertion = {
|
|
81
82
|
fn: this._assertSnapshot,
|
|
82
83
|
properties: properties,
|
|
83
|
-
field: 'body'
|
|
84
|
+
field: 'body',
|
|
85
|
+
type: 'body'
|
|
84
86
|
};
|
|
85
87
|
|
|
86
88
|
this._addAssertion(assertion);
|
|
@@ -139,12 +141,14 @@ class ExpectRequest extends Request {
|
|
|
139
141
|
const headerAssertions = [];
|
|
140
142
|
|
|
141
143
|
for (const assertion of this.assertions) {
|
|
142
|
-
if (
|
|
144
|
+
if (assertion.type === 'body') {
|
|
143
145
|
assertion.fn(response, assertion);
|
|
144
146
|
} else if (assertion.type === 'status') {
|
|
145
147
|
statusCodeAssertions.push(assertion);
|
|
146
148
|
} else if (assertion.type === 'header') {
|
|
147
149
|
headerAssertions.push(assertion);
|
|
150
|
+
} else {
|
|
151
|
+
headerAssertions.push(assertion);
|
|
148
152
|
}
|
|
149
153
|
}
|
|
150
154
|
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/express-test",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"repository": "https://github.com/TryGhost/framework/tree/main/packages/express-test",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"dev": "echo \"Implement me!\"",
|
|
10
|
-
"test": "NODE_ENV=testing c8 mocha --require ./test/utils/overrides.js './test/**/*.test.js'",
|
|
10
|
+
"test": "NODE_ENV=testing c8 --all --reporter text --reporter cobertura mocha --require ./test/utils/overrides.js './test/**/*.test.js'",
|
|
11
11
|
"lint": "eslint . --ext .js --cache",
|
|
12
12
|
"posttest": "yarn lint"
|
|
13
13
|
},
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"c8": "7.11.0",
|
|
23
|
-
"express": "4.17.
|
|
23
|
+
"express": "4.17.3",
|
|
24
24
|
"express-session": "1.17.2",
|
|
25
|
-
"mocha": "9.2.
|
|
25
|
+
"mocha": "9.2.1",
|
|
26
26
|
"sinon": "13.0.1"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@tryghost/jest-snapshot": "^0.3.
|
|
29
|
+
"@tryghost/jest-snapshot": "^0.3.5",
|
|
30
30
|
"cookiejar": "^2.1.3",
|
|
31
31
|
"reqresnext": "^1.7.0"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "d09bf53d3435dd9782cea9c5a7062e07538a2b0b"
|
|
34
34
|
}
|