@tryghost/express-test 0.3.0 → 0.4.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.
@@ -1,6 +1,7 @@
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');
4
5
 
5
6
  class ExpectRequest extends Request {
6
7
  constructor(...args) {
@@ -55,7 +56,7 @@ class ExpectRequest extends Request {
55
56
  return this;
56
57
  }
57
58
 
58
- matchBodySnapshot(properties) {
59
+ matchBodySnapshot(properties = {}) {
59
60
  let assertion = {
60
61
  fn: this._assertSnapshot,
61
62
  properties: properties,
@@ -67,7 +68,7 @@ class ExpectRequest extends Request {
67
68
  return this;
68
69
  }
69
70
 
70
- matchHeaderSnapshot(properties) {
71
+ matchHeaderSnapshot(properties = {}) {
71
72
  let assertion = {
72
73
  fn: this._assertSnapshot,
73
74
  properties: properties,
@@ -103,6 +104,7 @@ class ExpectRequest extends Request {
103
104
  }
104
105
 
105
106
  _addAssertion(assertion) {
107
+ // We create the error here so that we get a useful stack trace
106
108
  let error = new assert.AssertionError({
107
109
  message: 'Unexpected assertion error',
108
110
  expected: assertion.expected,
@@ -152,13 +154,32 @@ class ExpectRequest extends Request {
152
154
  _assertSnapshot(result, assertion) {
153
155
  const {properties, field, error} = assertion;
154
156
 
155
- const match = snapshotManager.match(result[field], properties);
157
+ if (!result[field]) {
158
+ error.message = `Unable to match snapshot on undefined field ${field} ${error.contextString}`;
159
+ error.expected = field;
160
+ error.actual = 'undefined';
161
+ assert.notEqual(result[field], undefined, error);
162
+ }
163
+
164
+ const hint = `[${field}]`;
165
+ const match = snapshotManager.match(result[field], properties, hint);
166
+
167
+ Object.keys(properties).forEach((prop) => {
168
+ const errorMessage = `"response.${field}" is missing the expected property "${prop}"`;
169
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
170
+ error.expected = prop;
171
+ error.actual = 'undefined';
172
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
173
+
174
+ assert.notEqual(result[field][prop], undefined, error);
175
+ });
156
176
 
157
177
  if (match.pass !== true) {
158
- const snapshotName = match.message().match(/Snapshot name: `(.*)`/)[1];
178
+ const errorMessage = `"response.${field}" does not match snapshot.`;
179
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
159
180
  error.expected = match.expected;
160
181
  error.actual = match.actual;
161
- error.message = `Expected ${field} to match in Snapshot: "${snapshotName}" ${error.contextString}`;
182
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
162
183
  }
163
184
 
164
185
  assert.equal(match.pass, true, error);
package/lib/utils.js CHANGED
@@ -3,3 +3,9 @@ module.exports.isJSON = function isJSON(mime) {
3
3
  // but not /json-seq
4
4
  return /[/+]json($|[^-\w])/i.test(mime);
5
5
  };
6
+
7
+ module.exports.makeMessageFromMatchMessage = function makeMessageFromMatchMessage(message, errorMessage) {
8
+ const messageLines = message.split('\n');
9
+ messageLines.splice(0, 1, errorMessage);
10
+ return messageLines.join('\n');
11
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/express-test",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -26,9 +26,9 @@
26
26
  "sinon": "13.0.1"
27
27
  },
28
28
  "dependencies": {
29
- "@tryghost/jest-snapshot": "^0.2.0",
29
+ "@tryghost/jest-snapshot": "^0.3.0",
30
30
  "cookiejar": "^2.1.3",
31
31
  "reqresnext": "^1.6.6"
32
32
  },
33
- "gitHead": "1750e2bdc5b8f659a4439490c79a8c262a74e66f"
33
+ "gitHead": "5304fb34a2b5dd02f96386fceed34124940ce9ec"
34
34
  }