diff --git a/packages/jest-editor-support/src/Process.js b/packages/jest-editor-support/src/Process.js index 541bf1a23a35..ca34b15245f5 100644 --- a/packages/jest-editor-support/src/Process.js +++ b/packages/jest-editor-support/src/Process.js @@ -19,7 +19,7 @@ const ProjectWorkspace = require('./ProjectWorkspace'); * @param {string[]} args * @returns {ChildProcess} */ -module.exports.jestChildProcessWithArgs = ( +module.exports.createProcess = ( workspace: ProjectWorkspace, args: Array, ): ChildProcess => { diff --git a/packages/jest-editor-support/src/ProjectWorkspace.js b/packages/jest-editor-support/src/ProjectWorkspace.js index 929a2cc96c3c..884436da7ba7 100644 --- a/packages/jest-editor-support/src/ProjectWorkspace.js +++ b/packages/jest-editor-support/src/ProjectWorkspace.js @@ -37,7 +37,7 @@ module.exports = class ProjectWorkspace { /** * Path to a local Jest config file. - * + * * @type {string} */ pathToConfig: string; @@ -45,14 +45,14 @@ module.exports = class ProjectWorkspace { /** * 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, ) { diff --git a/packages/jest-editor-support/src/Runner.js b/packages/jest-editor-support/src/Runner.js index 9e86d64b1365..3800e601ac75 100644 --- a/packages/jest-editor-support/src/Runner.js +++ b/packages/jest-editor-support/src/Runner.js @@ -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, + ) => 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'; } @@ -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. @@ -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(); }); diff --git a/packages/jest-editor-support/src/Settings.js b/packages/jest-editor-support/src/Settings.js index cf4089b0d0d1..37cd3b3c1d31 100644 --- a/packages/jest-editor-support/src/Settings.js +++ b/packages/jest-editor-support/src/Settings.js @@ -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 @@ -33,17 +33,22 @@ type ConfigRepresentation = { testRegex: string, testMatch: Array } +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, + ) => 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 = { @@ -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 =') diff --git a/packages/jest-editor-support/src/__tests__/Runner-test.js b/packages/jest-editor-support/src/__tests__/Runner-test.js index a601fce410f1..d1fe22cb2329 100644 --- a/packages/jest-editor-support/src/__tests__/Runner-test.js +++ b/packages/jest-editor-support/src/__tests__/Runner-test.js @@ -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, }; }); @@ -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(); @@ -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); }); }); diff --git a/packages/jest-editor-support/src/__tests__/parsers/parserTests.js b/packages/jest-editor-support/src/__tests__/parsers/parserTests.js index d554db6fe827..5a726d5fc190 100644 --- a/packages/jest-editor-support/src/__tests__/parsers/parserTests.js +++ b/packages/jest-editor-support/src/__tests__/parsers/parserTests.js @@ -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)', () => { diff --git a/packages/jest-editor-support/src/parsers/BabylonParser.js b/packages/jest-editor-support/src/parsers/BabylonParser.js index 9d934e641b6e..b709b690d80e 100644 --- a/packages/jest-editor-support/src/parsers/BabylonParser.js +++ b/packages/jest-editor-support/src/parsers/BabylonParser.js @@ -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); diff --git a/packages/jest-editor-support/src/types.js b/packages/jest-editor-support/src/types.js index f44b41e24e40..5baafd44596b 100644 --- a/packages/jest-editor-support/src/types.js +++ b/packages/jest-editor-support/src/types.js @@ -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, + ) => ChildProcess +} + export type JestFileResults = { name: string, summary: string, message: string, status: "failed" | "passed", - startTime:number, - endTime:number, + startTime: number, + endTime: number, assertionResults: Array, } @@ -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, }