@tryghost/jest-snapshot 0.5.15 → 0.5.17

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2013-2023 Ghost Foundation
3
+ Copyright (c) 2013-2025 Ghost Foundation
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -38,4 +38,4 @@ Follow the instructions for the top-level repo.
38
38
 
39
39
  # Copyright & License
40
40
 
41
- Copyright (c) 2013-2023 Ghost Foundation - Released under the [MIT license](LICENSE).
41
+ Copyright (c) 2013-2025 Ghost Foundation - Released under the [MIT license](LICENSE).
@@ -1,38 +1,74 @@
1
- const {SnapshotState, toMatchSnapshot} = require('jest-snapshot');
1
+ const {SnapshotState, toMatchSnapshot, EXTENSION} = require('jest-snapshot');
2
2
  const errors = require('@tryghost/errors');
3
3
  const utils = require('@jest/expect-utils');
4
4
  const assert = require('assert');
5
5
  const path = require('path');
6
6
  const {makeMessageFromMatchMessage} = require('./utils');
7
7
 
8
+ const DOT_EXTENSION = `.${EXTENSION}`;
9
+
8
10
  class SnapshotManager {
9
11
  constructor() {
10
12
  this.registry = {};
11
13
  this.currentTest = {};
12
- this.defaultSnapshotPath = '__snapshots__';
14
+ this.defaultSnapshotRoot = '__snapshots__';
13
15
  }
14
16
 
15
- _getNameForSnapshot(snapshotFilename, snapshotNameTemplate) {
16
- if (!this.registry[snapshotFilename]) {
17
- this.registry[snapshotFilename] = {};
18
- }
19
-
20
- const nextCounter = (this.registry[snapshotFilename][snapshotNameTemplate] || 0) + 1;
21
- this.registry[snapshotFilename][snapshotNameTemplate] = nextCounter;
17
+ /**
18
+ * Determines whether to update all snapshots or only new ones
19
+ * @returns {string} e.g. 'all' or 'new'
20
+ */
21
+ _willUpdate() {
22
+ const updateSnapshots = (
23
+ process.env.SNAPSHOT_UPDATE
24
+ || process.env.UPDATE_SNAPSHOT
25
+ || process.env.SNAPSHOTS_UPDATE
26
+ || process.env.UPDATE_SNAPSHOTS
27
+ );
22
28
 
23
- return `${snapshotNameTemplate} ${nextCounter}`;
29
+ return updateSnapshots ? 'all' : 'new';
24
30
  }
25
31
 
26
- _resolveSnapshotFilePath(testFile) {
27
- const parsedPath = path.parse(testFile);
32
+ /**
33
+ * Looks in the registry for a snapshot with the same filename and nameTemplate
34
+ * If needed, increments the counter
35
+ * Returns nametemplate with the counter appended.
36
+ *
37
+ * @param {string} testPath e.g. '/path/to/tests/my-fake.test.js'
38
+ * @param {string} testTitle e.g. 'My fake test title'
39
+ * @returns {string} e.g. 'My fake test title 1'
40
+ */
41
+ _getNameForSnapshot(testPath, testTitle) {
42
+ if (!(testPath in this.registry)) {
43
+ this.registry[testPath] = {};
44
+ }
45
+
46
+ const nextCounter = (this.registry[testPath][testTitle] || 0) + 1;
47
+ this.registry[testPath][testTitle] = nextCounter;
28
48
 
29
- parsedPath.dir = path.join(parsedPath.dir, this.defaultSnapshotPath);
49
+ return `${testTitle} ${nextCounter}`;
50
+ }
30
51
 
31
- return path.format(parsedPath);
52
+ /**
53
+ * Takes the full path to the test file and returns the full path to the snapshot file
54
+ *
55
+ * @param {string} testPath e.g. '/path/to/tests/my-fake.test.js'
56
+ * @returns {string} snapshotFilePath e.g. '/path/to/tests/__snapshots__/my-fake.test.js.snap'
57
+ */
58
+ _resolveSnapshotFilePath(testPath) {
59
+ return path.join(
60
+ path.join(path.dirname(testPath), this.defaultSnapshotRoot),
61
+ path.basename(testPath) + DOT_EXTENSION
62
+ );
32
63
  }
33
64
 
65
+ /**
66
+ * Returns config for the current test
67
+ * Throws an error if the current test is not configured
68
+ * @returns {Object} e.g. {snapshotPath: 'test/__snapshots__/my-fake.test.js.snap', snapshotName: 'My fake test title 1', willUpdate: 'new'}
69
+ */
34
70
  _getConfig() {
35
- if (!this.currentTest.filename || !this.currentTest.nameTemplate) {
71
+ if (!this.currentTest.testPath || !this.currentTest.testTitle) {
36
72
  throw new errors.IncorrectUsageError({
37
73
  message: 'Unable to run snapshot tests, current test was not configured',
38
74
  context: 'Snapshot testing requires current test filename and nameTemplate to be set for each test',
@@ -40,46 +76,64 @@ class SnapshotManager {
40
76
  });
41
77
  }
42
78
 
43
- let testFile = this.currentTest.filename;
44
- const testTitle = this.currentTest.nameTemplate;
45
- const snapshotName = this._getNameForSnapshot(testFile, testTitle);
46
- const updateSnapshots = (
47
- process.env.SNAPSHOT_UPDATE
48
- || process.env.UPDATE_SNAPSHOT
49
- || process.env.SNAPSHOTS_UPDATE
50
- || process.env.UPDATE_SNAPSHOTS
51
- );
52
- const willUpdate = updateSnapshots ? 'all' : 'new';
79
+ const {testPath, testTitle} = this.currentTest;
80
+
81
+ const snapshotName = this._getNameForSnapshot(testPath, testTitle);
82
+ const willUpdate = this._willUpdate();
53
83
 
54
84
  // Set full path
55
- testFile = this._resolveSnapshotFilePath(testFile);
85
+ const snapshotPath = this._resolveSnapshotFilePath(testPath);
56
86
 
57
- return {testFile, snapshotName, willUpdate};
87
+ return {snapshotPath, snapshotName, willUpdate};
58
88
  }
59
89
 
90
+ /**
91
+ * Resets the registry to an empty object
92
+ */
60
93
  resetRegistry() {
61
94
  this.registry = {};
62
95
  }
63
96
 
97
+ /**
98
+ * Resets the registry for the current test only
99
+ */
64
100
  resetRegistryForCurrentTest() {
65
- const testTitle = this.currentTest.nameTemplate;
66
- if (this.currentTest.filename in this.registry && testTitle in this.registry[this.currentTest.filename]) {
67
- delete this.registry[this.currentTest.filename][testTitle];
101
+ const {testPath, testTitle} = this.currentTest;
102
+ if (testPath in this.registry && testTitle in this.registry[testPath]) {
103
+ delete this.registry[testPath][testTitle];
68
104
  }
69
105
  }
70
106
 
107
+ /**
108
+ * @param {Object} testConfig
109
+ * @param {String} testConfig.testPath full path to the test file
110
+ * @param {String} testConfig.testTitle the full title of the test - all the describe and it names concatenated
111
+ */
71
112
  setCurrentTest(testConfig) {
72
113
  this.currentTest = testConfig;
73
114
  }
74
115
 
75
- getSnapshotState(testFile, willUpdate) {
116
+ /**
117
+ * Gets a SnapshotState instance for the current test
118
+ * @param {string} snapshotPath e.g. 'test/__snapshots__/my-fake.test.js.snap'
119
+ * @param {string} willUpdate e.g. 'new'
120
+ * @returns {SnapshotState}
121
+ */
122
+ getSnapshotState(snapshotPath, willUpdate) {
76
123
  // Initialize the SnapshotState, it’s responsible for actually matching
77
124
  // actual snapshot with expected one and storing results
78
- return new SnapshotState(testFile, {
125
+ return new SnapshotState(snapshotPath, {
79
126
  updateSnapshot: willUpdate
80
127
  });
81
128
  }
82
129
 
130
+ /**
131
+ * Asserts the snapshot values match using the match function
132
+ * Does some formatting of the error message
133
+ *
134
+ * @param {{field: string}} response
135
+ * @param {{properties: Object, field: string, error: Object, hint: string}} assertion
136
+ */
83
137
  assertSnapshot(response, assertion) {
84
138
  const {properties, field, error} = assertion;
85
139
 
@@ -116,10 +170,21 @@ class SnapshotManager {
116
170
  assert.equal(match.pass, true, error);
117
171
  }
118
172
 
173
+ /**
174
+ * Calls Jest's toMatchSnapshot function
175
+ * Binds the snapshotState, currentTestName, utils and equals function to the matcher
176
+ * Executes the matcher
177
+ * Saves the snapshot state
178
+ *
179
+ * @param {Object} received - our actual value
180
+ * @param {Object} [properties] - the properties we want to match against
181
+ * @param {string} [hint] - a hint to help with the match
182
+ * @returns {Object} result of the match
183
+ */
119
184
  match(received, properties = {}, hint) {
120
- const {testFile, snapshotName, willUpdate} = this._getConfig();
185
+ const {snapshotPath, snapshotName, willUpdate} = this._getConfig();
121
186
 
122
- const snapshotState = this.getSnapshotState(testFile, willUpdate);
187
+ const snapshotState = this.getSnapshotState(snapshotPath, willUpdate);
123
188
  const matcher = toMatchSnapshot.bind({
124
189
  snapshotState,
125
190
  currentTestName: snapshotName,
@@ -12,9 +12,20 @@ function matchSnapshotAssertion(properties) {
12
12
  }
13
13
 
14
14
  const mochaHooks = {
15
+
16
+ /**
17
+ * Runs before all tests
18
+ * Resets the registry so we start with a clean slate
19
+ */
15
20
  beforeAll() {
16
21
  snapshotManager.resetRegistry();
17
22
  },
23
+
24
+ /**
25
+ * Runs before each test
26
+ * Passes the current test config to the snapshot manager
27
+ * If we're running a retry, reset the registry for this test only
28
+ */
18
29
  beforeEach() {
19
30
  const {currentTest} = this;
20
31
 
@@ -24,8 +35,8 @@ const mochaHooks = {
24
35
  }
25
36
 
26
37
  snapshotManager.setCurrentTest({
27
- filename: currentTest.file + '.snap',
28
- nameTemplate: currentTest.fullTitle()
38
+ testPath: currentTest.file,
39
+ testTitle: currentTest.fullTitle()
29
40
  });
30
41
  }
31
42
  };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryghost/jest-snapshot",
3
- "version": "0.5.15",
3
+ "version": "0.5.17",
4
4
  "repository": "https://github.com/TryGhost/framework/tree/main/packages/jest-snapshot",
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 --all --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
10
+ "test": "NODE_ENV=testing c8 --all --check-coverage --100 --reporter text --reporter cobertura --reporter html-spa mocha './test/**/*.test.js'",
11
11
  "lint": "eslint . --ext .js --cache",
12
12
  "posttest": "yarn lint"
13
13
  },
@@ -19,16 +19,16 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "10.1.2",
23
- "mocha": "10.6.0",
22
+ "c8": "10.1.3",
23
+ "mocha": "10.7.3",
24
24
  "should": "13.2.3",
25
25
  "sinon": "18.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.3.5",
30
+ "@tryghost/errors": "^1.3.7",
31
31
  "jest-snapshot": "^29.0.0"
32
32
  },
33
- "gitHead": "d3d489cb44ac3d4e35e21388d48ee0d94e496d86"
33
+ "gitHead": "b169bd1734b749c3abdaac134f030756b9a282bb"
34
34
  }