From eff7a1cfbdffa2111deaa020378c9395385a72a0 Mon Sep 17 00:00:00 2001 From: Recca Tsai Date: Sun, 1 Oct 2017 19:25:21 +0800 Subject: [PATCH] --showConfig support jest20 and jest21 (#4575) * --showConfig to show all project configs #4078 * return configs * new method getConfigs * new method getConfigs * call completed * eslint * support jest 20 * lint * version * getConfig for jest21 * lint * fix jest20 bug * fix bug * lint * add type ConfigRepresentations * remove ConfigRepresentations * update --- packages/jest-editor-support/src/Settings.js | 20 +++++---- .../src/__tests__/settings.test.js | 45 +++++++++++++++---- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/packages/jest-editor-support/src/Settings.js b/packages/jest-editor-support/src/Settings.js index 36f93e98d283..9ad13b186346 100644 --- a/packages/jest-editor-support/src/Settings.js +++ b/packages/jest-editor-support/src/Settings.js @@ -32,6 +32,8 @@ type ConfigRepresentation = { testMatch: Array, }; +type ConfigRepresentations = Array; + export default class Settings extends EventEmitter { getConfigProcess: ChildProcess; jestVersionMajor: number | null; @@ -39,6 +41,7 @@ export default class Settings extends EventEmitter { workspace: ProjectWorkspace, args: Array, ) => ChildProcess; + configs: ConfigRepresentations; settings: ConfigRepresentation; workspace: ProjectWorkspace; @@ -52,6 +55,8 @@ export default class Settings extends EventEmitter { testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'], testRegex: '(/__tests__/.*|\\.(test|spec))\\.jsx?$', }; + + this.configs = [this.settings]; } getConfigs(completed: any) { @@ -60,13 +65,10 @@ export default class Settings extends EventEmitter { ]); this.getConfigProcess.stdout.on('data', (data: Buffer) => { - const {configs, version} = JSON.parse(data.toString()); - // We can give warnings to versions under 17 now - // See https://github.com/facebook/jest/issues/2343 for moving this into - // the config object - - this.jestVersionMajor = parseInt(version.split('.').shift(), 10); - this.settings = configs; + const settings = JSON.parse(data.toString()); + this.jestVersionMajor = parseInt(settings.version.split('.').shift(), 10); + this.configs = + this.jestVersionMajor >= 21 ? settings.configs : [settings.config]; }); // They could have an older build of Jest which @@ -76,9 +78,9 @@ export default class Settings extends EventEmitter { }); } - getConfig(completed: any) { + getConfig(completed: any, index: number = 0) { this.getConfigs(() => { - this.settings = this.settings[0]; + this.settings = this.configs[index]; completed(); }); } diff --git a/packages/jest-editor-support/src/__tests__/settings.test.js b/packages/jest-editor-support/src/__tests__/settings.test.js index 90ad492eea94..55debc02f7c1 100644 --- a/packages/jest-editor-support/src/__tests__/settings.test.js +++ b/packages/jest-editor-support/src/__tests__/settings.test.js @@ -26,7 +26,7 @@ describe('Settings', () => { expect(settings.settings).toEqual(expect.any(Object)); }); - it('reads and parses the configs', () => { + it('[jest 20] reads and parses the config', () => { const workspace = new ProjectWorkspace( 'root_path', 'path_to_jest', @@ -34,9 +34,9 @@ describe('Settings', () => { 1000, ); const completed = jest.fn(); - const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}]; + const config = {cacheDirectory: '/tmp/jest', name: '[md5 hash]'}; const json = { - configs, + config, version: '19.0.0', }; @@ -46,16 +46,16 @@ describe('Settings', () => { const buffer = makeBuffer(JSON.stringify(json)); const settings = new Settings(workspace, {createProcess}); - settings.getConfigs(completed); + settings.getConfig(completed); settings.getConfigProcess.stdout.emit('data', buffer); settings.getConfigProcess.emit('close'); expect(completed).toHaveBeenCalled(); expect(settings.jestVersionMajor).toBe(19); - expect(settings.settings).toEqual(configs); + expect(settings.settings).toEqual(config); }); - it('reads and parses the config', () => { + it('[jest 21] reads and parses the config', () => { const workspace = new ProjectWorkspace( 'root_path', 'path_to_jest', @@ -66,7 +66,7 @@ describe('Settings', () => { const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}]; const json = { configs, - version: '19.0.0', + version: '21.0.0', }; const mockProcess: any = new EventEmitter(); @@ -80,10 +80,39 @@ describe('Settings', () => { settings.getConfigProcess.emit('close'); expect(completed).toHaveBeenCalled(); - expect(settings.jestVersionMajor).toBe(19); + expect(settings.jestVersionMajor).toBe(21); expect(settings.settings).toEqual(configs[0]); }); + it('[jest 21] reads and parses the configs', () => { + const workspace = new ProjectWorkspace( + 'root_path', + 'path_to_jest', + 'test', + 1000, + ); + const completed = jest.fn(); + const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}]; + const json = { + configs, + version: '21.0.0', + }; + + const mockProcess: any = new EventEmitter(); + mockProcess.stdout = new EventEmitter(); + const createProcess = () => mockProcess; + const buffer = makeBuffer(JSON.stringify(json)); + const settings = new Settings(workspace, {createProcess}); + + settings.getConfigs(completed); + settings.getConfigProcess.stdout.emit('data', buffer); + settings.getConfigProcess.emit('close'); + + expect(completed).toHaveBeenCalled(); + expect(settings.jestVersionMajor).toBe(21); + expect(settings.configs).toEqual(configs); + }); + it('calls callback even if no data is sent', () => { const workspace = new ProjectWorkspace( 'root_path',