Skip to content

Commit

Permalink
chore(deps): update jest monorepo to v23 (major) (#305)
Browse files Browse the repository at this point in the history
* chore(deps): update jest monorepo to v23

* feature(formatting): Harmonize the function formatting

BREAKING CHANGE: If you use the `showFunctions: true` option, the function are now always inlined in the output by default. Before it was not always the case (depending one the engine, platform or babel versions)

You could get back to the previous behavior by using the `preserveFunctionLineBreak` function export as a value of the option `functionValue`.

* test(smoke): Adapt the CommonJS bundle import
  • Loading branch information
renovate[bot] authored and armandabric committed Jan 20, 2019
1 parent 447f9c7 commit aef55a2
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 244 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@commitlint/config-angular": "7.3.1",
"babel-cli": "6.26.0",
"babel-eslint": "10.0.1",
"babel-jest": "22.2.2",
"babel-jest": "23.6.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-flow": "6.23.0",
"babel-preset-react": "6.24.1",
Expand All @@ -57,11 +57,11 @@
"eslint-plugin-prettier": "3.0.1",
"eslint-plugin-react": "7.12.4",
"esm": "3.1.1",
"expect": "22.3.0",
"expect": "23.6.0",
"flow-bin": "0.91.0",
"flow-copy-source": "2.0.2",
"husky": "1.3.1",
"jest": "22.3.0",
"jest": "23.6.0",
"json": "9.0.6",
"lint-staged": "8.1.0",
"mversion": "1.12.0",
Expand Down
11 changes: 10 additions & 1 deletion src/formatter/formatFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import type { Options } from './../options';

function noRefCheck() {}

const defaultFunctionValue = (fn: any): any => fn.toString();
export const inlineFunction = (fn: any): string =>
fn
.toString()
.split('\n')
.map(line => line.trim())
.join('');

export const preserveFunctionLineBreak = (fn: any): string => fn.toString();

const defaultFunctionValue = inlineFunction;

export default (fn: Function, options: Options): string => {
const { functionValue = defaultFunctionValue, showFunctions } = options;
Expand Down
7 changes: 3 additions & 4 deletions src/formatter/formatFunction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ describe('formatFunction', () => {
});

it('should format a function if showFunctions is true', () => {
expect(formatFunction(hello, { showFunctions: true }))
.toEqual(`function hello() {
return 1;
}`);
expect(formatFunction(hello, { showFunctions: true })).toEqual(
'function hello() {return 1;}'
);
});

it('should format a function without name if showFunctions is true', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ const reactElementToJsxString = (
};

export default reactElementToJsxString;

export {
inlineFunction,
preserveFunctionLineBreak,
} from './formatter/formatFunction';
22 changes: 17 additions & 5 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import React, { Fragment, Component } from 'react';
import { createRenderer } from 'react-test-renderer/shallow';
import { mount } from 'enzyme';
import reactElementToJSXString from './index';
import reactElementToJSXString, { preserveFunctionLineBreak } from './index';
import AnonymousStatelessComponent from './AnonymousStatelessComponent';

class TestComponent extends React.Component {}
Expand Down Expand Up @@ -889,13 +889,25 @@ describe('reactElementToJSXString(ReactElement)', () => {
reactElementToJSXString(<div fn={fn} />, {
showFunctions: true,
})
).toEqual(
`<div
).toEqual(`<div fn={function fn() {return 'value';}} />`);
});

it('should expose the multiline "functionValue" formatter', () => {
/* eslint-disable arrow-body-style */
const fn = () => {
return 'value';
};

expect(
reactElementToJSXString(<div fn={fn} />, {
showFunctions: true,
functionValue: preserveFunctionLineBreak,
})
).toEqual(`<div
fn={function fn() {
return 'value';
}}
/>`
);
/>`);
});

it('reactElementToJSXString(<DisplayNamePrecedence />)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const requireReactElementToJsxString = buildType => {
if (buildType === 'esm') {
return require(`./../../dist/esm`).default;
} else if (buildType === 'cjs') {
return require('./../../dist/cjs');
return require('./../../dist/cjs').default;
}

throw new Error(`Unknown build type: "${buildType}"`);
Expand Down
Loading

0 comments on commit aef55a2

Please sign in to comment.