@tryghost/express-test 0.8.4 → 0.10.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/lib/agent.js CHANGED
@@ -3,14 +3,19 @@ const ExpectRequest = require('./expect-request');
3
3
  const {RequestOptions} = require('./request');
4
4
  const {normalizeURL} = require('./utils');
5
5
 
6
+ /**
7
+ * @typedef AgentOptions
8
+ * @param {string} [baseUrl] - the base URL path for the API query
9
+ * @param {Object} [queryParams] - custom query params to append to each request
10
+ * @param {Object} [headers] - any default headers to append to each request
11
+ * @param {Object} [body] - a default body to append to each request
12
+ * */
13
+
6
14
  class Agent {
7
15
  /**
8
16
  *
9
17
  * @param {Object} app instance of Express app
10
- * @param {Object} [defaults]
11
- * @param {string} [defaults.baseUrl]
12
- * @param {Object} [defaults.headers]
13
- * @param {Object} [defaults.queryParams] - custom query params to append to each request
18
+ * @param {AgentOptions} [defaults]
14
19
  */
15
20
  constructor(app, defaults = {}) {
16
21
  this.app = app;
@@ -19,20 +24,31 @@ class Agent {
19
24
  this.jar = new CookieJar();
20
25
  }
21
26
 
22
- _makeUrl(url) {
27
+ /**
28
+ *
29
+ * @param {String} url
30
+ * @param {object} [urlOptions]
31
+ * @param {string} [urlOptions.baseUrl] - the base URL path for the API query
32
+ * @param {Object} [urlOptions.queryParams] - custom query params to append to each request
33
+ * @returns
34
+ */
35
+ _makeUrl(url, urlOptions = {}) {
23
36
  let processedURL = url;
37
+ const baseUrl = urlOptions.baseUrl || this.defaults.baseUrl || null;
24
38
 
25
- if (this.defaults.baseUrl) {
26
- processedURL = `/${this.defaults.baseUrl}/${processedURL}`.replace(/(^|[^:])\/\/+/g, '$1/');
39
+ if (baseUrl) {
40
+ processedURL = `/${baseUrl}/${processedURL}`.replace(/(^|[^:])\/\/+/g, '$1/');
27
41
  }
28
42
 
29
43
  processedURL = normalizeURL(processedURL);
30
44
 
31
- if (this.defaults.queryParams) {
45
+ const queryParams = Object.assign({}, this.defaults.queryParams, urlOptions.queryParams);
46
+
47
+ if (Object.keys(queryParams).length > 0) {
32
48
  const searchParams = new URLSearchParams();
33
49
 
34
- for (const key in this.defaults.queryParams) {
35
- searchParams.append(key, this.defaults.queryParams[key]);
50
+ for (const key in queryParams) {
51
+ searchParams.append(key, queryParams[key]);
36
52
  }
37
53
 
38
54
  if (processedURL.includes('?')) {
@@ -45,6 +61,13 @@ class Agent {
45
61
  return processedURL;
46
62
  }
47
63
 
64
+ /**
65
+ *
66
+ * @param {string}} method - HTTP method
67
+ * @param {string} url - url to request
68
+ * @param {AgentOptions} options
69
+ * @returns
70
+ */
48
71
  _mergeOptions(method, url, options = {}) {
49
72
  // It doesn't make sense to call this method without these properties
50
73
  if (!method) {
@@ -55,9 +78,12 @@ class Agent {
55
78
  throw new Error('_mergeOptions cannot be called without a url'); /* eslint-disable-line no-restricted-syntax */
56
79
  }
57
80
 
81
+ // urlOptions
82
+ const {baseUrl, queryParams} = options;
83
+
58
84
  return new RequestOptions({
59
85
  method,
60
- url: this._makeUrl(url),
86
+ url: this._makeUrl(url, Object.assign({}, {baseUrl, queryParams})),
61
87
  headers: Object.assign({}, this.defaults.headers, options.headers),
62
88
  // Set this to an empty object for ease, as express.json will do this anyway
63
89
  body: Object.assign({}, this.defaults.body, options.body)
@@ -198,6 +198,10 @@ class ExpectRequest extends Request {
198
198
 
199
199
  if (response.body && response.body.errors && response.body.errors[0].message) {
200
200
  error.message += `\n${response.body.errors[0].message}`;
201
+
202
+ if (response.body.errors[0].context) {
203
+ error.message += `\n${response.body.errors[0].context}`;
204
+ }
201
205
  }
202
206
 
203
207
  error.actual = response.statusCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "0.8.4",
3
+ "version": "0.10.1",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/express-test",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,16 +19,16 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "7.11.0",
23
- "express": "4.17.3",
22
+ "c8": "7.11.3",
23
+ "express": "4.18.0",
24
24
  "express-session": "1.17.2",
25
- "mocha": "9.2.2",
26
- "sinon": "13.0.2"
25
+ "mocha": "10.0.0",
26
+ "sinon": "14.0.0"
27
27
  },
28
28
  "dependencies": {
29
- "@tryghost/jest-snapshot": "^0.3.12",
29
+ "@tryghost/jest-snapshot": "^0.3.14",
30
30
  "cookiejar": "^2.1.3",
31
31
  "reqresnext": "^1.7.0"
32
32
  },
33
- "gitHead": "6a49350821ab2dd54d4b8a1ac7cde27b72aeaca0"
33
+ "gitHead": "6b46efd28457ee4b86cf4ae9da9bf87238099ddd"
34
34
  }