integrate toBeGolden with jest's snapshot system (#3124)

This commit is contained in:
Joel Einbinder 2020-07-23 14:19:51 -07:00 committed by GitHub
parent e5afd92762
commit d2f24e8888
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -120,11 +120,31 @@ class PlaywrightEnvironment extends NodeEnvironment {
const testOptions = this.global.testOptions;
function toBeGolden(received, goldenName) {
return GoldenUtils.compare(received, {
const {snapshotState} = this;
const updateSnapshot = snapshotState._updateSnapshot;
const expectedPath = path.join(testOptions.GOLDEN_DIR, goldenName);
const fileExists = fs.existsSync(expectedPath);
if (updateSnapshot === 'all' || (updateSnapshot === 'new' && !fileExists)) {
fs.writeFileSync(expectedPath, received);
if (fileExists)
snapshotState.updated++;
else
snapshotState.added++;
return {
pass: true
}
};
const {pass, message} = GoldenUtils.compare(received, {
goldenPath: testOptions.GOLDEN_DIR,
outputPath: testOptions.OUTPUT_DIR,
goldenName
});
if (pass)
snapshotState.matched++;
else
snapshotState.unmatched++;
return {pass, message};
};
this.global.expect.extend({ toBeGolden });
}