@tryghost/jest-snapshot 0.2.0 → 0.3.3
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/snapshot-manager.js +26 -4
- package/package.json +5 -4
package/lib/snapshot-manager.js
CHANGED
|
@@ -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
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 SnapshotManageer {
|
|
|
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
|
-
|
|
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 SnapshotManageer {
|
|
|
44
66
|
});
|
|
45
67
|
}
|
|
46
68
|
|
|
47
|
-
match(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 SnapshotManageer {
|
|
|
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();
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/jest-snapshot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.3",
|
|
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 --reporter html --reporter text-summary mocha './test/**/*.test.js'",
|
|
10
|
+
"test": "NODE_ENV=testing c8 --all --check-coverage --reporter html --reporter text-summary --reporter cobertura mocha './test/**/*.test.js'",
|
|
11
11
|
"lint": "eslint . --ext .js --cache",
|
|
12
12
|
"posttest": "yarn lint"
|
|
13
13
|
},
|
|
@@ -20,13 +20,14 @@
|
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"c8": "7.11.0",
|
|
23
|
-
"mocha": "9.2.
|
|
23
|
+
"mocha": "9.2.1",
|
|
24
24
|
"should": "13.2.3",
|
|
25
25
|
"sinon": "13.0.1"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"@tryghost/errors": "^1.2.3",
|
|
28
29
|
"expect": "^27.5.0",
|
|
29
30
|
"jest-snapshot": "^27.5.0"
|
|
30
31
|
},
|
|
31
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "64606b3f53d3ec8e6bf988f694d9dc4bbf5fac70"
|
|
32
33
|
}
|