@@ -2,7 +2,7 @@ import { platform } from 'os'
22import { existsSync , readFileSync } from 'fs'
33import { normalize , join } from 'path'
44
5- import { IPluginSettings } from './IPluginSettings '
5+ import { IPluginSettings , hasUserSetPathToJest } from './Settings '
66
77/**
88 * Known binary names of `react-scripts` forks
@@ -11,7 +11,7 @@ const createReactAppBinaryNames = ['react-scripts', 'react-native-scripts', 'rea
1111
1212/**
1313 * Tries to read the test command from the scripts section within `package.json`
14- *
14+ *
1515 * Returns the test command in case of success,
1616 * `undefined` if there was an exception while reading and parsing `package.json`
1717 * `null` if there is no test script
@@ -29,24 +29,24 @@ export function getTestCommand(rootPath: string): string | undefined | null {
2929 }
3030}
3131
32- /**
32+ /**
3333 * Checks if the supplied test command could have been generated by create-react-app
3434*/
35- export function isCRATestCommand ( testCommand : string ) : boolean {
35+ export function isCreateReactAppTestCommand ( testCommand : string ) : boolean {
3636 return testCommand && createReactAppBinaryNames . some ( binary => testCommand . indexOf ( binary + ' test' ) === 0 )
3737}
3838
3939/**
4040 * Checks if the project in `rootPath` was bootstrapped by `create-react-app`.
4141 */
42- function isBootstrappedWithCRA ( rootPath : string ) : boolean {
42+ function isBootstrappedWithCreateReactApp ( rootPath : string ) : boolean {
4343 const testCommand = getTestCommand ( rootPath )
4444 if ( testCommand === undefined ) {
4545 // In case parsing `package.json` failed or was unconclusive,
4646 // fallback to checking for the presence of the binaries in `./node_modules/.bin`
4747 return createReactAppBinaryNames . some ( binary => hasNodeExecutable ( rootPath , binary ) )
4848 }
49- return isCRATestCommand ( testCommand )
49+ return isCreateReactAppTestCommand ( testCommand )
5050}
5151
5252function hasNodeExecutable ( rootPath : string , executable : string ) : boolean {
@@ -56,29 +56,32 @@ function hasNodeExecutable(rootPath: string, executable: string): boolean {
5656}
5757
5858/**
59- * Handles getting the jest runner, handling the OS and project specific work too
59+ * Handles getting the jest runner, handling the OS and project specific work too
6060 *
6161 * @returns {string }
6262 */
63- export function pathToJest ( pluginSettings : IPluginSettings ) {
64- if ( pluginSettings . pathToJest ) {
65- if ( isBootstrappedWithCRA ( pluginSettings . rootPath ) ) {
66- return 'npm test --'
67- }
68- return normalize ( pluginSettings . pathToJest )
63+ export function pathToJest ( { pathToJest, rootPath } : IPluginSettings ) {
64+ if ( hasUserSetPathToJest ( pathToJest ) ) {
65+ return normalize ( pathToJest )
6966 }
7067
71- const platform = process . platform
72- if ( platform === 'win32' && existsSync ( join ( pluginSettings . rootPath , 'node_modules' , '.bin' , 'jest.cmd' ) ) ) {
73- return normalize ( join ( pluginSettings . rootPath , 'node_modules' , '.bin' , 'jest.cmd' ) )
74- } else if (
75- ( platform === 'linux' || platform === 'darwin' ) &&
76- existsSync ( join ( pluginSettings . rootPath , 'node_modules' , '.bin' , 'jest' ) )
77- ) {
78- return normalize ( join ( pluginSettings . rootPath , 'node_modules' , '.bin' , 'jest' ) )
68+ if ( isBootstrappedWithCreateReactApp ( rootPath ) ) {
69+ return 'npm test --'
7970 }
8071
81- return 'jest'
72+ const localJestExecutable = pathToLocalJestExecutable ( rootPath )
73+ if ( existsSync ( localJestExecutable ) ) {
74+ return localJestExecutable
75+ }
76+ return `jest${ isWindows ( ) ? '.cmd' : '' } `
77+ }
78+
79+ function pathToLocalJestExecutable ( rootDir ) {
80+ return normalize ( join ( rootDir , `node_modules/.bin/jest${ isWindows ( ) ? '.cmd' : '' } ` ) )
81+ }
82+
83+ function isWindows ( ) {
84+ return platform ( ) === 'win32'
8285}
8386
8487/**
0 commit comments