From 96d5453f62bd317353d1d19364d9e659ccc5aa40 Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Sun, 25 Feb 2018 07:13:46 -0500 Subject: [PATCH] Update `jest-editor-support` `Settings` to use spawn in shell option (#5658) * Fix TypeScript error; Spawn using shell option * Add to CHANGELOG * Fix Flow types * Fix prettylint * Fix "Unexpected token ..." --- CHANGELOG.md | 2 + packages/jest-editor-support/index.d.ts | 2 +- packages/jest-editor-support/src/Settings.js | 13 +++++-- .../src/__tests__/settings.test.js | 37 ++++++++++++++++++- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef70b719a8d8..e3b6352a72a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ * `[jest-resolve]` Update node module resolution algorithm to correctly handle symlinked paths ([#5085](https://github.com/facebook/jest/pull/5085)) +* `[jest-editor-support]` Update `Settings` to use spawn in shell option + ([#5658](https://github.com/facebook/jest/pull/5658)) ## 22.4.2 diff --git a/packages/jest-editor-support/index.d.ts b/packages/jest-editor-support/index.d.ts index ce9b8d6d9cb4..3382c4b14262 100644 --- a/packages/jest-editor-support/index.d.ts +++ b/packages/jest-editor-support/index.d.ts @@ -170,7 +170,7 @@ export interface SnapshotMetadata { exists: boolean; name: string; node: { - loc: editor.Node + loc: Node }; content?: string; } diff --git a/packages/jest-editor-support/src/Settings.js b/packages/jest-editor-support/src/Settings.js index 9ad13b186346..7091a33f41b6 100644 --- a/packages/jest-editor-support/src/Settings.js +++ b/packages/jest-editor-support/src/Settings.js @@ -7,7 +7,7 @@ * @flow */ -import type {Options} from './types'; +import type {Options, SpawnOptions} from './types'; import {ChildProcess} from 'child_process'; import EventEmitter from 'events'; @@ -40,15 +40,18 @@ export default class Settings extends EventEmitter { _createProcess: ( workspace: ProjectWorkspace, args: Array, + options: SpawnOptions, ) => ChildProcess; configs: ConfigRepresentations; settings: ConfigRepresentation; workspace: ProjectWorkspace; + spawnOptions: SpawnOptions; constructor(workspace: ProjectWorkspace, options?: Options) { super(); this.workspace = workspace; this._createProcess = (options && options.createProcess) || createProcess; + this.spawnOptions = {shell: options && options.shell}; // Defaults for a Jest project this.settings = { @@ -60,9 +63,11 @@ export default class Settings extends EventEmitter { } getConfigs(completed: any) { - this.getConfigProcess = this._createProcess(this.workspace, [ - '--showConfig', - ]); + this.getConfigProcess = this._createProcess( + this.workspace, + ['--showConfig'], + this.spawnOptions, + ); this.getConfigProcess.stdout.on('data', (data: Buffer) => { const settings = JSON.parse(data.toString()); diff --git a/packages/jest-editor-support/src/__tests__/settings.test.js b/packages/jest-editor-support/src/__tests__/settings.test.js index 55debc02f7c1..fa471810dcd3 100644 --- a/packages/jest-editor-support/src/__tests__/settings.test.js +++ b/packages/jest-editor-support/src/__tests__/settings.test.js @@ -21,9 +21,11 @@ describe('Settings', () => { 'test', 1000, ); - const settings = new Settings(workspace); + const options = {shell: true}; + const settings = new Settings(workspace, options); expect(settings.workspace).toEqual(workspace); expect(settings.settings).toEqual(expect.any(Object)); + expect(settings.spawnOptions).toEqual(options); }); it('[jest 20] reads and parses the config', () => { @@ -132,6 +134,39 @@ describe('Settings', () => { expect(completed).toHaveBeenCalled(); }); + + it('passes command, args, and options to createProcess', () => { + const localJestMajorVersion = 1000; + const pathToConfig = 'test'; + const pathToJest = 'path_to_jest'; + const rootPath = 'root_path'; + + const workspace = new ProjectWorkspace( + rootPath, + pathToJest, + pathToConfig, + localJestMajorVersion, + ); + const createProcess = jest.fn().mockReturnValue({ + on: () => {}, + stdout: new EventEmitter(), + }); + const spawnOptions = {shell: true}; + const options: any = Object.assign({}, createProcess, spawnOptions); + const settings = new Settings(workspace, options); + settings.getConfig(() => {}); + + expect(createProcess).toBeCalledWith( + { + localJestMajorVersion, + pathToConfig, + pathToJest, + rootPath, + }, + ['--showConfig'], + spawnOptions, + ); + }); }); const makeBuffer = (content: string) => {