Skip to content

Commit

Permalink
Move the setup to our own fork of just-environment-puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Apr 16, 2021
1 parent 228760d commit d070533
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
39 changes: 38 additions & 1 deletion packages/scripts/config/jest-environment-puppeteer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand All @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
43 changes: 0 additions & 43 deletions packages/scripts/config/jest-puppeteer-environment.js

This file was deleted.

0 comments on commit d070533

Please sign in to comment.