Skip to content

Commit 65f40cb

Browse files
recca0120tabrindle
authored andcommitted
--showConfig support jest20 and jest21 (jestjs#4575)
* --showConfig to show all project configs jestjs#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
1 parent 4314707 commit 65f40cb

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

packages/jest-editor-support/src/Settings.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ type ConfigRepresentation = {
3232
testMatch: Array<Glob>,
3333
};
3434

35+
type ConfigRepresentations = Array<ConfigRepresentation>;
36+
3537
export default class Settings extends EventEmitter {
3638
getConfigProcess: ChildProcess;
3739
jestVersionMajor: number | null;
3840
_createProcess: (
3941
workspace: ProjectWorkspace,
4042
args: Array<string>,
4143
) => ChildProcess;
44+
configs: ConfigRepresentations;
4245
settings: ConfigRepresentation;
4346
workspace: ProjectWorkspace;
4447

@@ -52,6 +55,8 @@ export default class Settings extends EventEmitter {
5255
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],
5356
testRegex: '(/__tests__/.*|\\.(test|spec))\\.jsx?$',
5457
};
58+
59+
this.configs = [this.settings];
5560
}
5661

5762
getConfigs(completed: any) {
@@ -60,13 +65,10 @@ export default class Settings extends EventEmitter {
6065
]);
6166

6267
this.getConfigProcess.stdout.on('data', (data: Buffer) => {
63-
const {configs, version} = JSON.parse(data.toString());
64-
// We can give warnings to versions under 17 now
65-
// See https://github.com/facebook/jest/issues/2343 for moving this into
66-
// the config object
67-
68-
this.jestVersionMajor = parseInt(version.split('.').shift(), 10);
69-
this.settings = configs;
68+
const settings = JSON.parse(data.toString());
69+
this.jestVersionMajor = parseInt(settings.version.split('.').shift(), 10);
70+
this.configs =
71+
this.jestVersionMajor >= 21 ? settings.configs : [settings.config];
7072
});
7173

7274
// They could have an older build of Jest which
@@ -76,9 +78,9 @@ export default class Settings extends EventEmitter {
7678
});
7779
}
7880

79-
getConfig(completed: any) {
81+
getConfig(completed: any, index: number = 0) {
8082
this.getConfigs(() => {
81-
this.settings = this.settings[0];
83+
this.settings = this.configs[index];
8284
completed();
8385
});
8486
}

packages/jest-editor-support/src/__tests__/settings.test.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ describe('Settings', () => {
2626
expect(settings.settings).toEqual(expect.any(Object));
2727
});
2828

29-
it('reads and parses the configs', () => {
29+
it('[jest 20] reads and parses the config', () => {
3030
const workspace = new ProjectWorkspace(
3131
'root_path',
3232
'path_to_jest',
3333
'test',
3434
1000,
3535
);
3636
const completed = jest.fn();
37-
const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}];
37+
const config = {cacheDirectory: '/tmp/jest', name: '[md5 hash]'};
3838
const json = {
39-
configs,
39+
config,
4040
version: '19.0.0',
4141
};
4242

@@ -46,16 +46,16 @@ describe('Settings', () => {
4646
const buffer = makeBuffer(JSON.stringify(json));
4747
const settings = new Settings(workspace, {createProcess});
4848

49-
settings.getConfigs(completed);
49+
settings.getConfig(completed);
5050
settings.getConfigProcess.stdout.emit('data', buffer);
5151
settings.getConfigProcess.emit('close');
5252

5353
expect(completed).toHaveBeenCalled();
5454
expect(settings.jestVersionMajor).toBe(19);
55-
expect(settings.settings).toEqual(configs);
55+
expect(settings.settings).toEqual(config);
5656
});
5757

58-
it('reads and parses the config', () => {
58+
it('[jest 21] reads and parses the config', () => {
5959
const workspace = new ProjectWorkspace(
6060
'root_path',
6161
'path_to_jest',
@@ -66,7 +66,7 @@ describe('Settings', () => {
6666
const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}];
6767
const json = {
6868
configs,
69-
version: '19.0.0',
69+
version: '21.0.0',
7070
};
7171

7272
const mockProcess: any = new EventEmitter();
@@ -80,10 +80,39 @@ describe('Settings', () => {
8080
settings.getConfigProcess.emit('close');
8181

8282
expect(completed).toHaveBeenCalled();
83-
expect(settings.jestVersionMajor).toBe(19);
83+
expect(settings.jestVersionMajor).toBe(21);
8484
expect(settings.settings).toEqual(configs[0]);
8585
});
8686

87+
it('[jest 21] reads and parses the configs', () => {
88+
const workspace = new ProjectWorkspace(
89+
'root_path',
90+
'path_to_jest',
91+
'test',
92+
1000,
93+
);
94+
const completed = jest.fn();
95+
const configs = [{cacheDirectory: '/tmp/jest', name: '[md5 hash]'}];
96+
const json = {
97+
configs,
98+
version: '21.0.0',
99+
};
100+
101+
const mockProcess: any = new EventEmitter();
102+
mockProcess.stdout = new EventEmitter();
103+
const createProcess = () => mockProcess;
104+
const buffer = makeBuffer(JSON.stringify(json));
105+
const settings = new Settings(workspace, {createProcess});
106+
107+
settings.getConfigs(completed);
108+
settings.getConfigProcess.stdout.emit('data', buffer);
109+
settings.getConfigProcess.emit('close');
110+
111+
expect(completed).toHaveBeenCalled();
112+
expect(settings.jestVersionMajor).toBe(21);
113+
expect(settings.configs).toEqual(configs);
114+
});
115+
87116
it('calls callback even if no data is sent', () => {
88117
const workspace = new ProjectWorkspace(
89118
'root_path',

0 commit comments

Comments
 (0)