Skip to content

Commit

Permalink
Scripts: Add test-e2e script
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 4, 2018
1 parent 2fc34bc commit 3d31dce
Show file tree
Hide file tree
Showing 13 changed files with 497 additions and 402 deletions.
533 changes: 260 additions & 273 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,15 @@
"espree": "3.5.4",
"glob": "7.1.2",
"husky": "0.14.3",
"jest-puppeteer": "3.2.1",
"lerna": "3.4.3",
"lint-staged": "7.2.0",
"lint-staged": "7.3.0",
"lodash": "4.17.10",
"mkdirp": "0.5.1",
"node-sass": "4.9.2",
"path-type": "3.0.0",
"pegjs": "0.10.0",
"phpegjs": "1.0.0-beta7",
"postcss-color-function": "4.0.1",
"puppeteer": "1.6.1",
"react-test-renderer": "16.6.3",
"rimraf": "2.6.2",
"rtlcss": "2.4.0",
Expand Down Expand Up @@ -174,7 +172,7 @@
"publish:prod": "npm run build:packages && lerna publish",
"test": "npm run lint && npm run test-unit",
"pretest-e2e": "concurrently \"./bin/reset-e2e-tests.sh\" \"npm run build\"",
"test-e2e": "cross-env JEST_PUPPETEER_CONFIG=test/e2e/puppeteer.config.js wp-scripts test-unit-js --config test/e2e/jest.config.json --runInBand",
"test-e2e": "wp-scripts test-e2e --config test/e2e/jest.config.json",
"test-e2e:watch": "npm run test-e2e -- --watch",
"test-php": "npm run lint-php && npm run test-unit-php",
"test-unit": "wp-scripts test-unit-js --config test/unit/jest.config.json",
Expand Down
21 changes: 21 additions & 0 deletions packages/scripts/config/jest-e2e.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
const path = require( 'path' );

/**
* Internal dependencies
*/
const { hasBabelConfig } = require( '../utils' );

const jestE2EConfig = {
preset: 'jest-puppeteer',
};

if ( ! hasBabelConfig() ) {
jestE2EConfig.transform = {
'^.+\\.jsx?$': path.join( __dirname, 'babel-transform' ),
};
}

module.exports = jestE2EConfig;
21 changes: 21 additions & 0 deletions packages/scripts/config/jest-unit.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
const path = require( 'path' );

/**
* Internal dependencies
*/
const { hasBabelConfig } = require( '../utils' );

const jestUnitConfig = {
preset: '@wordpress/jest-preset-default',
};

if ( ! hasBabelConfig() ) {
jestUnitConfig.transform = {
'^.+\\.jsx?$': path.join( __dirname, 'babel-transform' ),
};
}

module.exports = jestUnitConfig;
28 changes: 0 additions & 28 deletions packages/scripts/config/jest.config.js

This file was deleted.

File renamed without changes.
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"cross-spawn": "^5.1.0",
"eslint": "^4.19.1",
"jest": "^23.6.0",
"jest-puppeteer": "^3.5.2",
"npm-package-json-lint": "^3.3.1",
"puppeteer": "^1.10.0",
"read-pkg-up": "^1.0.1",
"resolve-bin": "^0.4.0"
},
Expand Down
44 changes: 44 additions & 0 deletions packages/scripts/scripts/test-e2e-jest-puppeteer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'test';
process.env.NODE_ENV = 'test';

// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on( 'unhandledRejection', ( err ) => {
throw err;
} );

/**
* External dependencies
*/
const jest = require( 'jest' );

/**
* Internal dependencies
*/
const {
fromConfigRoot,
getCliArgs,
hasCliArg,
hasProjectFile,
hasJestConfig,
} = require( '../utils' );

// Provides a default config path for Puppeteer when jest-puppeteer.config.js
// wasn't found at the root of the project or a custom path wasn't defined
// using JEST_PUPPETEER_CONFIG environment variable.
if ( ! hasProjectFile( 'jest-puppeteer.config.js' ) && ! process.env.JEST_PUPPETEER_CONFIG ) {
process.env.JEST_PUPPETEER_CONFIG = fromConfigRoot( 'puppeteer.config.js' );
}

const hasRunInBand = hasCliArg( '--runInBand' ) ||
hasCliArg( '-i' );

