@tryghost/jest-snapshot 0.3.13 → 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.
@@ -1,9 +1,11 @@
1
1
  const {SnapshotState, toMatchSnapshot} = require('jest-snapshot');
2
2
  const errors = require('@tryghost/errors');
3
3
  const utils = require('@jest/expect-utils');
4
+ const assert = require('assert');
4
5
  const path = require('path');
6
+ const {makeMessageFromMatchMessage} = require('./utils');
5
7
 
6
- class SnapshotManageer {
8
+ class SnapshotManager {
7
9
  constructor() {
8
10
  this.registry = {};
9
11
  this.currentTest = {};
@@ -64,13 +66,47 @@ class SnapshotManageer {
64
66
  }
65
67
 
66
68
  getSnapshotState(testFile, willUpdate) {
67
- // Intilize the SnapshotState, it’s responsible for actually matching
69
+ // Initialize the SnapshotState, it’s responsible for actually matching
68
70
  // actual snapshot with expected one and storing results
69
71
  return new SnapshotState(testFile, {
70
72
  updateSnapshot: willUpdate
71
73
  });
72
74
  }
73
75
 
76
+ assertSnapshot(response, assertion) {
77
+ const {properties, field, error} = assertion;
78
+
79
+ if (!response[field]) {
80
+ error.message = `Unable to match snapshot on undefined field ${field} ${error.contextString}`;
81
+ error.expected = field;
82
+ error.actual = 'undefined';
83
+ assert.notEqual(response[field], undefined, error);
84
+ }
85
+
86
+ const hint = `[${field}]`;
87
+ const match = this.match(response[field], properties, hint);
88
+
89
+ Object.keys(properties).forEach((prop) => {
90
+ const errorMessage = `"response.${field}" is missing the expected property "${prop}"`;
91
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
92
+ error.expected = prop;
93
+ error.actual = 'undefined';
94
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
95
+
96
+ assert.notEqual(response[field][prop], undefined, error);
97
+ });
98
+
99
+ if (match.pass !== true) {
100
+ const errorMessage = `"response.${field}" does not match snapshot.`;
101
+ error.message = makeMessageFromMatchMessage(match.message(), errorMessage);
102
+ error.expected = match.expected;
103
+ error.actual = match.actual;
104
+ error.showDiff = false; // Disable mocha's diff output as it's already present in match.message()
105
+ }
106
+
107
+ assert.equal(match.pass, true, error);
108
+ }
109
+
74
110
  match(received, properties = {}, hint) {
75
111
  const {testFile, snapshotName, willUpdate} = this._getConfig();
76
112
 
@@ -92,4 +128,4 @@ class SnapshotManageer {
92
128
  }
93
129
  }
94
130
 
95
- module.exports = SnapshotManageer;
131
+ module.exports = SnapshotManager;
package/lib/utils.js ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ *
3
+ * @param {String} message
4
+ * @param {String} errorMessage
5
+ * @returns {String}
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/jest-snapshot",
3
- "version": "0.3.13",
3
+ "version": "0.4.1",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/jest-snapshot",
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.2",
23
- "mocha": "9.2.2",
22
+ "c8": "7.11.3",
23
+ "mocha": "10.0.0",
24
24
  "should": "13.2.3",
25
- "sinon": "13.0.2"
25
+ "sinon": "14.0.0"
26
26
  },
27
27
  "dependencies": {
28
28
  "@jest/expect": "^28.0.1",
29
29
  "@jest/expect-utils": "^28.0.1",
30
- "@tryghost/errors": "^1.2.12",
30
+ "@tryghost/errors": "^1.2.14",
31
31
  "jest-snapshot": "^28.0.0"
32
32
  },
33
- "gitHead": "944b81c690bc66f703c8cf36a310e51ce5393b9f"
33
+ "gitHead": "8ab5a88e706d32769575ac98e0aefcd1cfbac3dc"
34
34
  }