@tryghost/jest-snapshot 0.3.12 → 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,4 +1,4 @@
1
- const expect = require('expect');
1
+ const {jestExpect} = require('@jest/expect');
2
2
  const SnapshotManager = require('./snapshot-manager');
3
3
  const snapshotManager = new SnapshotManager();
4
4
 
@@ -25,7 +25,7 @@ const mochaHooks = {
25
25
  }
26
26
  };
27
27
 
28
- const {any, anything, stringMatching} = expect;
28
+ const {any, anything, stringMatching} = jestExpect;
29
29
 
30
30
  module.exports = {
31
31
  mochaHooks,
@@ -1,10 +1,11 @@
1
1
  const {SnapshotState, toMatchSnapshot} = require('jest-snapshot');
2
2
  const errors = require('@tryghost/errors');
3
- const expect = require('expect');
4
- const utils = require('expect/build/utils');
3
+ const utils = require('@jest/expect-utils');
4
+ const assert = require('assert');
5
5
  const path = require('path');
6
+ const {makeMessageFromMatchMessage} = require('./utils');
6
7
 
7
- class SnapshotManageer {
8
+ class SnapshotManager {
8
9
  constructor() {
9
10
  this.registry = {};
10
11
  this.currentTest = {};
@@ -65,33 +66,56 @@ class SnapshotManageer {
65
66
  }
66
67
 
67
68
  getSnapshotState(testFile, willUpdate) {
68
- // Intilize the SnapshotState, it’s responsible for actually matching
69
+ // Initialize the SnapshotState, it’s responsible for actually matching
69
70
  // actual snapshot with expected one and storing results
70
71
  return new SnapshotState(testFile, {
71
72
  updateSnapshot: willUpdate
72
73
  });
73
74
  }
74
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
+
75
110
  match(received, properties = {}, hint) {
76
111
  const {testFile, snapshotName, willUpdate} = this._getConfig();
77
112
 
78
113
  const snapshotState = this.getSnapshotState(testFile, willUpdate);
79
- // Equals is not exposed from the internals of expect
80
- // This truly bananananas workaround comes from here: https://github.com/facebook/jest/issues/11867
81
- let equals = null;
82
- expect.extend({
83
- __capture_equals__() {
84
- equals = this.equals;
85
- return {pass: true};
86
- }
87
- });
88
- expect().__capture_equals__();
89
-
90
114
  const matcher = toMatchSnapshot.bind({
91
115
  snapshotState,
92
116
  currentTestName: snapshotName,
93
117
  utils,
94
- equals
118
+ equals: utils.equals
95
119
  });
96
120
 
97
121
  // Execute the matcher
@@ -104,4 +128,4 @@ class SnapshotManageer {
104
128
  }
105
129
  }
106
130
 
107
- 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.12",
3
+ "version": "0.4.0",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/jest-snapshot",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,15 +19,16 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "7.11.0",
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
- "@tryghost/errors": "^1.2.11",
29
- "expect": "^27.5.0",
30
- "jest-snapshot": "^27.5.0"
28
+ "@jest/expect": "^28.0.1",
29
+ "@jest/expect-utils": "^28.0.1",
30
+ "@tryghost/errors": "^1.2.13",
31
+ "jest-snapshot": "^28.0.0"
31
32
  },
32
- "gitHead": "6a49350821ab2dd54d4b8a1ac7cde27b72aeaca0"
33
+ "gitHead": "0af921ce800c79f89685307914bd6d704302c043"
33
34
  }