Skip to content

Commit

Permalink
Add detection for the react-scripts executable
Browse files Browse the repository at this point in the history
When a project is bootstrapped with create-react-app using the latest
versions of node/npm, the react-scripts executable is installed in the
top level node_modules/.bin directory. This has been added as the first
path to check.

This fixes jest-community#161, closes jest-community#39, closes jest-community#113 and resolves facebook/create-react-app#2709, and closes jest-community#125.
  • Loading branch information
seanpoulter committed Nov 4, 2017
1 parent ce7ee98 commit a9d61df
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ export function pathToJest(pluginSettings: IPluginSettings) {
const path = normalize(pluginSettings.pathToJest)

const defaultPath = normalize('node_modules/.bin/jest')
if (path === defaultPath) {
const defaultCreateReactPath = 'node_modules/react-scripts/node_modules/.bin/jest'
const defaultCreateReactPathWindows = 'node_modules/react-scripts/node_modules/.bin/jest.cmd'
const createReactPath = platform() === 'win32' ? defaultCreateReactPathWindows : defaultCreateReactPath
const absolutePath = join(pluginSettings.rootPath, createReactPath)

const craExists = existsSync(absolutePath)
if (craExists) {
// If it's the default, run the script instead
return platform() === 'win32' ? 'npm.cmd test --' : 'npm test --'
}
if (path === defaultPath && isBootstrappedWithCreateReactApp(pluginSettings.rootPath)) {
// If it's the default, run the script instead
return platform() === 'win32' ? 'npm.cmd test --' : 'npm test --'
}

// For windows support, see https://github.com/orta/vscode-jest/issues/10
Expand All @@ -33,6 +25,19 @@ export function pathToJest(pluginSettings: IPluginSettings) {
return path
}

function isBootstrappedWithCreateReactApp(rootPath: string): boolean {
return (
hasExecutable(rootPath, 'node_modules/.bin/react-scripts') ||
hasExecutable(rootPath, 'node_modules/react-scripts/node_modules/.bin/jest')
)
}

function hasExecutable(rootPath: string, executablePath: string): boolean {
const ext = platform() === 'win32' ? '.cmd' : ''
const absolutePath = join(rootPath, executablePath + ext)
return existsSync(absolutePath)
}

/**
* Handles getting the path to config file
*
Expand Down

0 comments on commit a9d61df

Please sign in to comment.