@tryghost/express-test 0.9.0 → 0.11.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.
package/index.js CHANGED
@@ -4,6 +4,7 @@ module.exports = require('./lib/agent');
4
4
  // version missmatching on the client side
5
5
  module.exports.snapshot = {
6
6
  mochaHooks: require('@tryghost/jest-snapshot').mochaHooks,
7
+ snapshotManager: require('@tryghost/jest-snapshot').snapshotManager,
7
8
  any: require('@tryghost/jest-snapshot').any,
8
9
  stringMatching: require('@tryghost/jest-snapshot').stringMatching
9
10
  };
package/lib/agent.js CHANGED
@@ -3,36 +3,53 @@ 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
- constructor(app, defaults = {}) {
20
+ constructor(app, defaults = {}, snapshotManager) {
16
21
  this.app = app;
17
22
  this.defaults = defaults;
18
23
 
19
24
  this.jar = new CookieJar();
25
+ this.snapshotManager = snapshotManager;
20
26
  }
21
27
 
22
- _makeUrl(url) {
28
+ /**
29
+ *
30
+ * @param {String} url
31
+ * @param {object} [urlOptions]
32
+ * @param {string} [urlOptions.baseUrl] - the base URL path for the API query
33
+ * @param {Object} [urlOptions.queryParams] - custom query params to append to each request
34
+ * @returns
35
+ */
36
+ _makeUrl(url, urlOptions = {}) {
23
37
  let processedURL = url;
38
+ const baseUrl = urlOptions.baseUrl || this.defaults.baseUrl || null;
24
39
 
25
- if (this.defaults.baseUrl) {
26
- processedURL = `/${this.defaults.baseUrl}/${processedURL}`.replace(/(^|[^:])\/\/+/g, '$1/');
40
+ if (baseUrl) {
41
+ processedURL = `/${baseUrl}/${processedURL}`.replace(/(^|[^:])\/\/+/g, '$1/');
27
42
  }
28
43
 
29
44
  processedURL = normalizeURL(processedURL);
30
45
 
31
- if (this.defaults.queryParams) {
46
+ const queryParams = Object.assign({}, this.defaults.queryParams, urlOptions.queryParams);
47
+
48
+ if (Object.keys(queryParams).length > 0) {
32
49
  const searchParams = new URLSearchParams();
33
50
 
34
- for (const key in this.defaults.queryParams) {
35
- searchParams.append(key, this.defaults.queryParams[key]);
51
+ for (const key in queryParams) {
52
+ searchParams.append(key, queryParams[key]);
36
53
  }
37
54
 
38
55
  if (processedURL.includes('?')) {
@@ -45,6 +62,13 @@ class Agent {
45
62
  return processedURL;
46
63
  }
47
64
 
65
+ /**
66
+ *
67
+ * @param {string}} method - HTTP method
68
+ * @param {string} url - url to request
69
+ * @param {AgentOptions} options
70
+ * @returns
71
+ */
48
72
  _mergeOptions(method, url, options = {}) {
49
73
  // It doesn't make sense to call this method without these properties
50
74
  if (!method) {
@@ -55,9 +79,12 @@ class Agent {
55
79
  throw new Error('_mergeOptions cannot be called without a url'); /* eslint-disable-line no-restricted-syntax */
56
80
  }
57
81
 
82
+ // urlOptions
83
+ const {baseUrl, queryParams} = options;
84
+
58
85
  return new RequestOptions({
59
86
  method,
60
- url: this._makeUrl(url),
87
+ url: this._makeUrl(url, Object.assign({}, {baseUrl, queryParams})),
61
88
  headers: Object.assign({}, this.defaults.headers, options.headers),
62
89
  // Set this to an empty object for ease, as express.json will do this anyway
63
90
  body: Object.assign({}, this.defaults.body, options.body)
@@ -70,7 +97,7 @@ class Agent {
70
97
  if (!url) {
71
98
  throw new Error('Cannot make a request without supplying a url'); /* eslint-disable-line no-restricted-syntax */
72
99
  }
73
- return new ExpectRequest(this.app, this.jar, this._mergeOptions(method.toUpperCase(), url, options));
100
+ return new ExpectRequest(this.app, this.jar, this._mergeOptions(method.toUpperCase(), url, options), this.snapshotManager);
74
101
  };
75
102
  });
76
103
 
@@ -1,7 +1,6 @@
1
1
  const assert = require('assert');
2
2
  const {Request, RequestOptions} = require('./request');
3
3
  const {snapshotManager} = require('@tryghost/jest-snapshot');
4
- const {makeMessageFromMatchMessage} = require('./utils');
5
4
 
6
5
  /**
7
6
  * @typedef {object} ExpressTestAssertion
@@ -17,13 +16,16 @@ const {makeMessageFromMatchMessage} = require('./utils');
17
16
  */
18
17
 
19
18
  class ExpectRequest extends Request {
20
- constructor(...args) {
21
- super(...args);
19
+ constructor(app, cookieJar, reqOptions, _snapshotManager) {
20
+ super(app, cookieJar, reqOptions);
22
21
 
23
22
  /**
24
23
  * @property {ExpressTestAssertion[]} assertions
25
24
  */
26
25
  this.assertions = [];
26
+
27
+ this.snapshotManager = _snapshotManager ?? snapshotManager;
28
+ this._assertSnapshot = this._assertSnapshot.bind(this);
27
29
  }
28
30
 
29
31
  expect(callback) {
@@ -244,37 +246,7 @@ class ExpectRequest extends Request {
244
246
  }
245
247
 
246
248
  _assertSnapshot(response, assertion) {
247
- const {properties, field, error} = assertion;
248
-
249
- if (!response[field]) {
250
- error.message = `Unable to match snapshot on undefined field ${field} ${error.contextString}`;
251
- error.expected = field;
252
- error.actual = 'undefined';
253
- assert.notEqual(response[field], undefined, error);
254
- }
255
-
256
- const hint = `[${field}]`;
257
- const match = snapshotManager.match(response[field], properties, hint);
258
-
259
- Object.keys(properties).forEach((prop) => {
260
- const errorMessage = `"response.${field}" is missing the expected property "${prop}"`;
261
- error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
262
- error.expected = prop;
263
- error.actual = 'undefined';
264
- error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
265
-
266
- assert.notEqual(response[field][prop], undefined, error);
267
- });
268
-
269
- if (match.pass !== true) {
270
- const errorMessage = `"response.${field}" does not match snapshot.`;
271
- error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
272
- error.expected = match.expected;
273
- error.actual = match.actual;
274
- error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
275
- }
276
-
277
- assert.equal(match.pass, true, error);
249
+ this.snapshotManager.assertSnapshot(response, assertion);
278
250
  }
279
251
  }
280
252
 
package/lib/utils.js CHANGED
@@ -1,17 +1,9 @@
1
- // const url = require('url');
2
-
3
1
  module.exports.isJSON = function isJSON(mime) {
4
2
  // should match /json or +json
5
3
  // but not /json-seq
6
4
  return /[/+]json($|[^-\w])/i.test(mime);
7
5
  };
8
6
 
9
- module.exports.makeMessageFromMatchMessage = function makeMessageFromMatchMessage(message, errorMessage) {
10
- const messageLines = message.split('\n');
11
- messageLines.splice(0, 1, errorMessage);
12
- return messageLines.join('\n');
13
- };
14
-
15
7
  module.exports.normalizeURL = function normalizeURL(toNormalize) {
16
8
  const split = toNormalize.split('?');
17
9
  const pathname = split[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
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.4.0",
30
30
  "cookiejar": "^2.1.3",
31
31
  "reqresnext": "^1.7.0"
32
32
  },
33
- "gitHead": "9799b440419e56125eabc4ddd34290d3f40733f9"
33
+ "gitHead": "0af921ce800c79f89685307914bd6d704302c043"
34
34
  }