Skip to content

Commit 03dbca0

Browse files
committed
Moved willUpdate logic into it's own function
- This logic is clogging up the getConfig function, making it harder to reason about what getConfig is doing - I have a hunch we don't need getConfig at all... - This is a step in a hopefully better direction
1 parent 83e735b commit 03dbca0

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

packages/jest-snapshot/lib/SnapshotManager.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ class SnapshotManager {
1414
this.defaultSnapshotRoot = '__snapshots__';
1515
}
1616

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+
);
28+
29+
return updateSnapshots ? 'all' : 'new';
30+
}
31+
1732
/**
1833
* Looks in the registry for a snapshot with the same filename and nameTemplate
1934
* If needed, increments the counter
@@ -64,13 +79,7 @@ class SnapshotManager {
6479
const {testPath, testTitle} = this.currentTest;
6580

6681
const snapshotName = this._getNameForSnapshot(testPath, testTitle);
67-
const updateSnapshots = (
68-
process.env.SNAPSHOT_UPDATE
69-
|| process.env.UPDATE_SNAPSHOT
70-
|| process.env.SNAPSHOTS_UPDATE
71-
|| process.env.UPDATE_SNAPSHOTS
72-
);
73-
const willUpdate = updateSnapshots ? 'all' : 'new';
82+
const willUpdate = this._willUpdate();
7483

7584
// Set full path
7685
const snapshotPath = this._resolveSnapshotFilePath(testPath);

packages/jest-snapshot/test/SnapshotManager.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ describe('Snapshot Manager', function () {
117117
assert.equal(result, '/abc/cde/__snapshots__/a.test.js.snap');
118118
});
119119

120+
it('_willUpdate: will return all when the environment variable is set', function () {
121+
const originalValue = process.env.SNAPSHOT_UPDATE || '';
122+
process.env.SNAPSHOT_UPDATE = 1;
123+
const snapshotManager = new SnapshotManager();
124+
assert.equal(snapshotManager._willUpdate(), 'all');
125+
process.env.SNAPSHOT_UPDATE = originalValue;
126+
});
127+
128+
it('_willUpdate: will return new when the environment variable is not set', function () {
129+
const originalValue = process.env.SNAPSHOT_UPDATE || '';
130+
process.env.SNAPSHOT_UPDATE = '';
131+
const snapshotManager = new SnapshotManager();
132+
assert.equal(snapshotManager._willUpdate(), 'new');
133+
process.env.SNAPSHOT_UPDATE = originalValue;
134+
});
135+
120136
it('_getConfig: will throw if no current test is set', function () {
121137
const snapshotManager = new SnapshotManager();
122138

0 commit comments

Comments
 (0)