@tryghost/express-test 0.2.0 → 0.4.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.
@@ -1,5 +1,7 @@
1
1
  const assert = require('assert');
2
2
  const {Request, RequestOptions} = require('./request');
3
+ const {snapshotManager} = require('@tryghost/jest-snapshot');
4
+ const {makeMessageFromMatchMessage} = require('./utils');
3
5
 
4
6
  class ExpectRequest extends Request {
5
7
  constructor(...args) {
@@ -54,6 +56,30 @@ class ExpectRequest extends Request {
54
56
  return this;
55
57
  }
56
58
 
59
+ matchBodySnapshot(properties = {}) {
60
+ let assertion = {
61
+ fn: this._assertSnapshot,
62
+ properties: properties,
63
+ field: 'body'
64
+ };
65
+
66
+ this._addAssertion(assertion);
67
+
68
+ return this;
69
+ }
70
+
71
+ matchHeaderSnapshot(properties = {}) {
72
+ let assertion = {
73
+ fn: this._assertSnapshot,
74
+ properties: properties,
75
+ field: 'headers'
76
+ };
77
+
78
+ this._addAssertion(assertion);
79
+
80
+ return this;
81
+ }
82
+
57
83
  finalize(callback) {
58
84
  super.finalize((error, response) => {
59
85
  if (error) {
@@ -78,6 +104,7 @@ class ExpectRequest extends Request {
78
104
  }
79
105
 
80
106
  _addAssertion(assertion) {
107
+ // We create the error here so that we get a useful stack trace
81
108
  let error = new assert.AssertionError({
82
109
  message: 'Unexpected assertion error',
83
110
  expected: assertion.expected,
@@ -95,6 +122,11 @@ class ExpectRequest extends Request {
95
122
  const {error} = assertion;
96
123
 
97
124
  error.message = `Expected statusCode ${assertion.expected}, got statusCode ${response.statusCode} ${error.contextString}`;
125
+
126
+ if (response.body && response.body.errors && response.body.errors[0].message) {
127
+ error.message += `\n${response.body.errors[0].message}`;
128
+ }
129
+
98
130
  error.actual = response.statusCode;
99
131
 
100
132
  assert.equal(response.statusCode, assertion.expected, error);
@@ -123,6 +155,40 @@ class ExpectRequest extends Request {
123
155
  assert.equal(expectedHeaderString, actualHeaderString, error);
124
156
  }
125
157
  }
158
+
159
+ _assertSnapshot(result, assertion) {
160
+ const {properties, field, error} = assertion;
161
+
162
+ if (!result[field]) {
163
+ error.message = `Unable to match snapshot on undefined field ${field} ${error.contextString}`;
164
+ error.expected = field;
165
+ error.actual = 'undefined';
166
+ assert.notEqual(result[field], undefined, error);
167
+ }
168
+
169
+ const hint = `[${field}]`;
170
+ const match = snapshotManager.match(result[field], properties, hint);
171
+
172
+ Object.keys(properties).forEach((prop) => {
173
+ const errorMessage = `"response.${field}" is missing the expected property "${prop}"`;
174
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
175
+ error.expected = prop;
176
+ error.actual = 'undefined';
177
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
178
+
179
+ assert.notEqual(result[field][prop], undefined, error);
180
+ });
181
+
182
+ if (match.pass !== true) {
183
+ const errorMessage = `"response.${field}" does not match snapshot.`;
184
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
185
+ error.expected = match.expected;
186
+ error.actual = match.actual;
187
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
188
+ }
189
+
190
+ assert.equal(match.pass, true, error);
191
+ }
126
192
  }
127
193
 
128
194
  module.exports = ExpectRequest;
package/lib/request.js CHANGED
@@ -1,13 +1,14 @@
1
1
  const {default: reqresnext} = require('reqresnext');
2
2
  const {CookieAccessInfo} = require('cookiejar');
3
3
  const {parse} = require('url');
4
- const {isJSON} = require('./utils');
4
+ const {isJSON, convertKeysToLowerCase} = require('./utils');
5
5
 
6
6
  class RequestOptions {
7
7
  constructor({method, url, headers, body} = {}) {
8
8
  this.method = method || 'GET';
9
9
  this.url = url || '/';
10
- this.headers = headers || {};
10
+ // TODO: remove convertKeysToLowerCase utility once https://github.com/antongolub/reqresnext/issues/33 is fixed
11
+ this.headers = convertKeysToLowerCase(headers) || {};
11
12
  this.body = body;
12
13
  }
13
14
 
@@ -29,7 +30,8 @@ class Request {
29
30
  }
30
31
 
31
32
  header(name, value) {
32
- this.reqOptions.headers[name] = value;
33
+ // TODO: remove toLowerCase once https://github.com/antongolub/reqresnext/issues/33 is fixed
34
+ this.reqOptions.headers[name.toLowerCase()] = value;
33
35
  return this;
34
36
  }
35
37
 
@@ -67,7 +69,7 @@ class Request {
67
69
  const {req, res} = reqresnext(Object.assign({}, reqOptions, {app}), {app});
68
70
 
69
71
  // This is needed to make error handling work
70
- // @TODO: submit this upstream in reqresnext
72
+ // @TODO: remove once https://github.com/antongolub/reqresnext/issues/31 is solved
71
73
  req.socket.destroy = () => { };
72
74
  return {req, res};
73
75
  }
package/lib/utils.js CHANGED
@@ -3,3 +3,19 @@ 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
+ };
12
+
13
+ module.exports.convertKeysToLowerCase = function (map) {
14
+ const newMap = {};
15
+
16
+ for (const key in map) {
17
+ newMap[key.toLowerCase()] = map[key];
18
+ }
19
+
20
+ return newMap;
21
+ };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "0.2.0",
3
+ "version": "0.4.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 './test/**/*.test.js'",
10
+ "test": "NODE_ENV=testing c8 mocha --require ./test/utils/overrides.js './test/**/*.test.js'",
11
11
  "lint": "eslint . --ext .js --cache",
12
12
  "posttest": "yarn lint"
13
13
  },
@@ -26,8 +26,9 @@
26
26
  "sinon": "13.0.1"
27
27
  },
28
28
  "dependencies": {
29
+ "@tryghost/jest-snapshot": "^0.3.1",
29
30
  "cookiejar": "^2.1.3",
30
31
  "reqresnext": "^1.6.6"
31
32
  },
32
- "gitHead": "ba2449737cd3a66d811e34b5b28b86f5d3538396"
33
+ "gitHead": "fdec1082aa5efa81196205cbcd8a31c61b50c8fa"
33
34
  }