Skip to content

Commit

Permalink
--showConfig support jest20 and jest21 (#4575)
Browse files Browse the repository at this point in the history
* --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
  • Loading branch information
recca0120 authored and cpojer committed Oct 1, 2017
1 parent 6e5d149 commit eff7a1c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
20 changes: 11 additions & 9 deletions packages/jest-editor-support/src/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ type ConfigRepresentation = {
testMatch: Array<Glob>,
};

type ConfigRepresentations = Array<ConfigRepresentation>;

export default class Settings extends EventEmitter {
getConfigProcess: ChildProcess;
jestVersionMajor: number | null;
_createProcess: (
workspace: ProjectWorkspace,
args: Array<string>,
) => ChildProcess;
configs: ConfigRepresentations;
settings: ConfigRepresentation;
workspace: ProjectWorkspace;

Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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();
});
}
Expand Down
45 changes: 37 additions & 8 deletions packages/jest-editor-support/src/__tests__/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ 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',
'test',
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',
};

Expand All @@ -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',
Expand All @@ -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();
Expand All @@ -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',
Expand Down

0 comments on commit eff7a1c

Please sign in to comment.