Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update babylon to babel/parser 7.x #30

Merged
merged 23 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "8"
- "10"
script:
- yarn ci
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
Expand Down
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed

## Master


* Replace babylon and typescript parsers with @babel/parser 7.x - @firsttris
-->

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

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

17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ The engine that allows editors to build on top of Jest.

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

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/).
## API
```
parse(
filePath: string,
serializedData?: string,
strictMode: boolean = false,
)
```
Parse is a static Jest parser which uses Babel 7 and supports js,jsx,mjs,ts,tsx files.

[Supported ECMAScript proposals](https://github.com/babel/babel/blob/928b9f8c9518284eac6d0598633f2ec373fc6d0c/packages/babel-parser/typings/babel-parser.d.ts#L97)

- filePath = Path to the file you want to parse.
- serializedData = Serialized data, will be used instead of the filePath if available (optional).
- strictMode = If this option is activated the parser throws an exception if the filetype is not detected, defaults to false.

Copy link
Collaborator

@connectdotz connectdotz May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great way to start documenting the API 👍 , can we throw in an example, I am a big fan of "learn-by-example'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be best to create an example with typescript and to link it in the readme (maybe follow up PR)


## Note

Expand Down
82 changes: 20 additions & 62 deletions fixtures/parser_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
seanpoulter marked this conversation as resolved.
Show resolved Hide resolved
*/

const fixtures = __dirname;

function parserTests(parse: (file: string) => ParserResult) {
const assertBlock2 = (
block,
sl: number,
sc: number,
el: number,
ec: number,
name: ?string = null,
) => assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
function parserTests(parse: (file: string) => ParseResult) {
const assertBlock = (block, start, end, name: ?string = null) => {
expect(block.start).toEqual(start);
expect(block.end).toEqual(end);
if (name) {
expect(block.name).toEqual(name);
}
};
const assertBlock2 = (block, sl: number, sc: number, el: number, ec: number, name: ?string = null) =>
assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
firsttris marked this conversation as resolved.
Show resolved Hide resolved
describe('File parsing without throwing', () => {
it('Should not throw', () => {
expect(() => {
Expand Down Expand Up @@ -174,12 +169,7 @@ function parserTests(parse: (file: string) => ParserResult) {
expect(data.describeBlocks.length).toEqual(4);

const firstDescribe = data.describeBlocks[0];
assertBlock(
firstDescribe,
{column: 1, line: 10},
{column: 2, line: 20},
'.isCI',
);
assertBlock(firstDescribe, {column: 1, line: 10}, {column: 2, line: 20}, '.isCI');
});
it('finds test blocks within describe blocks', () => {
const data = parse(`${fixtures}/dangerjs/travis-ci.example`);
Expand All @@ -191,7 +181,7 @@ function parserTests(parse: (file: string) => ParserResult) {
b =>
b.name === 'needs to have a PR number' ||
b.name === 'does not validate without josh' ||
b.name === 'does not validate when ${key} is missing',
b.name === 'does not validate when ${key} is missing'
);
expect(found.length).toBe(3);
});
Expand All @@ -200,9 +190,7 @@ function parserTests(parse: (file: string) => ParserResult) {
let nested;
beforeEach(() => {
const data = parse(`${fixtures}/nested_elements.example`);
nested = data.root.children.filter(
e => e.type === 'describe' && e.name === 'describe 1.0',
)[0];
nested = data.root.children.filter(e => e.type === 'describe' && e.name === 'describe 1.0')[0];
});
it('can find nested describe or test blocks', () => {
expect(nested.children.length).toBe(2);
Expand Down Expand Up @@ -233,58 +221,32 @@ function parserTests(parse: (file: string) => ParserResult) {
expect(parseResult.expects.length).toEqual(4);
});
test(`no expression template`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'no expression template',
)[0];
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'no expression template')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];

expect(dBlock.children.length).toBe(1);
expect(t1.children.length).toBe(1);

assertBlock(
t1,
{column: 3, line: 2},
{column: 5, line: 4},
'test has no expression either',
);
assertBlock(t1, {column: 3, line: 2}, {column: 5, line: 4}, 'test has no expression either');
assertBlock(e1, {column: 5, line: 3}, {column: 25, line: 3});
});

test(`simple template literal`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'simple template literal',
)[0];
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'simple template literal')[0];
expect(dBlock.children.length).toBe(3);

const t1 = dBlock.children[0];
const t2 = dBlock.children[1];
const t3 = dBlock.children[2];

assertBlock(
t1,
{column: 3, line: 8},
{column: 46, line: 8},
'${expression} up front',
);
assertBlock(
t2,
{column: 3, line: 9},
{column: 4, line: 10},
'at the end ${expression}',
);
assertBlock(
t3,
{column: 3, line: 11},
{column: 5, line: 12},
'mixed ${expression1} and ${expression2}',
);
assertBlock(t1, {column: 3, line: 8}, {column: 46, line: 8}, '${expression} up front');
assertBlock(t2, {column: 3, line: 9}, {column: 4, line: 10}, 'at the end ${expression}');
assertBlock(t3, {column: 3, line: 11}, {column: 5, line: 12}, 'mixed ${expression1} and ${expression2}');
});

test(`template literal with functions`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'template literal with functions',
)[0];
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'template literal with functions')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];

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

test(`multiline template literal`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'multiline template literal',
)[0];
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'multiline template literal')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];

