@tryghost/jest-snapshot 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2022 Ghost Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Jest Snapshot
2
+
3
+ Tools for using jest snapshot without jest
4
+
5
+ ## Install
6
+
7
+ `npm install @tryghost/jest-snapshot --save`
8
+
9
+ or
10
+
11
+ `yarn add @tryghost/jest-snapshot`
12
+
13
+
14
+ ## Usage
15
+
16
+
17
+ ## Develop
18
+
19
+ This is a mono repository, managed with [lerna](https://lernajs.io/).
20
+
21
+ Follow the instructions for the top-level repo.
22
+ 1. `git clone` this repo & `cd` into it as usual
23
+ 2. Run `yarn` to install top-level dependencies.
24
+
25
+
26
+ ## Run
27
+
28
+ - `yarn dev`
29
+
30
+
31
+ ## Test
32
+
33
+ - `yarn lint` run just eslint
34
+ - `yarn test` run lint and tests
35
+
36
+
37
+
38
+
39
+ # Copyright & License
40
+
41
+ Copyright (c) 2013-2022 Ghost Foundation - Released under the [MIT license](LICENSE).
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/jest-snapshot');
@@ -0,0 +1,38 @@
1
+ const expect = require('expect');
2
+ const SnapshotMatcher = require('./snapshot-matcher');
3
+ const snapshotMatcher = new SnapshotMatcher();
4
+
5
+ function matchSnapshotAssertion(properties) {
6
+ this.params = {operator: 'to match a stored snapshot'};
7
+
8
+ const result = snapshotMatcher.matchSnapshot(this.obj, properties);
9
+
10
+ this.params.message = result.message();
11
+ this.assert(result.pass.should.eql(true));
12
+ }
13
+
14
+ const mochaHooks = {
15
+ beforeAll() {
16
+ snapshotMatcher.resetRegistry();
17
+ },
18
+ beforeEach() {
19
+ const {currentTest} = this;
20
+
21
+ snapshotMatcher.setCurrentTest({
22
+ filename: currentTest.file + '.snap',
23
+ nameTemplate: currentTest.fullTitle()
24
+ });
25
+ }
26
+ };
27
+
28
+ const {any, anything, stringMatching} = expect;
29
+
30
+ module.exports = {
31
+ mochaHooks,
32
+ snapshotMatcher,
33
+ matchSnapshot: snapshotMatcher.matchSnapshot,
34
+ matchSnapshotAssertion,
35
+ any,
36
+ anything,
37
+ stringMatching
38
+ };
@@ -0,0 +1,79 @@
1
+ const {SnapshotState, toMatchSnapshot} = require('jest-snapshot');
2
+ const expect = require('expect');
3
+ const utils = require('expect/build/utils');
4
+
5
+ class SnapshotMatcher {
6
+ constructor() {
7
+ this.registry = {};
8
+ this.currentTest = {};
9
+ }
10
+
11
+ _getNameForSnapshot(snapshotFilename, snapshotNameTemplate) {
12
+ if (!this.registry[snapshotFilename]) {
13
+ this.registry[snapshotFilename] = {};
14
+ }
15
+
16
+ const nextCounter = (this.registry[snapshotFilename][snapshotNameTemplate] || 0) + 1;
17
+ this.registry[snapshotFilename][snapshotNameTemplate] = nextCounter;
18
+
19
+ return `${snapshotNameTemplate} ${nextCounter}`;
20
+ }
21
+
22
+ _getConfig() {
23
+ const testFile = this.currentTest.filename;
24
+ const testTitle = this.currentTest.nameTemplate;
25
+ const snapshotName = this._getNameForSnapshot(testFile, testTitle);
26
+ const willUpdate = process.env.SNAPSHOT_UPDATE ? 'all' : 'new';
27
+
28
+ return {testFile, snapshotName, willUpdate};
29
+ }
30
+
31
+ resetRegistry() {
32
+ this.registry = {};
33
+ }
34
+
35
+ setCurrentTest(testConfig) {
36
+ this.currentTest = testConfig;
37
+ }
38
+
39
+ getSnapshotState(testFile, willUpdate) {
40
+ // Intilize the SnapshotState, it’s responsible for actually matching
41
+ // actual snapshot with expected one and storing results
42
+ return new SnapshotState(testFile, {
43
+ updateSnapshot: willUpdate
44
+ });
45
+ }
46
+
47
+ matchSnapshot(received, properties = {}) {
48
+ const {testFile, snapshotName, willUpdate} = this._getConfig();
49
+
50
+ const snapshotState = this.getSnapshotState(testFile, willUpdate);
51
+ // Equals is not exposed from the internals of expect
52
+ // This truly bananananas workaround comes from here: https://github.com/facebook/jest/issues/11867
53
+ let equals = () => {};
54
+ expect.extend({
55
+ __capture_equals__() {
56
+ equals = this.equals;
57
+ return {pass: true};
58
+ }
59
+ });
60
+ expect().__capture_equals__();
61
+
62
+ const matcher = toMatchSnapshot.bind({
63
+ snapshotState,
64
+ currentTestName: snapshotName,
65
+ utils,
66
+ equals
67
+ });
68
+
69
+ // Execute the matcher
70
+ const result = matcher(received, properties);
71
+
72
+ // Store the state of snapshot, depending on updateSnapshot value
73
+ snapshotState.save();
74
+
75
+ return result;
76
+ }
77
+ }
78
+
79
+ module.exports = SnapshotMatcher;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@tryghost/jest-snapshot",
3
+ "version": "0.1.0",
4
+ "repository": "https://github.com/TryGhost/framework/tree/main/packages/jest-snapshot",
5
+ "author": "Ghost Foundation",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "scripts": {
9
+ "dev": "echo \"Implement me!\"",
10
+ "test": "NODE_ENV=testing c8 --all --check-coverage --reporter html --reporter text-summary mocha './test/**/*.test.js'",
11
+ "lint": "eslint . --ext .js --cache",
12
+ "posttest": "yarn lint"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "lib"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "devDependencies": {
22
+ "c8": "7.11.0",
23
+ "mocha": "9.2.0",
24
+ "should": "13.2.3",
25
+ "sinon": "13.0.1"
26
+ },
27
+ "dependencies": {
28
+ "expect": "^27.5.0",
29
+ "jest-snapshot": "^27.5.0"
30
+ },
31
+ "gitHead": "ba2449737cd3a66d811e34b5b28b86f5d3538396"
32
+ }