Skip to content

Commit

Permalink
Allow to pass a custom invoker to jest-editor-support Runner/Setting (#…
Browse files Browse the repository at this point in the history
…2817)

* Allow to pass a custom invoker to jest-editor-support Runner/Setting to support spawning jest on a remote server

* Rename invoker to createProcess

* Remove the empty test

* Fixes lint warning on master and on the pr

* rename jestChildProcessWithArgs to createProcess

* Fix Settings as well

* Update Runner-test.js
  • Loading branch information
kentaromiura authored and cpojer committed Feb 7, 2017
1 parent 3a614d6 commit ccd80c2
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/jest-editor-support/src/Process.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ProjectWorkspace = require('./ProjectWorkspace');
* @param {string[]} args
* @returns {ChildProcess}
*/
module.exports.jestChildProcessWithArgs = (
module.exports.createProcess = (
workspace: ProjectWorkspace, args: Array<string>,
): ChildProcess => {

Expand Down
8 changes: 4 additions & 4 deletions packages/jest-editor-support/src/ProjectWorkspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ module.exports = class ProjectWorkspace {

/**
* Path to a local Jest config file.
*
*
* @type {string}
*/
pathToConfig: string;

/**
* local Jest major release version, as the runner could run against
* any version of Jest.
*
*
* @type {number}
*/
localJestMajorVersion: number;

constructor(
rootPath: string,
pathToJest: string,
rootPath: string,
pathToJest: string,
pathToConfig: string,
localJestMajorVersion: number,
) {
Expand Down
17 changes: 12 additions & 5 deletions packages/jest-editor-support/src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ const {readFile} = require('fs');
const {tmpdir} = require('os');
const {EventEmitter} = require('events');
const ProjectWorkspace = require('./ProjectWorkspace');
const {jestChildProcessWithArgs} = require('./Process');
const {createProcess} = require('./Process');

import type {Options} from './types';

// This class represents the running process, and
// passes out events when it understands what data is being
// pass sent out of the process
module.exports = class Runner extends EventEmitter {
debugprocess: ChildProcess;
workspace: ProjectWorkspace;
outputPath: string;
workspace: ProjectWorkspace;
_createProcess: (
workspace: ProjectWorkspace,
args: Array<string>,
) => ChildProcess;

constructor(workspace: ProjectWorkspace) {
constructor(workspace: ProjectWorkspace, options?: Options) {
super();
this._createProcess = (options && options.createProcess) || createProcess;
this.workspace = workspace;
this.outputPath = tmpdir() + '/jest_runner.json';
}
Expand All @@ -47,7 +54,7 @@ module.exports = class Runner extends EventEmitter {
this.outputPath,
];

this.debugprocess = jestChildProcessWithArgs(this.workspace, args);
this.debugprocess = this._createProcess(this.workspace, args);
this.debugprocess.stdout.on('data', (data: Buffer) => {
// Make jest save to a file, otherwise we get chunked data
// and it can be hard to put it back together.
Expand Down Expand Up @@ -85,7 +92,7 @@ module.exports = class Runner extends EventEmitter {

runJestWithUpdateForSnapshots(completion: any) {
const args = ['--updateSnapshot'];
const updateProcess = jestChildProcessWithArgs(this.workspace, args);
const updateProcess = this._createProcess(this.workspace, args);
updateProcess.on('close', () => {
completion();
});
Expand Down
19 changes: 12 additions & 7 deletions packages/jest-editor-support/src/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {ChildProcess} = require('child_process');
const EventEmitter = require('events');
const {EOL} = require('os');
const ProjectWorkspace = require('./ProjectWorkspace');
const {jestChildProcessWithArgs} = require('./Process');
const {createProcess} = require('./Process');

// This class represents the the configuration of Jest's process
// we want to start with the defaults then override whatever they output
Expand All @@ -33,17 +33,22 @@ type ConfigRepresentation = {
testRegex: string,
testMatch: Array<Glob>
}
import type {Options} from './types';

module.exports = class Settings extends EventEmitter {
debugprocess: ChildProcess;
workspace: ProjectWorkspace;

settings: ConfigRepresentation;
jestVersionMajor: number | null;
_createProcess: (
workspace: ProjectWorkspace,
args: Array<string>,
) => ChildProcess;
settings: ConfigRepresentation;
workspace: ProjectWorkspace;

constructor(workspace: ProjectWorkspace) {
constructor(workspace: ProjectWorkspace, options?: Options) {
super();
this.workspace = workspace;
this._createProcess = (options && options.createProcess) || createProcess;

// Defaults for a Jest project
this.settings = {
Expand All @@ -60,12 +65,12 @@ module.exports = class Settings extends EventEmitter {
// in a non-existant folder.
const folderThatDoesntExist = 'hi-there-danger-are-you-following-along';
const args = ['--debug', folderThatDoesntExist];
this.debugprocess = jestChildProcessWithArgs(this.workspace, args);
this.debugprocess = this._createProcess(this.workspace, args);

this.debugprocess.stdout.on('data', (data: Buffer) => {
const string = data.toString();
// We can give warnings to versions under 17 now
// See https://github.com/facebook/jest/issues/2343 for moving this into
// See https://github.com/facebook/jest/issues/2343 for moving this into
// the config object
if (string.includes('jest version =')) {
const version = string.split('jest version =')
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-editor-support/src/__tests__/Runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jest.doMock('fs', () => {

// Let's us use a per-test "jest process"
let mockDebugProcess = {};
const mockJestChildProcessWithArgs = jest.fn(() => mockDebugProcess);
jest.mock('../Process.js', () => {
const mockCreateProcess = jest.fn(() => mockDebugProcess);
jest.doMock('../Process.js', () => {
return {
jestChildProcessWithArgs: mockJestChildProcessWithArgs,
createProcess: mockCreateProcess,
};
});

Expand All @@ -42,7 +42,7 @@ describe('events', () => {
let fakeProcess;

beforeEach(() => {
mockJestChildProcessWithArgs.mockClear();
mockCreateProcess.mockClear();
const workspace = new ProjectWorkspace('.', 'node_modules/.bin/jest', 18);
runner = new Runner(workspace);
fakeProcess = new EventEmitter();
Expand Down Expand Up @@ -87,12 +87,12 @@ describe('events', () => {

it('should only start one jest process at a time', () => {
runner.start();
expect(mockJestChildProcessWithArgs).toHaveBeenCalledTimes(1);
expect(mockCreateProcess).toHaveBeenCalledTimes(1);
});

it('should start jest process after killing the old process', () => {
runner.closeProcess();
runner.start();
expect(mockJestChildProcessWithArgs).toHaveBeenCalledTimes(2);
expect(mockCreateProcess).toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ function parserTests(parse: (file: string) => ParserReturn) {
expect(sixthIt.end).toEqual({column: 5, line: 25});
});

// These tests act more like linters that we don't raise on
// non-trivial files taken from some Artsy codebases,
// These tests act more like linters that we don't raise on
// non-trivial files taken from some Artsy codebases,
// which are MIT licensed.

it('For a danger test file (which has flow annotations)', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-editor-support/src/parsers/BabylonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const babylonParser = (file: string) => {
const plugins = Array.isArray(babel.plugins)
? babel.plugins.concat(['flow'])
: ['flow'];

const config = {plugins, sourceType: 'module'};
const ast = babylon.parse(data, config);

Expand Down
30 changes: 20 additions & 10 deletions packages/jest-editor-support/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ export type Location = {
line: number,
}

import type ProjectWorkspace from './ProjectWorkspace';
import type {ChildProcess} from 'child_process';

export type Options = {
createProcess?: (
workspace: ProjectWorkspace,
args: Array<string>,
) => ChildProcess
}

export type JestFileResults = {
name: string,
summary: string,
message: string,
status: "failed" | "passed",
startTime:number,
endTime:number,
startTime: number,
endTime: number,
assertionResults: Array<JestAssertionResults>,
}

Expand All @@ -33,14 +43,14 @@ export type JestAssertionResults = {
}

export type JestTotalResults = {
success:boolean,
startTime:number,
numTotalTests:number,
numTotalTestSuites:number,
numRuntimeErrorTestSuites:number,
numPassedTests:number,
numFailedTests:number,
numPendingTests:number,
success: boolean,
startTime: number,
numTotalTests: number,
numTotalTestSuites: number,
numRuntimeErrorTestSuites: number,
numPassedTests: number,
numFailedTests: number,
numPendingTests: number,
testResults: Array<JestFileResults>,
}

Expand Down

0 comments on commit ccd80c2

Please sign in to comment.