From d07053318072fd9860b88087792bbe52d2c2a388 Mon Sep 17 00:00:00 2001
From: Kai Hao <kevin830726@gmail.com>
Date: Fri, 16 Apr 2021 10:25:30 +0800
Subject: [PATCH] Move the setup to our own fork of just-environment-puppeteer

---
 .../jest-environment-puppeteer/index.js       | 39 ++++++++++++++++-
 .../config/jest-puppeteer-environment.js      | 43 -------------------
 2 files changed, 38 insertions(+), 44 deletions(-)
 delete mode 100644 packages/scripts/config/jest-puppeteer-environment.js

diff --git a/packages/scripts/config/jest-environment-puppeteer/index.js b/packages/scripts/config/jest-environment-puppeteer/index.js
index 845a30d1705c7c..292fae0de26f97 100644
--- a/packages/scripts/config/jest-environment-puppeteer/index.js
+++ b/packages/scripts/config/jest-environment-puppeteer/index.js
@@ -16,6 +16,9 @@
 /**
  * External dependencies
  */
+const path = require( 'path' );
+const { writeFile, mkdir } = require( 'fs' ).promises;
+const filenamify = require( 'filenamify' );
 const NodeEnvironment = require( 'jest-environment-node' );
 const chalk = require( 'chalk' );
 
@@ -25,7 +28,9 @@ const chalk = require( 'chalk' );
 const { readConfig, getPuppeteer } = require( './config' );
 
 const handleError = ( error ) => {
-	process.emit( 'uncaughtException', error );
+	// To match the same behavior in jest-jasmine2.
+	// eslint-disable-next-line no-console
+	console.error( error );
 };
 
 const KEYS = {
@@ -34,6 +39,9 @@ const KEYS = {
 	ENTER: '\r',
 };
 
+const root = process.env.GITHUB_WORKSPACE || process.cwd();
+const ARTIFACTS_PATH = path.join( root, 'artifacts' );
+
 class PuppeteerEnvironment extends NodeEnvironment {
 	// Jest is not available here, so we have to reverse engineer
 	// the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
@@ -158,6 +166,14 @@ class PuppeteerEnvironment extends NodeEnvironment {
 		};
 
 		await this.global.jestPuppeteer.resetBrowser();
+
+		try {
+			await mkdir( ARTIFACTS_PATH );
+		} catch ( err ) {
+			if ( err.code !== 'EEXIST' ) {
+				throw err;
+			}
+		}
 	}
 
 	async teardown() {
@@ -179,6 +195,27 @@ class PuppeteerEnvironment extends NodeEnvironment {
 			await browser.disconnect();
 		}
 	}
+
+	async storeArtifacts( testName ) {
+		const datetime = new Date().toISOString().split( '.' )[ 0 ];
+		const fileName = filenamify( `${ testName } ${ datetime }`, {
+			replacement: '-',
+		} );
+		await writeFile(
+			`${ ARTIFACTS_PATH }/${ fileName }-snapshot.html`,
+			await this.global.page.content()
+		);
+		await this.global.page.screenshot( {
+			path: `${ ARTIFACTS_PATH }/${ fileName }.jpg`,
+		} );
+	}
+
+	async handleTestEvent( event, state ) {
+		if ( event.name === 'test_fn_failure' ) {
+			const testName = state.currentlyRunningTest.name;
+			await this.storeArtifacts( testName );
+		}
+	}
 }
 
 module.exports = PuppeteerEnvironment;
diff --git a/packages/scripts/config/jest-puppeteer-environment.js b/packages/scripts/config/jest-puppeteer-environment.js
deleted file mode 100644
index a895b38d8e7001..00000000000000
--- a/packages/scripts/config/jest-puppeteer-environment.js
+++ /dev/null
@@ -1,43 +0,0 @@
-const { writeFile, mkdir } = require( 'fs' ).promises;
-const filenamify = require( 'filenamify' );
-const PuppeteerEnvironment = require( 'jest-environment-puppeteer' );
-
-const root = process.env.GITHUB_WORKSPACE || process.cwd();
-const ARTIFACTS_PATH = root + '/artifacts';
-
-class JestPuppeteerEnvironment extends PuppeteerEnvironment {
-	async setup() {
-		await super.setup();
-
-		try {
-			await mkdir( ARTIFACTS_PATH );
-		} catch ( err ) {
-			if ( err.code !== 'EEXIST' ) {
-				throw err;
-			}
-		}
-	}
-
-	async storeArtifacts( testName ) {
-		const datetime = new Date().toISOString().split( '.' )[ 0 ];
-		const fileName = filenamify( `${ testName } ${ datetime }`, {
-			replacement: '-',
-		} );
-		await writeFile(
-			`${ ARTIFACTS_PATH }/${ fileName }-snapshot.html`,
-			await this.global.page.content()
-		);
-		await this.global.page.screenshot( {
-			path: `${ ARTIFACTS_PATH }/${ fileName }.jpg`,
-		} );
-	}
-
-	async handleTestEvent( event, state ) {
-		if ( event.name === 'test_fn_failure' ) {
-			const testName = state.currentlyRunningTest.name;
-			await this.storeArtifacts( testName );
-		}
-	}
-}
-
-module.exports = JestPuppeteerEnvironment;