Expand All @@ -315,15 +275,13 @@ function parserTests(parse: (file: string) => ParserResult) {
{column: 3, line: 22},
{column: 5, line: 25},
`this \${test} will span in
multiple lines`,
multiple lines`
);
assertBlock(e1, {column: 5, line: 24}, {column: 32, line: 24});
});

test(`edge case: should not fail`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'edge case: should not fail',
)[0];
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'edge case: should not fail')[0];
seanpoulter marked this conversation as resolved.
Show resolved Hide resolved
const t1 = dBlock.children[0];
const e1 = t1.children[0];

Expand All @@ -341,7 +299,7 @@ function parserTests(parse: (file: string) => ParserResult) {
startLine: number,
startCol: number,
endLine: number,
endCol: number,
endCol: number
) => {
expect(nBlock.name).toEqual(name);
expect(nBlock.nameRange.start.line).toEqual(startLine);
Expand Down Expand Up @@ -372,7 +330,7 @@ function parserTests(parse: (file: string) => ParserResult) {
22,
7,
23,
18,
18
firsttris marked this conversation as resolved.
Show resolved Hide resolved
);
});
});
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test": "jest",
"flow": "flow",
"lint": "eslint \"?(__mocks__|src|tests)/**/*.+(js|ts)\"",
"lint:fix": "eslint \"?(__mocks__|src|tests)/**/*.+(js|ts)\" --fix",
"ci": "yarn type-check && yarn lint && yarn test --coverage && yarn flow",
"prettier": "prettier --check \"?(__mocks__|src|tests)/**/*.+(js|ts)\"",
"prettier-write": "prettier --write",
Expand Down Expand Up @@ -56,15 +57,16 @@
"flow-bin": "^0.97.0",
"jest": "^24.7.0",
"lint-staged": "^8.1.5",
"prettier": "^1.17.0"
"prettier": "^1.17.0",
"typescript": "^3.8.2"
},
"dependencies": {
"@babel/parser": "^7.8.3",
"@babel/traverse": "^7.6.2",
"@babel/types": "^7.8.3",
"@jest/types": "^24.8.0",
"babylon": "^6.14.1",
"core-js": "^3.2.1",
"jest-snapshot": "^24.7.0",
"typescript": "^3.4.3"
firsttris marked this conversation as resolved.
Show resolved Hide resolved
"jest-snapshot": "^24.7.0"
},
"jest": {
"testPathIgnorePatterns": [
Expand Down
4 changes: 1 addition & 3 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ export default class Runner extends EventEmitter {
// knowing this could leave orphan process...
// eslint-disable-next-line no-console
console.warn(
`failed to kill process group, this could leave some orphan process whose ppid=${
this.debugprocess.pid
}. error=`,
`failed to kill process group, this could leave some orphan process whose ppid=${this.debugprocess.pid}. error=`,
e
);
this.debugprocess.kill();
Expand Down
2 changes: 1 addition & 1 deletion src/Snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import traverse from '@babel/traverse';
import {buildSnapshotResolver, utils} from 'jest-snapshot';
import type {ProjectConfig} from '../types/Config';

import {getASTfor} from './parsers/babylon_parser';
import {getASTfor} from './parsers/babel_parser';

type Node = any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

const {parse} = require('../../parsers/babylon_parser');
const {parseJs} = require('../../parsers/babel_parser');
const {parserTests} = require('../../../fixtures/parser_tests');

parserTests(parse);
parserTests(parseJs);
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {parse} from '../../parsers/typescript_parser';
import {parseTs} from '../../parsers/babel_parser';
import {parserTests} from '../../../fixtures/parser_tests';

parserTests(parse);
parserTests(parseTs);
28 changes: 11 additions & 17 deletions src/__tests__/parsers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,31 @@
*/

import parse from '../../parsers';
import {parse as js_parse} from '../../parsers/babylon_parser';
import {parse as ts_parse} from '../../parsers/typescript_parser';
import {parseJs, parseTs} from '../../parsers/babel_parser';

jest.mock('../../parsers/babylon_parser', () => {
const mock_js_parse = jest.fn();
return {parse: mock_js_parse};
});
jest.mock('../../parsers/typescript_parser', () => {
const mock_ts_parse = jest.fn();
return {parse: mock_ts_parse};
jest.mock('../../parsers/babel_parser', () => {
return {parseJs: jest.fn(), parseTs: jest.fn()};
});

describe('select parser', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('for .js or .jsx file', () => {
const files = ['abc.js', 'abc.jsx'];
it('for .js or .jsx or .mjs file', () => {
const files = ['abc.js', 'abc.jsx', 'abc.mjs'];
files.forEach(file => {
parse(file, undefined, true);
expect(js_parse).toHaveBeenCalled();
expect(ts_parse).not.toHaveBeenCalled();
expect(parseJs).toHaveBeenCalled();
expect(parseTs).not.toHaveBeenCalled();
jest.clearAllMocks();
});
});
it('for .ts or .tsx file', () => {
const files = ['abc.ts', 'abc.tsx'];
files.forEach(file => {
parse(file, undefined, true);
expect(js_parse).not.toHaveBeenCalled();
expect(ts_parse).toHaveBeenCalled();
expect(parseJs).not.toHaveBeenCalled();
expect(parseTs).toHaveBeenCalled();
jest.clearAllMocks();
});
});
Expand All @@ -45,8 +39,8 @@ describe('select parser', () => {
const files = ['abc', 'abc.ttsx'];
files.forEach(file => {
expect(() => parse(file, undefined, false)).not.toThrow();
expect(js_parse).toHaveBeenCalled();
expect(ts_parse).not.toHaveBeenCalled();
expect(parseJs).toHaveBeenCalled();
expect(parseTs).not.toHaveBeenCalled();
jest.clearAllMocks();
});
});
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ describe('Runner', () => {
expect(close).toBeCalled();
});

it('emits debuggerProcessExit when process close', () => {
const close = jest.fn();
runner.on('debuggerProcessExit', close);
fakeProcess.emit('close');
expect(close).toBeCalled();
});

firsttris marked this conversation as resolved.
Show resolved Hide resolved
it('should start jest process after killing the old process', () => {
runner.closeProcess();
runner.start();
Expand Down
Loading