Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Joel Einbinderd2f24e888817
diff --git a/test/jest/playwrightEnvironment.js b/test/jest/playwrightEnvironment.js
index 283bfcceb8..2c1bc84416 100644
--- a/test/jest/playwrightEnvironment.js
+++ b/test/jest/playwrightEnvironment.js
@@ -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 });
}