2020-04-02 13:41:06 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const { spawnSync } = require( 'child_process' );
|
|
|
|
const program = require( 'commander' );
|
|
|
|
const path = require( 'path' );
|
|
|
|
const fs = require( 'fs' );
|
2021-11-26 19:14:49 +00:00
|
|
|
const {
|
|
|
|
getAppRoot,
|
|
|
|
resolveLocalE2ePath,
|
|
|
|
resolvePackagePath,
|
|
|
|
} = require( '../utils' );
|
2021-10-28 19:32:31 +00:00
|
|
|
const {
|
|
|
|
WC_E2E_SCREENSHOTS,
|
|
|
|
JEST_PUPPETEER_CONFIG,
|
|
|
|
DEFAULT_TIMEOUT_OVERRIDE,
|
|
|
|
} = process.env;
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
program
|
|
|
|
.usage( '<file ...> [options]' )
|
|
|
|
.option( '--dev', 'Development mode' )
|
2020-10-09 08:00:03 +00:00
|
|
|
.option( '--debug', 'Debug mode' )
|
2020-04-02 13:41:06 +00:00
|
|
|
.parse( process.argv );
|
|
|
|
|
2020-07-29 18:13:22 +00:00
|
|
|
const appPath = getAppRoot();
|
2020-04-15 17:07:32 +00:00
|
|
|
|
2021-03-08 19:07:29 +00:00
|
|
|
// clear the screenshots folder before running tests.
|
2021-03-08 20:56:47 +00:00
|
|
|
if ( WC_E2E_SCREENSHOTS ) {
|
2021-11-17 13:50:40 +00:00
|
|
|
const screenshotPath = resolveLocalE2ePath( 'screenshots' );
|
2021-10-28 19:32:31 +00:00
|
|
|
if ( fs.existsSync( screenshotPath ) ) {
|
|
|
|
fs.readdirSync( screenshotPath ).forEach( ( file, index ) => {
|
|
|
|
const filename = path.join( screenshotPath, file );
|
|
|
|
fs.unlinkSync( filename );
|
|
|
|
} );
|
2021-03-08 20:56:47 +00:00
|
|
|
}
|
2021-03-08 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 19:14:49 +00:00
|
|
|
const nodeConfigDirs = [ resolvePackagePath( 'config' ) ];
|
2020-04-15 17:07:32 +00:00
|
|
|
|
|
|
|
if ( appPath ) {
|
2021-11-17 13:50:40 +00:00
|
|
|
nodeConfigDirs.unshift( resolveLocalE2ePath( 'config' ) );
|
2020-04-15 17:07:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 19:32:31 +00:00
|
|
|
const testEnvVars = {
|
2020-04-29 14:22:11 +00:00
|
|
|
NODE_ENV: 'test-e2e',
|
2020-04-15 17:07:32 +00:00
|
|
|
NODE_CONFIG_DIR: nodeConfigDirs.join( ':' ),
|
2020-05-04 17:37:25 +00:00
|
|
|
node_config_dev: program.dev ? 'yes' : 'no',
|
2020-05-05 16:56:00 +00:00
|
|
|
jest_test_timeout: program.dev ? 120000 : 30000,
|
2020-04-02 13:41:06 +00:00
|
|
|
};
|
2020-07-29 18:13:22 +00:00
|
|
|
|
2021-08-12 22:05:55 +00:00
|
|
|
if ( DEFAULT_TIMEOUT_OVERRIDE ) {
|
|
|
|
testEnvVars.jest_test_timeout = DEFAULT_TIMEOUT_OVERRIDE;
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:23:32 +00:00
|
|
|
if ( ! JEST_PUPPETEER_CONFIG ) {
|
2020-07-29 18:13:22 +00:00
|
|
|
// Use local Puppeteer config if there is one.
|
|
|
|
// Load test configuration file into an object.
|
2021-11-17 13:50:40 +00:00
|
|
|
const localJestConfigFile = resolveLocalE2ePath( 'config/jest-puppeteer.config.js' );
|
2021-11-26 19:14:49 +00:00
|
|
|
const jestConfigFile = resolvePackagePath( 'config/jest-puppeteer.config.js' );
|
2020-07-29 18:13:22 +00:00
|
|
|
|
2021-10-28 19:32:31 +00:00
|
|
|
testEnvVars.JEST_PUPPETEER_CONFIG = fs.existsSync( localJestConfigFile )
|
|
|
|
? localJestConfigFile
|
|
|
|
: jestConfigFile;
|
2020-07-28 19:23:32 +00:00
|
|
|
}
|
2020-04-02 13:41:06 +00:00
|
|
|
|
2020-10-01 18:56:02 +00:00
|
|
|
// Check for running a specific script
|
|
|
|
if ( program.args.length == 1 ) {
|
|
|
|
// Check for both absolute and relative paths
|
2021-10-28 19:32:31 +00:00
|
|
|
const testSpecAbs = path.resolve( program.args[ 0 ] );
|
|
|
|
const testSpecRel = path.resolve( appPath, program.args[ 0 ] );
|
2020-10-01 18:56:02 +00:00
|
|
|
if ( fs.existsSync( testSpecAbs ) ) {
|
|
|
|
process.env.jest_test_spec = testSpecAbs;
|
|
|
|
} else if ( fs.existsSync( testSpecRel ) ) {
|
2021-10-28 19:32:31 +00:00
|
|
|
process.env.jest_test_spec = testSpecRel;
|
2020-10-01 18:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:41:06 +00:00
|
|
|
let jestCommand = 'jest';
|
|
|
|
const jestArgs = [
|
|
|
|
'--maxWorkers=1',
|
|
|
|
'--rootDir=./',
|
|
|
|
'--verbose',
|
|
|
|
...program.args,
|
|
|
|
];
|
|
|
|
|
2020-10-09 08:00:03 +00:00
|
|
|
if ( program.debug ) {
|
2020-04-02 13:41:06 +00:00
|
|
|
jestCommand = 'npx';
|
|
|
|
jestArgs.unshift( 'ndb', 'jest' );
|
|
|
|
}
|
|
|
|
|
|
|
|
const envVars = Object.assign( {}, process.env, testEnvVars );
|
|
|
|
|
2021-11-26 19:14:49 +00:00
|
|
|
let configPath = resolvePackagePath( 'config/jest.config.js' );
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
// Look for a Jest config in the dependent app's path.
|
|
|
|
if ( appPath ) {
|
2021-11-17 13:50:40 +00:00
|
|
|
const appConfig = resolveLocalE2ePath( 'config/jest.config.js' );
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
if ( fs.existsSync( appConfig ) ) {
|
|
|
|
configPath = appConfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jestArgs.push( '--config=' + configPath );
|
|
|
|
|
2021-10-28 19:32:31 +00:00
|
|
|
const jestProcess = spawnSync( jestCommand, jestArgs, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: envVars,
|
|
|
|
} );
|
2020-04-02 13:41:06 +00:00
|
|
|
|
|
|
|
console.log( 'Jest exit code: ' + jestProcess.status );
|
|
|
|
|
|
|
|
// Pass Jest exit code to npm
|
|
|
|
process.exit( jestProcess.status );
|