jest.run(
[].concat(
! hasJestConfig() && [ '--config', JSON.stringify( fromConfigRoot( 'jest-e2e.config' ) ) ],
! hasRunInBand && '--runInBand',
getCliArgs()
).filter( Boolean )
);
1 change: 1 addition & 0 deletions packages/scripts/scripts/test-e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require( './test-e2e-jest-puppeteer' );
15 changes: 4 additions & 11 deletions packages/scripts/scripts/test-unit-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ const jest = require( 'jest' );
* Internal dependencies
*/
const {
fromConfigRoot,
getCliArgs,
hasCliArg,
hasProjectFile,
hasPackageProp,
hasJestConfig,
} = require( '../utils' );

const args = getCliArgs();

const hasJestConfig = hasCliArg( '-c' ) ||
hasCliArg( '--config' ) ||
hasProjectFile( 'jest.config.js' ) ||
hasProjectFile( 'jest.config.json' ) ||
hasPackageProp( 'jest' );

const config = ! hasJestConfig ?
[ '--config', JSON.stringify( require( '../config/jest.config' ) ) ] :
const config = ! hasJestConfig() ?
[ '--config', JSON.stringify( fromConfigRoot( 'jest-unit.config' ) ) ] :
[];

jest.run( [ ...config, ...args ] );
85 changes: 85 additions & 0 deletions packages/scripts/utils/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* External dependencies
*/
const spawn = require( 'cross-spawn' );

/**
* Internal dependencies
*/
const {
fromScriptsRoot,
hasScriptFile,
} = require( './file' );
const {
exit,
getCliArgs,
} = require( './process' );

const getCliArg = ( arg ) => {
for ( const cliArg of getCliArgs() ) {
const [ name, value ] = cliArg.split( '=' );
if ( name === arg ) {
return value || null;
}
}
};

const hasCliArg = ( arg ) => getCliArg( arg ) !== undefined;

const handleSignal = ( signal ) => {
if ( signal === 'SIGKILL' ) {
// eslint-disable-next-line no-console
console.log(
'The script failed because the process exited too early. ' +
'This probably means the system ran out of memory or someone called ' +
'`kill -9` on the process.'
);
} else if ( signal === 'SIGTERM' ) {
// eslint-disable-next-line no-console
console.log(
'The script failed because the process exited too early. ' +
'Someone might have called `kill` or `killall`, or the system could ' +
'be shutting down.'
);
}
exit( 1 );
};

const spawnScript = ( scriptName, args = [] ) => {
if ( ! scriptName ) {
// eslint-disable-next-line no-console
console.log( 'Script name is missing.' );
exit( 1 );
}

if ( ! hasScriptFile( scriptName ) ) {
// eslint-disable-next-line no-console
console.log(
'Unknown script "' + scriptName + '". ' +
'Perhaps you need to update @wordpress/scripts?'
);
exit( 1 );
}

const { signal, status } = spawn.sync(
'node',
[
fromScriptsRoot( scriptName ),
...args,
],
{ stdio: 'inherit' },
);

if ( signal ) {
handleSignal( signal );
}

exit( status );
};

module.exports = {
getCliArg,
getCliArgs,
hasCliArg,
spawnScript,
};
32 changes: 32 additions & 0 deletions packages/scripts/utils/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* External dependencies
*/
const { existsSync } = require( 'fs' );
const path = require( 'path' );

/**
* Internal dependencies
*/
const { getPackagePath } = require( './package' );

const fromProjectRoot = ( fileName ) =>
path.join( path.dirname( getPackagePath() ), fileName );

const hasProjectFile = ( fileName ) =>
existsSync( fromProjectRoot( fileName ) );

const fromConfigRoot = ( fileName ) =>
path.join( path.dirname( __dirname ), 'config', fileName );

const fromScriptsRoot = ( scriptName ) =>
path.join( path.dirname( __dirname ), 'scripts', `${ scriptName }.js` );

const hasScriptFile = ( scriptName ) =>
existsSync( fromScriptsRoot( scriptName ) );

module.exports = {
fromConfigRoot,
fromScriptsRoot,
hasProjectFile,
hasScriptFile,
};
Loading

0 comments on commit 3d31dce

Please sign in to comment.