@tryghost/express-test 0.10.1 → 0.11.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/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
@@ -17,11 +17,12 @@ class Agent {
17
17
  * @param {Object} app instance of Express app
18
18
  * @param {AgentOptions} [defaults]
19
19
  */
20
- constructor(app, defaults = {}) {
20
+ constructor(app, defaults = {}, snapshotManager) {
21
21
  this.app = app;
22
22
  this.defaults = defaults;
23
23
 
24
24
  this.jar = new CookieJar();
25
+ this.snapshotManager = snapshotManager;
25
26
  }
26
27
 
27
28
  /**
@@ -96,7 +97,7 @@ class Agent {
96
97
  if (!url) {
97
98
  throw new Error('Cannot make a request without supplying a url'); /* eslint-disable-line no-restricted-syntax */
98
99
  }
99
- 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);
100
101
  };
101
102
  });
102
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.10.1",
3
+ "version": "0.11.2",
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.3",
23
- "express": "4.18.0",
24
- "express-session": "1.17.2",
22
+ "c8": "7.12.0",
23
+ "express": "4.18.1",
24
+ "express-session": "1.17.3",
25
25
  "mocha": "10.0.0",
26
26
  "sinon": "14.0.0"
27
27
  },
28
28
  "dependencies": {
29
- "@tryghost/jest-snapshot": "^0.3.14",
29
+ "@tryghost/jest-snapshot": "^0.4.2",
30
30
  "cookiejar": "^2.1.3",
31
31
  "reqresnext": "^1.7.0"
32
32
  },
33
- "gitHead": "6b46efd28457ee4b86cf4ae9da9bf87238099ddd"
33
+ "gitHead": "cc9195ea06015d9711ed1fa65267ff85c19edb05"
34
34
  }