Skip to content

Commit d2f24e8

Browse files
integrate toBeGolden with jest's snapshot system (#3124)
1 parent e5afd92 commit d2f24e8

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

test/jest/playwrightEnvironment.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,31 @@ class PlaywrightEnvironment extends NodeEnvironment {
120120

121121
const testOptions = this.global.testOptions;
122122
function toBeGolden(received, goldenName) {
123-
return GoldenUtils.compare(received, {
123+
const {snapshotState} = this;
124+
const updateSnapshot = snapshotState._updateSnapshot;
125+
const expectedPath = path.join(testOptions.GOLDEN_DIR, goldenName);
126+
const fileExists = fs.existsSync(expectedPath);
127+
if (updateSnapshot === 'all' || (updateSnapshot === 'new' && !fileExists)) {
128+
fs.writeFileSync(expectedPath, received);
129+
if (fileExists)
130+
snapshotState.updated++;
131+
else
132+
snapshotState.added++;
133+
return {
134+
pass: true
135+
}
136+
};
137+
138+
const {pass, message} = GoldenUtils.compare(received, {
124139
goldenPath: testOptions.GOLDEN_DIR,
125140
outputPath: testOptions.OUTPUT_DIR,
126141
goldenName
127142
});
143+
if (pass)
144+
snapshotState.matched++;
145+
else
146+
snapshotState.unmatched++;
147+
return {pass, message};
128148
};
129149
this.global.expect.extend({ toBeGolden });
130150
}

0 commit comments

Comments
 (0)