This repository has been archived by the owner on Jul 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: Provide the default configuration for the
test
command (#83)
* Scripts: Provide the default configuration for the `test` command It is used in the case when the project does not have a config for Jest or Babel * Scripts: Add tests for utils to ensure they work properly * Add back end-of-file new line
- Loading branch information
Showing
14 changed files
with
344 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
{ | ||
"lerna": "2.9.0", | ||
"commands": { | ||
"publish": { | ||
"ignore": [ | ||
"**/test/*.js" | ||
] | ||
} | ||
}, | ||
"packages": [ | ||
"packages/*" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
const spawn = require( 'cross-spawn' ); | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getCliArgs, spawnScript } = require( '../utils' ); | ||
|
||
const allowedScripts = [ 'test' ]; | ||
const [ scriptName, ...nodeArgs ] = process.argv.slice( 2 ); | ||
const [ scriptName, ...nodesArgs ] = getCliArgs(); | ||
|
||
if ( allowedScripts.indexOf( scriptName ) === -1 ) { | ||
console.log( 'Unknown script "' + scriptName + '".' ); | ||
console.log( 'Perhaps you need to update @wordpress/scripts?' ); | ||
process.exit( 1 ); | ||
} | ||
|
||
const result = spawn.sync( | ||
'node', | ||
[ | ||
require.resolve( `../scripts/${ scriptName }-script` ), | ||
...nodeArgs | ||
], | ||
{ stdio: 'inherit' } | ||
); | ||
if ( result.signal ) { | ||
if ( result.signal === 'SIGKILL' ) { | ||
console.log( | ||
'The build 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 ( result.signal === 'SIGTERM' ) { | ||
console.log( | ||
'The build failed because the process exited too early. ' + | ||
'Someone might have called `kill` or `killall`, or the system could ' + | ||
'be shutting down.' | ||
); | ||
} | ||
process.exit( 1 ); | ||
} | ||
process.exit( result.status ); | ||
spawnScript( scriptName, nodesArgs ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const babelJest = require( 'babel-jest' ); | ||
|
||
module.exports = babelJest.createTransformer( { | ||
presets: [ '@wordpress/default' ], | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { | ||
hasProjectFile, | ||
hasPackageProp, | ||
} = require( '../utils' ); | ||
|
||
const jestConfig = { | ||
preset: '@wordpress/jest-preset-default' | ||
}; | ||
|
||
const hasBabelConfig = hasProjectFile( '.babelrc' ) || | ||
hasPackageProp( 'babel' ); | ||
|
||
if ( ! hasBabelConfig ) { | ||
jestConfig.transform = { | ||
'^.+\\.js$': path.join( __dirname, 'babel-transform' ), | ||
}; | ||
} | ||
|
||
module.exports = jestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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 { | ||
getCliArgs, | ||
hasCliArg, | ||
hasProjectFile, | ||
hasPackageProp, | ||
} = 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' ) ) ] | ||
: []; | ||
|
||
jest.run( [ ...config, ...args ] ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require( './test-unit-jest' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const spawn = require( 'cross-spawn' ); | ||
const { existsSync } = require( 'fs' ); | ||
const path = require( 'path' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getPackagePath, hasPackageProp } = require( './package' ); | ||
const { exit, getCliArgs } = require( './process' ); | ||
|
||
const first = list => list[ 0 ]; | ||
|
||
const hasCliArg = ( arg ) => getCliArgs() | ||
.some( ( value ) => first( value.split( '=' ) ) === arg ); | ||
|
||
const fromProjectRoot = ( fileName ) => | ||
path.join( path.dirname( getPackagePath() ), fileName ); | ||
|
||
const hasProjectFile = ( fileName ) => | ||
existsSync( fromProjectRoot( fileName ) ); | ||
|
||
const fromScriptsRoot = ( scriptName ) => | ||
path.join( path.dirname( __dirname ), 'scripts', `${ scriptName }.js` ); | ||
|
||
const hasScriptFile = ( scriptName ) => | ||
existsSync( fromScriptsRoot( scriptName ) ); | ||
|
||
const handleSignal = ( signal ) => { | ||
if ( signal === 'SIGKILL' ) { | ||
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' ) { | ||
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 ) { | ||
console.log( 'Script name is missing.' ); | ||
exit( 1 ); | ||
} | ||
|
||
if ( ! hasScriptFile( scriptName ) ) { | ||
console.log( 'Unknown script "' + scriptName + '".' ); | ||
console.log( '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 = { | ||
getCliArgs, | ||
hasCliArg, | ||
hasProjectFile, | ||
hasPackageProp, | ||
spawnScript, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { realpathSync } = require( 'fs' ); | ||
const readPkgUp = require( 'read-pkg-up' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getCurrentWorkingDirectory } = require( './process' ); | ||
|
||
const { pkg, path: pkgPath } = readPkgUp.sync( { | ||
cwd: realpathSync( getCurrentWorkingDirectory() ), | ||
} ); | ||
|
||
const getPackagePath = () => pkgPath; | ||
|
||
const hasPackageProp = ( prop ) => pkg && pkg.hasOwnProperty( prop ); | ||
|
||
module.exports = { | ||
getPackagePath, | ||
hasPackageProp, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const getCliArgs = () => process.argv.slice( 2 ); | ||
|
||
module.exports = { | ||
exit: process.exit, | ||
getCliArgs, | ||
getCurrentWorkingDirectory: process.cwd, | ||
}; |
Oops, something went wrong.