Skip to content

Commit 5e122c5

Browse files
firsttrisseanpoulterTristan Teufel
authored
update babylon to babel/parser 7.x (#30)
* update babylon to babel/parser 7.x * fix snapshot.test and typing * update changelog * remove @flow from parser_tests.js * revert import in parser_tests.js * increase test coverage * remove typescript * make options optional * add addtional plugins * Update CHANGELOG.md Co-Authored-By: Sean Poulter <sean.poulter+gh@gmail.com> * fix changelog * add support for mjs * make ECMA Proposal plugins configurable * update readme * remove package-lock.json * move typescript to devDependencies * extend readme * extend default plugin list * update plugin list * increadse node_js to 10 in travis.yml * update readme * fix prettier Co-authored-by: Sean Poulter <sean.poulter+gh@gmail.com> Co-authored-by: Tristan Teufel <tristant@CI327.fritz.box>
1 parent 0f49706 commit 5e122c5

15 files changed

+2033
-2715
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- "8"
3+
- "10"
44
script:
55
- yarn ci
66
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ Please add your own contribution below inside the Master section
44
Bug-fixes within the same version aren't needed
55
66
## Master
7-
8-
7+
* Replace babylon and typescript parsers with @babel/parser 7.x - @firsttris
98
-->
109

1110
### 27.2.0
@@ -51,4 +50,3 @@ in your head, consider this a 1.0 kinda thing.
5150
look at the usage of the `buildSnapshotResolver`
5251

5352
- Adds the ability to parse describe blocks - https://github.com/facebook/jest/pull/7215
54-

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@ The engine that allows editors to build on top of Jest.
1010

1111
This is only useful if you are interested in building an editor integration for Jest.
1212

13-
For now as an end user, we'd recommend looking at either [vscode-jest](https://github.com/jest-community/vscode-jest/) or [majestic](https://github.com/Raathigesh/majestic/).
13+
## API
14+
```
15+
parse(
16+
filePath: string,
17+
serializedData?: string,
18+
strictMode: boolean = false,
19+
)
20+
```
21+
Parse is a static Jest parser which uses Babel 7 and supports js,jsx,mjs,ts,tsx files.
22+
23+
[Supported ECMAScript proposals](https://github.com/babel/babel/blob/928b9f8c9518284eac6d0598633f2ec373fc6d0c/packages/babel-parser/typings/babel-parser.d.ts#L97)
24+
25+
- filePath = Path to the file you want to parse.
26+
- serializedData = Serialized data, will be used instead of the filePath if available (optional).
27+
- strictMode = If this option is activated the parser throws an exception if the filetype is not detected, defaults to false.
28+
1429

1530
## Note
1631

fixtures/parser_tests.js

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,21 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
67
*/
78

89
const fixtures = __dirname;
910

10-
function parserTests(parse: (file: string) => ParserResult) {
11-
const assertBlock2 = (
12-
block,
13-
sl: number,
14-
sc: number,
15-
el: number,
16-
ec: number,
17-
name: ?string = null,
18-
) => assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
11+
function parserTests(parse: (file: string) => ParseResult) {
1912
const assertBlock = (block, start, end, name: ?string = null) => {
2013
expect(block.start).toEqual(start);
2114
expect(block.end).toEqual(end);
2215
if (name) {
2316
expect(block.name).toEqual(name);
2417
}
2518
};
19+
const assertBlock2 = (block, sl: number, sc: number, el: number, ec: number, name: ?string = null) =>
20+
assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
2621
describe('File parsing without throwing', () => {
2722
it('Should not throw', () => {
2823
expect(() => {
@@ -174,12 +169,7 @@ function parserTests(parse: (file: string) => ParserResult) {
174169
expect(data.describeBlocks.length).toEqual(4);
175170

176171
const firstDescribe = data.describeBlocks[0];
177-
assertBlock(
178-
firstDescribe,
179-
{column: 1, line: 10},
180-
{column: 2, line: 20},
181-
'.isCI',
182-
);
172+
assertBlock(firstDescribe, {column: 1, line: 10}, {column: 2, line: 20}, '.isCI');
183173
});
184174
it('finds test blocks within describe blocks', () => {
185175
const data = parse(`${fixtures}/dangerjs/travis-ci.example`);
@@ -191,7 +181,7 @@ function parserTests(parse: (file: string) => ParserResult) {
191181
b =>
192182
b.name === 'needs to have a PR number' ||
193183
b.name === 'does not validate without josh' ||
194-
b.name === 'does not validate when ${key} is missing',
184+
b.name === 'does not validate when ${key} is missing'
195185
);
196186
expect(found.length).toBe(3);
197187
});
@@ -200,9 +190,7 @@ function parserTests(parse: (file: string) => ParserResult) {
200190
let nested;
201191
beforeEach(() => {
202192
const data = parse(`${fixtures}/nested_elements.example`);
203-
nested = data.root.children.filter(
204-
e => e.type === 'describe' && e.name === 'describe 1.0',
205-
)[0];
193+
nested = data.root.children.filter(e => e.type === 'describe' && e.name === 'describe 1.0')[0];
206194
});
207195
it('can find nested describe or test blocks', () => {
208196
expect(nested.children.length).toBe(2);
@@ -233,58 +221,32 @@ function parserTests(parse: (file: string) => ParserResult) {
233221
expect(parseResult.expects.length).toEqual(4);
234222
});
235223
test(`no expression template`, () => {
236-
const dBlock = parseResult.describeBlocks.filter(
237-
b => b.name === 'no expression template',
238-
)[0];
224+
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'no expression template')[0];
239225
const t1 = dBlock.children[0];
240226
const e1 = t1.children[0];
241227

242228
expect(dBlock.children.length).toBe(1);
243229
expect(t1.children.length).toBe(1);
244230

245-
assertBlock(
246-
t1,
247-
{column: 3, line: 2},
248-
{column: 5, line: 4},
249-
'test has no expression either',
250-
);
231+
assertBlock(t1, {column: 3, line: 2}, {column: 5, line: 4}, 'test has no expression either');
251232
assertBlock(e1, {column: 5, line: 3}, {column: 25, line: 3});
252233
});
253234

254235
test(`simple template literal`, () => {
255-
const dBlock = parseResult.describeBlocks.filter(
256-
b => b.name === 'simple template literal',
257-
)[0];
236+
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'simple template literal')[0];
258237
expect(dBlock.children.length).toBe(3);
259238

260239
const t1 = dBlock.children[0];
261240
const t2 = dBlock.children[1];
262241
const t3 = dBlock.children[2];
263242

264-
assertBlock(
265-
t1,
266-
{column: 3, line: 8},
267-
{column: 46, line: 8},
268-
'${expression} up front',
269-
);
270-
assertBlock(
271-
t2,
272-
{column: 3, line: 9},
273-
{column: 4, line: 10},
274-
'at the end ${expression}',
275-
);
276-
assertBlock(
277-
t3,
278-
{column: 3, line: 11},
279-
{column: 5, line: 12},
280-
'mixed ${expression1} and ${expression2}',
281-
);
243+
assertBlock(t1, {column: 3, line: 8}, {column: 46, line: 8}, '${expression} up front');
244+
assertBlock(t2, {column: 3, line: 9}, {column: 4, line: 10}, 'at the end ${expression}');
245+
assertBlock(t3, {column: 3, line: 11}, {column: 5, line: 12}, 'mixed ${expression1} and ${expression2}');
282246
});
283247

284248
test(`template literal with functions`, () => {
285-
const dBlock = parseResult.describeBlocks.filter(
286-
b => b.name === 'template literal with functions',
287-
)[0];
249+
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'template literal with functions')[0];
288250
const t1 = dBlock.children[0];
289251
const e1 = t1.children[0];
290252

@@ -295,15 +257,13 @@ function parserTests(parse: (file: string) => ParserResult) {
295257
t1,
296258
{column: 3, line: 16},
297259
{column: 5, line: 18},
298-
'this ${test} calls ${JSON.stringfy(expression)} should still work',
260+
'this ${test} calls ${JSON.stringfy(expression)} should still work'
299261
);
300262
assertBlock(e1, {column: 5, line: 17}, {column: 31, line: 17});
301263
});
302264

303265
test(`multiline template literal`, () => {
304-
const dBlock = parseResult.describeBlocks.filter(
305-
b => b.name === 'multiline template literal',
306-
)[0];
266+
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'multiline template literal')[0];
307267
const t1 = dBlock.children[0];
308268
const e1 = t1.children[0];
309269

@@ -315,15 +275,13 @@ function parserTests(parse: (file: string) => ParserResult) {
315275
{column: 3, line: 22},
316276
{column: 5, line: 25},
317277
`this \${test} will span in
318-
multiple lines`,
278+
multiple lines`
319279
);
320280
assertBlock(e1, {column: 5, line: 24}, {column: 32, line: 24});
321281
});
322282

323283
test(`edge case: should not fail`, () => {
324-
const dBlock = parseResult.describeBlocks.filter(
325-
b => b.name === 'edge case: should not fail',
326-
)[0];
284+
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'edge case: should not fail')[0];
327285
const t1 = dBlock.children[0];
328286
const e1 = t1.children[0];
329287

@@ -341,7 +299,7 @@ function parserTests(parse: (file: string) => ParserResult) {
341299
startLine: number,
342300
startCol: number,
343301
endLine: number,
344-
endCol: number,
302+
endCol: number
345303
) => {
346304
expect(nBlock.name).toEqual(name);
347305
expect(nBlock.nameRange.start.line).toEqual(startLine);
@@ -372,7 +330,7 @@ function parserTests(parse: (file: string) => ParserResult) {
372330
22,
373331
7,
374332
23,
375-
18,
333+
18
376334
);
377335
});
378336
});

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"test": "jest",
2525
"flow": "flow",
2626
"lint": "eslint \"?(__mocks__|src|tests)/**/*.+(js|ts)\"",
27+
"lint:fix": "eslint \"?(__mocks__|src|tests)/**/*.+(js|ts)\" --fix",
2728
"ci": "yarn type-check && yarn lint && yarn test --coverage && yarn flow",
2829
"prettier": "prettier --check \"?(__mocks__|src|tests)/**/*.+(js|ts)\"",
2930
"prettier-write": "prettier --write",
@@ -56,15 +57,16 @@
5657
"flow-bin": "^0.97.0",
5758
"jest": "^24.7.0",
5859
"lint-staged": "^8.1.5",
59-
"prettier": "^1.17.0"
60+
"prettier": "^1.17.0",
61+
"typescript": "^3.8.2"
6062
},
6163
"dependencies": {
64+
"@babel/parser": "^7.8.3",
6265
"@babel/traverse": "^7.6.2",
66+
"@babel/types": "^7.8.3",
6367
"@jest/types": "^24.8.0",
64-
"babylon": "^6.14.1",
6568
"core-js": "^3.2.1",
66-
"jest-snapshot": "^24.7.0",
67-
"typescript": "^3.4.3"
69+
"jest-snapshot": "^24.7.0"
6870
},
6971
"jest": {
7072
"testPathIgnorePatterns": [

src/Runner.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ export default class Runner extends EventEmitter {
189189
// knowing this could leave orphan process...
190190
// eslint-disable-next-line no-console
191191
console.warn(
192-
`failed to kill process group, this could leave some orphan process whose ppid=${
193-
this.debugprocess.pid
194-
}. error=`,
192+
`failed to kill process group, this could leave some orphan process whose ppid=${this.debugprocess.pid}. error=`,
195193
e
196194
);
197195
this.debugprocess.kill();

src/Snapshot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import traverse from '@babel/traverse';
1212
import {buildSnapshotResolver, utils} from 'jest-snapshot';
1313
import type {ProjectConfig} from '../types/Config';
1414

15-
import {getASTfor} from './parsers/babylon_parser';
15+
import {getASTfor} from './parsers/babel_parser';
1616

1717
type Node = any;
1818

src/__tests__/parsers/babylon_parser.test.js renamed to src/__tests__/parsers/babel_parser.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
const {parse} = require('../../parsers/babylon_parser');
10+
const {parseJs} = require('../../parsers/babel_parser');
1111
const {parserTests} = require('../../../fixtures/parser_tests');
1212

13-
parserTests(parse);
13+
parserTests(parseJs);

src/__tests__/parsers/typescript_parser.test.js renamed to src/__tests__/parsers/babel_typescript_parser.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {parse} from '../../parsers/typescript_parser';
8+
import {parseTs} from '../../parsers/babel_parser';
99
import {parserTests} from '../../../fixtures/parser_tests';
1010

11-
parserTests(parse);
11+
parserTests(parseTs);

src/__tests__/parsers/index.test.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,31 @@
66
*/
77

88
import parse from '../../parsers';
9-
import {parse as js_parse} from '../../parsers/babylon_parser';
10-
import {parse as ts_parse} from '../../parsers/typescript_parser';
9+
import {parseJs, parseTs} from '../../parsers/babel_parser';
1110

12-
jest.mock('../../parsers/babylon_parser', () => {
13-
const mock_js_parse = jest.fn();
14-
return {parse: mock_js_parse};
15-
});
16-
jest.mock('../../parsers/typescript_parser', () => {
17-
const mock_ts_parse = jest.fn();
18-
return {parse: mock_ts_parse};
11+
jest.mock('../../parsers/babel_parser', () => {
12+
return {parseJs: jest.fn(), parseTs: jest.fn()};
1913
});
2014

2115
describe('select parser', () => {
2216
beforeEach(() => {
2317
jest.clearAllMocks();
2418
});
25-
it('for .js or .jsx file', () => {
26-
const files = ['abc.js', 'abc.jsx'];
19+
it('for .js or .jsx or .mjs file', () => {
20+
const files = ['abc.js', 'abc.jsx', 'abc.mjs'];
2721
files.forEach(file => {
2822
parse(file, undefined, true);
29-
expect(js_parse).toHaveBeenCalled();
30-
expect(ts_parse).not.toHaveBeenCalled();
23+
expect(parseJs).toHaveBeenCalled();
24+
expect(parseTs).not.toHaveBeenCalled();
3125
jest.clearAllMocks();
3226
});
3327
});
3428
it('for .ts or .tsx file', () => {
3529
const files = ['abc.ts', 'abc.tsx'];
3630
files.forEach(file => {
3731
parse(file, undefined, true);
38-
expect(js_parse).not.toHaveBeenCalled();
39-
expect(ts_parse).toHaveBeenCalled();
32+
expect(parseJs).not.toHaveBeenCalled();
33+
expect(parseTs).toHaveBeenCalled();
4034
jest.clearAllMocks();
4135
});
4236
});
@@ -45,8 +39,8 @@ describe('select parser', () => {
4539
const files = ['abc', 'abc.ttsx'];
4640
files.forEach(file => {
4741
expect(() => parse(file, undefined, false)).not.toThrow();
48-
expect(js_parse).toHaveBeenCalled();
49-
expect(ts_parse).not.toHaveBeenCalled();
42+
expect(parseJs).toHaveBeenCalled();
43+
expect(parseTs).not.toHaveBeenCalled();
5044
jest.clearAllMocks();
5145
});
5246
});

src/__tests__/runner.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ describe('Runner', () => {
429429
expect(close).toBeCalled();
430430
});
431431

432+
it('emits debuggerProcessExit when process close', () => {
433+
const close = jest.fn();
434+
runner.on('debuggerProcessExit', close);
435+
fakeProcess.emit('close');
436+
expect(close).toBeCalled();
437+
});
438+
432439
it('should start jest process after killing the old process', () => {
433440
runner.closeProcess();
434441
runner.start();

0 commit comments

Comments
 (0)