@tryghost/express-test 0.1.1 → 0.4.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
@@ -37,7 +37,7 @@ class Agent {
37
37
  }
38
38
  }
39
39
 
40
- ['get', 'post', 'put', 'del'].forEach((method) => {
40
+ ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'].forEach((method) => {
41
41
  Agent.prototype[method] = function (url, options) {
42
42
  if (!url) {
43
43
  throw new Error('Cannot make a request without supplying a url'); /* eslint-disable-line no-restricted-syntax */
@@ -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) {
@@ -7,6 +9,12 @@ class ExpectRequest extends Request {
7
9
  }
8
10
 
9
11
  expect(callback) {
12
+ if (typeof callback !== 'function') {
13
+ throw new Error(/* eslint-disable-line no-restricted-syntax */
14
+ 'express-test expect() requires a callback function, did you mean expectStatus or expectHeader?'
15
+ );
16
+ }
17
+
10
18
  const wrapperFn = (response, assertion) => {
11
19
  try {
12
20
  callback(response);
@@ -47,6 +55,31 @@ class ExpectRequest extends Request {
47
55
 
48
56
  return this;
49
57
  }
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
+
50
83
  finalize(callback) {
51
84
  super.finalize((error, response) => {
52
85
  if (error) {
@@ -71,6 +104,7 @@ class ExpectRequest extends Request {
71
104
  }
72
105
 
73
106
  _addAssertion(assertion) {
107
+ // We create the error here so that we get a useful stack trace
74
108
  let error = new assert.AssertionError({
75
109
  message: 'Unexpected assertion error',
76
110
  expected: assertion.expected,
@@ -96,17 +130,59 @@ class ExpectRequest extends Request {
96
130
  _assertHeader(response, assertion) {
97
131
  const {expectedField, expectedValue, error} = assertion;
98
132
  const actual = response.headers[expectedField];
133
+
99
134
  const expectedHeaderString = `${expectedField}: ${expectedValue}`;
100
135
  const actualHeaderString = `${expectedField}: ${actual}`;
101
136
 
102
137
  error.expected = expectedHeaderString;
103
138
  error.actual = actualHeaderString;
104
139
 
105
- error.message = `Expected header "${expectedHeaderString}" to exist but got ${JSON.stringify(response.headers)} ${error.contextString}`;
140
+ error.message = `Expected header "${expectedHeaderString}" to exist, got headers: ${JSON.stringify(response.headers)} ${error.contextString}`;
106
141
  assert.notStrictEqual(actual, undefined, error);
107
142
 
108
- error.message = `Expected header "${expectedHeaderString}", got ${actualHeaderString} ${error.contextString}`;
109
- assert.equal(expectedHeaderString, actualHeaderString, error);
143
+ if (expectedValue instanceof RegExp) {
144
+ error.expected = expectedValue;
145
+ error.actual = actual;
146
+ error.message = `Expected header "${expectedField}" to have value matching "${expectedValue}", got "${actual}" ${error.contextString}`;
147
+ assert.equal(expectedValue.test(actual), true, error);
148
+ } else {
149
+ error.message = `Expected header "${expectedHeaderString}", got "${actualHeaderString}" ${error.contextString}`;
150
+ assert.equal(expectedHeaderString, actualHeaderString, error);
151
+ }
152
+ }
153
+
154
+ _assertSnapshot(result, assertion) {
155
+ const {properties, field, error} = assertion;
156
+
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
+ });
176
+
177
+ if (match.pass !== true) {
178
+ const errorMessage = `"response.${field}" does not match snapshot.`;
179
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
180
+ error.expected = match.expected;
181
+ error.actual = match.actual;
182
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
183
+ }
184
+
185
+ assert.equal(match.pass, true, error);
110
186
  }
111
187
  }
112
188
 
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,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryghost/express-test",
3
- "version": "0.1.1",
3
+ "version": "0.4.1",
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": "81312a5f57f2c71f189457b9e4e4e1fa1f749b0e"
33
+ "gitHead": "79e0c67a94342496f84870a200eab7e4e74e4dbc"
33
34
  }