This repository was 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
It is used in the case when the project does not have a config for Jest or Babel
- Loading branch information
Showing
12 changed files
with
242 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.8.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,81 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const spawn = require( 'cross-spawn' ); | ||
const { existsSync, realpathSync } = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const readPkgUp = require( 'read-pkg-up' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { exit, getCliArgs, getCurrentWorkingDirectory } = require( './process' ); | ||
|
||
const first = list => list[ 0 ]; | ||
|
||
const hasCliArg = ( arg ) => getCliArgs() | ||
.some( ( value ) => first( value.split( '=' ) ) === arg ); | ||
|
||
const { pkg, path: pkgPath } = readPkgUp.sync( { | ||
cwd: realpathSync( getCurrentWorkingDirectory() ), | ||
} ); | ||
|
||
const appDirectory = path.dirname( pkgPath ); | ||
|
||
const fromProjectRoot = ( fileName ) => path.join( appDirectory, 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 hasPackageProp = ( prop ) => pkg && pkg.hasOwnProperty( prop ); | ||
|
||
const handleSignal = ( signal ) => { | ||
if ( 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 ( 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.' | ||
); | ||
} | ||
exit( 1 ); | ||
}; | ||
|
||
const spawnScript = ( scriptName, args ) => { | ||
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,7 @@ | ||
const getCliArgs = () => process.argv.slice( 2 ); | ||
|
||
module.exports = { | ||
exit: process.exit, | ||
getCliArgs, | ||
getCurrentWorkingDirectory: process.cwd, | ||
}; |
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,54 @@ | ||
jest.mock( '../process', () => { | ||
const process = require.requireActual( '../process' ); | ||
|
||
return Object.keys( process ).reduce( | ||
( accumulator, methodName ) => ( { | ||
...accumulator, | ||
[ methodName ]: jest.spyOn( process, methodName ), | ||
} ), | ||
{}, | ||
); | ||
} ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
getCliArgs, | ||
hasCliArg, | ||
} from '../'; | ||
import { getCliArgs as getCliArgsMock } from '../process'; | ||
|
||
describe( 'utils', () => { | ||
describe( 'getCliArgs', () => { | ||
test( 'should have function defined', () => { | ||
expect( getCliArgs() ).toBeDefined(); | ||
} ); | ||
} ); | ||
|
||
describe( 'hasCliArg', () => { | ||
beforeAll( () => { | ||
getCliArgsMock.mockReturnValue( [ '-a', '--b', '--config=test' ] ); | ||
} ); | ||
|
||
afterAll( () => { | ||
getCliArgsMock.mockReset(); | ||
} ); | ||
|
||
test( 'should return false when no args passed', () => { | ||
getCliArgsMock.mockReturnValueOnce( [] ); | ||
|
||
expect( hasCliArg( '--no-args' ) ).toBe( false ); | ||
} ); | ||
|
||
test( 'should return false when checking for unrecognized arg', () => { | ||
expect( hasCliArg( '--non-existent' ) ).toBe( false ); | ||
} ); | ||
|
||
test( 'should return true when CLI arg found', () => { | ||
expect( hasCliArg( '-a' ) ).toBe( true ); | ||
expect( hasCliArg( '--b' ) ).toBe( true ); | ||
expect( hasCliArg( '--config' ) ).toBe( true ); | ||
} ); | ||
} ); | ||
} ); |