Skip to content

Commit

Permalink
update babylon to babel/parser 7.x (#30)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed May 29, 2020
1 parent 0f49706 commit 5e122c5
Show file tree
Hide file tree
Showing 15 changed files with 2,033 additions and 2,715 deletions.
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.


## 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.
*
*/

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);
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];
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
);
});
});
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"
"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();
});

it('should start jest process after killing the old process', () => {
runner.closeProcess();
runner.start();
Expand Down
Loading

0 comments on commit 5e122c5

Please sign in to comment.