diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb1dff57463..2efb5c4e8048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Chore & Maintenance - `[*]` Remove flow from code base ([#8061](https://github.com/facebook/jest/pull/8061)) +- `[*]` Use property initializer syntax in Jest codebase [#8117](https://github.com/facebook/jest/pull/8117) ### Performance diff --git a/babel.config.js b/babel.config.js index 5b6610d3801e..e3a840c0600f 100644 --- a/babel.config.js +++ b/babel.config.js @@ -21,6 +21,7 @@ module.exports = { plugins: [ ['@babel/plugin-transform-modules-commonjs', {allowTopLevelThis: true}], '@babel/plugin-transform-strict-mode', + '@babel/plugin-proposal-class-properties', ], presets: [ [ diff --git a/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap b/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap index 13d01c6299aa..4f6b6acbdace 100644 --- a/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap +++ b/e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap @@ -15,6 +15,6 @@ PASS __tests__/console.test.js 15 | }); 16 | - at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:180:10) + at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:199:10) at log (__tests__/console.test.js:13:13) `; diff --git a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap index 9609d706b96c..74963f616e60 100644 --- a/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap +++ b/e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap @@ -30,6 +30,6 @@ FAIL __tests__/index.js 12 | module.exports = () => 'test'; 13 | - at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:435:17) + at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:455:17) at Object.require (index.js:10:1) `; diff --git a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap index 8bb20a9f7d29..22a54c4127da 100644 --- a/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap +++ b/e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap @@ -33,6 +33,6 @@ FAIL __tests__/test.js | ^ 4 | - at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:202:17) + at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:222:17) at Object.require (index.js:3:18) `; diff --git a/examples/enzyme/.babelrc.js b/examples/enzyme/.babelrc.js index 7256ac590a22..8310867ffaae 100644 --- a/examples/enzyme/.babelrc.js +++ b/examples/enzyme/.babelrc.js @@ -2,4 +2,5 @@ module.exports = { presets: ['@babel/preset-env', '@babel/preset-react'], + plugins: ['@babel/plugin-proposal-class-properties'] }; diff --git a/examples/enzyme/CheckboxWithLabel.js b/examples/enzyme/CheckboxWithLabel.js index ccad8995d978..607f01c3c196 100644 --- a/examples/enzyme/CheckboxWithLabel.js +++ b/examples/enzyme/CheckboxWithLabel.js @@ -3,18 +3,13 @@ import React from 'react'; export default class CheckboxWithLabel extends React.Component { - constructor(props) { - super(props); - this.state = {isChecked: false}; + state = { + isChecked: false, + }; - // bind manually because React class components don't auto-bind - // http://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding - this.onChange = this.onChange.bind(this); - } - - onChange() { + onChange = () => { this.setState({isChecked: !this.state.isChecked}); - } + }; render() { return ( diff --git a/examples/enzyme/package.json b/examples/enzyme/package.json index a173e71c8568..13118eaa83df 100644 --- a/examples/enzyme/package.json +++ b/examples/enzyme/package.json @@ -8,6 +8,7 @@ }, "devDependencies": { "@babel/core": "*", + "@babel/plugin-proposal-class-properties": "*", "@babel/preset-env": "*", "@babel/preset-react": "*", "babel-jest": "*", diff --git a/examples/react-testing-library/.babelrc.js b/examples/react-testing-library/.babelrc.js index 7256ac590a22..8310867ffaae 100644 --- a/examples/react-testing-library/.babelrc.js +++ b/examples/react-testing-library/.babelrc.js @@ -2,4 +2,5 @@ module.exports = { presets: ['@babel/preset-env', '@babel/preset-react'], + plugins: ['@babel/plugin-proposal-class-properties'] }; diff --git a/examples/react-testing-library/CheckboxWithLabel.js b/examples/react-testing-library/CheckboxWithLabel.js index ccad8995d978..3a2ae64f7302 100644 --- a/examples/react-testing-library/CheckboxWithLabel.js +++ b/examples/react-testing-library/CheckboxWithLabel.js @@ -3,18 +3,11 @@ import React from 'react'; export default class CheckboxWithLabel extends React.Component { - constructor(props) { - super(props); - this.state = {isChecked: false}; + state = {isChecked: false}; - // bind manually because React class components don't auto-bind - // http://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding - this.onChange = this.onChange.bind(this); - } - - onChange() { + onChange = () => { this.setState({isChecked: !this.state.isChecked}); - } + }; render() { return ( diff --git a/examples/react-testing-library/package.json b/examples/react-testing-library/package.json index 9e2b4c43543b..271f352011a9 100644 --- a/examples/react-testing-library/package.json +++ b/examples/react-testing-library/package.json @@ -8,6 +8,7 @@ }, "devDependencies": { "@babel/core": "*", + "@babel/plugin-proposal-class-properties": "*", "@babel/preset-env": "*", "@babel/preset-react": "*", "babel-jest": "*", diff --git a/examples/react/CheckboxWithLabel.js b/examples/react/CheckboxWithLabel.js index ccad8995d978..9296cda1c5e0 100644 --- a/examples/react/CheckboxWithLabel.js +++ b/examples/react/CheckboxWithLabel.js @@ -6,15 +6,11 @@ export default class CheckboxWithLabel extends React.Component { constructor(props) { super(props); this.state = {isChecked: false}; - - // bind manually because React class components don't auto-bind - // http://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding - this.onChange = this.onChange.bind(this); } - onChange() { + onChange = () => { this.setState({isChecked: !this.state.isChecked}); - } + }; render() { return ( diff --git a/examples/snapshot/Link.react.js b/examples/snapshot/Link.react.js index 774937ef2add..288c7429df8b 100644 --- a/examples/snapshot/Link.react.js +++ b/examples/snapshot/Link.react.js @@ -11,21 +11,18 @@ export default class Link extends React.Component { constructor() { super(); - this._onMouseEnter = this._onMouseEnter.bind(this); - this._onMouseLeave = this._onMouseLeave.bind(this); - this.state = { class: STATUS.NORMAL, }; } - _onMouseEnter() { + _onMouseEnter = () => { this.setState({class: STATUS.HOVERED}); - } + }; - _onMouseLeave() { + _onMouseLeave = () => { this.setState({class: STATUS.NORMAL}); - } + }; render() { return ( diff --git a/package.json b/package.json index 495b73c3f80f..ba5cc0403165 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "private": true, "devDependencies": { "@babel/core": "^7.3.4", + "@babel/plugin-proposal-class-properties": "^7.3.4", "@babel/plugin-transform-modules-commonjs": "^7.1.0", "@babel/plugin-transform-strict-mode": "^7.0.0", "@babel/preset-env": "^7.1.0", diff --git a/packages/jest-core/src/TestSequencer.ts b/packages/jest-core/src/TestSequencer.ts index 3468dfabde4e..fc1ef479c517 100644 --- a/packages/jest-core/src/TestSequencer.ts +++ b/packages/jest-core/src/TestSequencer.ts @@ -32,11 +32,7 @@ type Cache = { * is called to store/update this information on the cache map. */ export default class TestSequencer { - private _cache: Map; - - constructor() { - this._cache = new Map(); - } + private _cache: Map = new Map(); _getCachePath(context: Context) { const {config} = context; diff --git a/packages/jest-core/src/plugins/update_snapshots_interactive.ts b/packages/jest-core/src/plugins/update_snapshots_interactive.ts index 2984a6524113..5670d1f84dbc 100644 --- a/packages/jest-core/src/plugins/update_snapshots_interactive.ts +++ b/packages/jest-core/src/plugins/update_snapshots_interactive.ts @@ -11,16 +11,11 @@ import {BaseWatchPlugin, JestHookSubscriber} from 'jest-watcher'; import SnapshotInteractiveMode from '../SnapshotInteractiveMode'; class UpdateSnapshotInteractivePlugin extends BaseWatchPlugin { - private _snapshotInteractiveMode: SnapshotInteractiveMode; - private _failedSnapshotTestAssertions: Array; - isInternal: true; - - constructor(options: {stdin: NodeJS.ReadStream; stdout: NodeJS.WriteStream}) { - super(options); - this._failedSnapshotTestAssertions = []; - this._snapshotInteractiveMode = new SnapshotInteractiveMode(this._stdout); - this.isInternal = true; - } + private _snapshotInteractiveMode: SnapshotInteractiveMode = new SnapshotInteractiveMode( + this._stdout, + ); + private _failedSnapshotTestAssertions: Array = []; + isInternal: true = true; getFailedSnapshotTestAssertions( testResults: AggregatedResult, diff --git a/packages/jest-watcher/src/lib/Prompt.ts b/packages/jest-watcher/src/lib/Prompt.ts index 46612eb60302..88422ed62a01 100644 --- a/packages/jest-watcher/src/lib/Prompt.ts +++ b/packages/jest-watcher/src/lib/Prompt.ts @@ -29,13 +29,11 @@ export default class Prompt { this._onChange = () => {}; this._onSuccess = () => {}; this._onCancel = () => {}; - - this._onResize = this._onResize.bind(this); } - private _onResize() { + private _onResize = () => { this._onChange(); - } + }; enter( onChange: (pattern: string, options: ScrollOptions) => void, diff --git a/yarn.lock b/yarn.lock index 62c92cfecf64..c4ccf489ec36 100644 --- a/yarn.lock +++ b/yarn.lock @@ -279,7 +279,7 @@ "@babel/helper-remap-async-to-generator" "^7.1.0" "@babel/plugin-syntax-async-generators" "^7.2.0" -"@babel/plugin-proposal-class-properties@^7.0.0": +"@babel/plugin-proposal-class-properties@^7.0.0", "@babel/plugin-proposal-class-properties@^7.3.4": version "7.3.4" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.3.4.tgz#410f5173b3dc45939f9ab30ca26684d72901405e" integrity sha512-lUf8D3HLs4yYlAo8zjuneLvfxN7qfKv1Yzbj5vjqaqMJxgJA3Ipwp4VUJ+OrOdz53Wbww6ahwB8UhB2HQyLotA==