Skip to content

Commit aef55a2

Browse files
renovate[bot]armandabric
authored andcommitted
chore(deps): update jest monorepo to v23 (major) (#305)
* 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
1 parent 447f9c7 commit aef55a2

File tree

7 files changed

+327
-244
lines changed

7 files changed

+327
-244
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"@commitlint/config-angular": "7.3.1",
4040
"babel-cli": "6.26.0",
4141
"babel-eslint": "10.0.1",
42-
"babel-jest": "22.2.2",
42+
"babel-jest": "23.6.0",
4343
"babel-preset-es2015": "6.24.1",
4444
"babel-preset-flow": "6.23.0",
4545
"babel-preset-react": "6.24.1",
@@ -57,11 +57,11 @@
5757
"eslint-plugin-prettier": "3.0.1",
5858
"eslint-plugin-react": "7.12.4",
5959
"esm": "3.1.1",
60-
"expect": "22.3.0",
60+
"expect": "23.6.0",
6161
"flow-bin": "0.91.0",
6262
"flow-copy-source": "2.0.2",
6363
"husky": "1.3.1",
64-
"jest": "22.3.0",
64+
"jest": "23.6.0",
6565
"json": "9.0.6",
6666
"lint-staged": "8.1.0",
6767
"mversion": "1.12.0",

src/formatter/formatFunction.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ import type { Options } from './../options';
22

33
function noRefCheck() {}
44

5-
const defaultFunctionValue = (fn: any): any => fn.toString();
5+
export const inlineFunction = (fn: any): string =>
6+
fn
7+
.toString()
8+
.split('\n')
9+
.map(line => line.trim())
10+
.join('');
11+
12+
export const preserveFunctionLineBreak = (fn: any): string => fn.toString();
13+
14+
const defaultFunctionValue = inlineFunction;
615

716
export default (fn: Function, options: Options): string => {
817
const { functionValue = defaultFunctionValue, showFunctions } = options;

src/formatter/formatFunction.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ describe('formatFunction', () => {
2222
});
2323

2424
it('should format a function if showFunctions is true', () => {
25-
expect(formatFunction(hello, { showFunctions: true }))
26-
.toEqual(`function hello() {
27-
return 1;
28-
}`);
25+
expect(formatFunction(hello, { showFunctions: true })).toEqual(
26+
'function hello() {return 1;}'
27+
);
2928
});
3029

3130
it('should format a function without name if showFunctions is true', () => {

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ const reactElementToJsxString = (
4141
};
4242

4343
export default reactElementToJsxString;
44+
45+
export {
46+
inlineFunction,
47+
preserveFunctionLineBreak,
48+
} from './formatter/formatFunction';

src/index.spec.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import React, { Fragment, Component } from 'react';
66
import { createRenderer } from 'react-test-renderer/shallow';
77
import { mount } from 'enzyme';
8-
import reactElementToJSXString from './index';
8+
import reactElementToJSXString, { preserveFunctionLineBreak } from './index';
99
import AnonymousStatelessComponent from './AnonymousStatelessComponent';
1010

1111
class TestComponent extends React.Component {}
@@ -889,13 +889,25 @@ describe('reactElementToJSXString(ReactElement)', () => {
889889
reactElementToJSXString(<div fn={fn} />, {
890890
showFunctions: true,
891891
})
892-
).toEqual(
893-
`<div
892+
).toEqual(`<div fn={function fn() {return 'value';}} />`);
893+
});
894+
895+
it('should expose the multiline "functionValue" formatter', () => {
896+
/* eslint-disable arrow-body-style */
897+
const fn = () => {
898+
return 'value';
899+
};
900+
901+
expect(
902+
reactElementToJSXString(<div fn={fn} />, {
903+
showFunctions: true,
904+
functionValue: preserveFunctionLineBreak,
905+
})
906+
).toEqual(`<div
894907
fn={function fn() {
895908
return 'value';
896909
}}
897-
/>`
898-
);
910+
/>`);
899911
});
900912

901913
it('reactElementToJSXString(<DisplayNamePrecedence />)', () => {

tests/smoke/smoke.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const requireReactElementToJsxString = buildType => {
88
if (buildType === 'esm') {
99
return require(`./../../dist/esm`).default;
1010
} else if (buildType === 'cjs') {
11-
return require('./../../dist/cjs');
11+
return require('./../../dist/cjs').default;
1212
}
1313

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

0 commit comments

Comments
 (0)