@tryghost/jest-snapshot 0.1.0 → 0.3.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,11 +1,11 @@
1
1
  const expect = require('expect');
2
- const SnapshotMatcher = require('./snapshot-matcher');
3
- const snapshotMatcher = new SnapshotMatcher();
2
+ const SnapshotManager = require('./snapshot-manager');
3
+ const snapshotManager = new SnapshotManager();
4
4
 
5
5
  function matchSnapshotAssertion(properties) {
6
6
  this.params = {operator: 'to match a stored snapshot'};
7
7
 
8
- const result = snapshotMatcher.matchSnapshot(this.obj, properties);
8
+ const result = snapshotManager.match(this.obj, properties);
9
9
 
10
10
  this.params.message = result.message();
11
11
  this.assert(result.pass.should.eql(true));
@@ -13,12 +13,12 @@ function matchSnapshotAssertion(properties) {
13
13
 
14
14
  const mochaHooks = {
15
15
  beforeAll() {
16
- snapshotMatcher.resetRegistry();
16
+ snapshotManager.resetRegistry();
17
17
  },
18
18
  beforeEach() {
19
19
  const {currentTest} = this;
20
20
 
21
- snapshotMatcher.setCurrentTest({
21
+ snapshotManager.setCurrentTest({
22
22
  filename: currentTest.file + '.snap',
23
23
  nameTemplate: currentTest.fullTitle()
24
24
  });
@@ -29,8 +29,7 @@ const {any, anything, stringMatching} = expect;
29
29
 
30
30
  module.exports = {
31
31
  mochaHooks,
32
- snapshotMatcher,
33
- matchSnapshot: snapshotMatcher.matchSnapshot,
32
+ snapshotManager,
34
33
  matchSnapshotAssertion,
35
34
  any,
36
35
  anything,
@@ -1,11 +1,14 @@
1
1
  const {SnapshotState, toMatchSnapshot} = require('jest-snapshot');
2
+ const errors = require('@tryghost/errors');
2
3
  const expect = require('expect');
3
4
  const utils = require('expect/build/utils');
5
+ const path = require('path');
4
6
 
5
- class SnapshotMatcher {
7
+ class SnapshotManageer {
6
8
  constructor() {
7
9
  this.registry = {};
8
10
  this.currentTest = {};
11
+ this.defaultSnapshotPath = '__snapshots__';
9
12
  }
10
13
 
11
14
  _getNameForSnapshot(snapshotFilename, snapshotNameTemplate) {
@@ -19,12 +22,31 @@ class SnapshotMatcher {
19
22
  return `${snapshotNameTemplate} ${nextCounter}`;
20
23
  }
21
24
 
25
+ _resolveSnapshotFilePath(testFile) {
26
+ const parsedPath = path.parse(testFile);
27
+
28
+ parsedPath.dir = path.join(parsedPath.dir, this.defaultSnapshotPath);
29
+
30
+ return path.format(parsedPath);
31
+ }
32
+
22
33
  _getConfig() {
23
- const testFile = this.currentTest.filename;
34
+ if (!this.currentTest.filename || !this.currentTest.nameTemplate) {
35
+ throw new errors.IncorrectUsageError({
36
+ message: 'Unable to run snapshot tests, current test was not configured',
37
+ context: 'Snapshot testing requires current test filename and nameTemplate to be set for each test',
38
+ help: 'Did you forget to do export.mochaHooks?'
39
+ });
40
+ }
41
+
42
+ let testFile = this.currentTest.filename;
24
43
  const testTitle = this.currentTest.nameTemplate;
25
44
  const snapshotName = this._getNameForSnapshot(testFile, testTitle);
26
45
  const willUpdate = process.env.SNAPSHOT_UPDATE ? 'all' : 'new';
27
46
 
47
+ // Set full path
48
+ testFile = this._resolveSnapshotFilePath(testFile);
49
+
28
50
  return {testFile, snapshotName, willUpdate};
29
51
  }
30
52
 
@@ -44,13 +66,13 @@ class SnapshotMatcher {
44
66
  });
45
67
  }
46
68
 
47
- matchSnapshot(received, properties = {}) {
69
+ match(received, properties = {}, hint) {
48
70
  const {testFile, snapshotName, willUpdate} = this._getConfig();
49
71
 
50
72
  const snapshotState = this.getSnapshotState(testFile, willUpdate);
51
73
  // Equals is not exposed from the internals of expect
52
74
  // This truly bananananas workaround comes from here: https://github.com/facebook/jest/issues/11867
53
- let equals = () => {};
75
+ let equals = null;
54
76
  expect.extend({
55
77
  __capture_equals__() {
56
78
  equals = this.equals;
@@ -67,7 +89,7 @@ class SnapshotMatcher {
67
89
  });
68
90
 
69
91
  // Execute the matcher
70
- const result = matcher(received, properties);
92
+ const result = matcher(received, properties, hint);
71
93
 
72
94
  // Store the state of snapshot, depending on updateSnapshot value
73
95
  snapshotState.save();
@@ -76,4 +98,4 @@ class SnapshotMatcher {
76
98
  }
77
99
  }
78
100
 
79
- module.exports = SnapshotMatcher;
101
+ module.exports = SnapshotManageer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/jest-snapshot",
3
- "version": "0.1.0",
3
+ "version": "0.3.2",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/jest-snapshot",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -25,8 +25,9 @@
25
25
  "sinon": "13.0.1"
26
26
  },
27
27
  "dependencies": {
28
+ "@tryghost/errors": "^1.2.2",
28
29
  "expect": "^27.5.0",
29
30
  "jest-snapshot": "^27.5.0"
30
31
  },
31
- "gitHead": "ba2449737cd3a66d811e34b5b28b86f5d3538396"
32
+ "gitHead": "e954893434fda3f8c8de1970aaaa6f6a8b297f5d"
32
33
  }