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

[WIP] Upgrade entire suite to Babel 7 #6949

Merged
merged 46 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
5cb0df5
chore: fix linting error in contributing guide
SimenB Sep 15, 2018
8b1be33
Fix for Babel7 bridge package name in documentation. (#6745)
quisido Sep 15, 2018
a66a571
Fix a memory leak in Error objects (#6965)
sokra Sep 15, 2018
68eb9a2
chore: add runtimeArgs in VSCode launch.json (#6985)
trivikr Sep 16, 2018
7308bbc
chore: use useDots option in silent reporter (#6984)
SimenB Sep 16, 2018
5c8ec2f
Add badge of vulnerabilities from snyk.io (#6986)
trivikr Sep 16, 2018
67e4c82
[WIP] feat: option to not escape strings in diff messages (#5661)
maciej-ka Sep 17, 2018
3dd29cd
docs: Explain how to rewrite assertions to avoid large irrelevant dif…
pedrottimark Sep 17, 2018
628a3d1
Preserve info from files with matching SHA-1s (#6990)
rubennorte Sep 17, 2018
fb20ddd
fix(jest-haste-map): Fixed Haste whitelist generation for scoped modu…
empyrical Sep 17, 2018
2af1c62
chore: add example using babel 7 (#6983)
SimenB Sep 18, 2018
86fc23d
docs: Replace shallow equality with referential identity in `ExpectAP…
pedrottimark Sep 19, 2018
c3dc90f
refactor: use execa instead of child_process for VCS commands (#6987)
SimenB Sep 19, 2018
41e0da0
chore: bump package versions (#6978)
SimenB Sep 19, 2018
a5cdea2
docs: fix `rejects.toMatch` on `rejects.toThrow` (#6999)
nodkz Sep 19, 2018
1251008
Fix mocking inherited static properties and prototype-less objects (#…
rubennorte Sep 20, 2018
3c39c40
chore: upgrade eslint
SimenB Sep 20, 2018
ab06a7b
Fix indentation of template literals in test (#7008)
rubennorte Sep 20, 2018
ee751bf
Added method to get an iterator for all files in HasteFS (#7010)
rubennorte Sep 20, 2018
0b77f78
Start babel migration.
milesj Sep 5, 2018
c27bcbe
More fixes.
milesj Sep 5, 2018
2a95623
More things.
milesj Sep 5, 2018
3910080
Fix RN.
milesj Sep 5, 2018
a228ba6
More updates.
milesj Sep 5, 2018
fa41c1f
Update configs.
Sep 10, 2018
7ea038b
Update RN support.
Sep 10, 2018
c7232be
Rework Babel config loading.
Sep 10, 2018
3b0f790
Update deps.
Sep 10, 2018
c1a9a2c
Update some deps.
Sep 10, 2018
a28d236
Fix rollup build.
Sep 10, 2018
7dd89ee
Remove browser build plugin.
Sep 10, 2018
dde23de
Target IE8.
Sep 10, 2018
0e6cfda
Update to beta RN.
Sep 10, 2018
dab8e57
Parser updates.
Sep 10, 2018
9abd68b
Upgrade istanbul.
Sep 10, 2018
141c901
Rework lazy.
Sep 10, 2018
ddb7d28
More work and polish.
Sep 11, 2018
6f7d4ab
Use transformSync.
Sep 12, 2018
765a38b
Update react-native.
Sep 12, 2018
3927ce4
Fix RN tests.
Sep 12, 2018
34e0e44
Final fixes.
milesj Sep 16, 2018
e0cd570
Minor changes.
milesj Sep 16, 2018
826f29f
Use babel for TS.
milesj Sep 16, 2018
0deb91e
Update deps. Run install.
milesj Sep 21, 2018
294ea06
Update editor support.
milesj Sep 21, 2018
8e3a39d
Add TS argument.
milesj Sep 21, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions .babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ untyped-import
untyped-type-import

[version]
^0.75.0
^0.80.0
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"request": "launch",
"name": "Debug Jest with current test file",
"program": "${workspaceFolder}/packages/jest-cli/bin/jest.js",
"args": ["--runInBand", "${file}"]
"args": ["--runInBand", "${file}"],
"runtimeArgs": ["-r", "flow-remove-types/register"]
}
]
}
23 changes: 21 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
## master

### Features

- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))
- `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)).

### Fixes

- `[jest-haste-map]` [**BREAKING**] Replaced internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960))
- `[jest-haste-map]` [**BREAKING**] Replace internal data structures to improve performance ([#6960](https://github.com/facebook/jest/pull/6960))
- `[jest-haste-map]` Do not visit again files with the same sha-1 ([#6990](https://github.com/facebook/jest/pull/6990))
- `[jest-jasmine2]` Fix memory leak in Error objects hold by the framework ([#6965](https://github.com/facebook/jest/pull/6965))
- `[jest-haste-map]` Fixed Haste whitelist generation for scoped modules on Windows ([#6980](https://github.com/facebook/jest/pull/6980))
- `[jest-mock]` Fix inheritance of static properties and methods in mocks ([#7003](https://github.com/facebook/jest/pull/7003))
- `[jest-mock]` Fix mocking objects without `Object.prototype` in their prototype chain ([#7003](https://github.com/facebook/jest/pull/7003))

### Chore & Maintenance

- `[docs]` Fix babel-core installation instructions ([#6745](https://github.com/facebook/jest/pull/6745))
- `[docs]` Explain how to rewrite assertions to avoid large irrelevant diff ([#6971](https://github.com/facebook/jest/pull/6971))
- `[examples]` add example using Babel 7 ([#6983](https://github.com/facebook/jest/pull/6983))
- `[docs]` Replace shallow equality with referential identity in `ExpectAPI.md` ([#6991](https://github.com/facebook/jest/pull/6991))
- `[jest-changed-files]` Refactor to use `execa` over `child_process` ([#6987](https://github.com/facebook/jest/pull/6987))
- `[*]` Bump dated dependencies ([#6978](https://github.com/facebook/jest/pull/6978))

## 23.6.0

### Features

- `[jest-ci]` Add `changedSince` to allowed watch mode configs ([#6955](https://github.com/facebook/jest/pull/6955))
- `[jest-cli]` Add `changedSince` to allowed watch mode configs ([#6955](https://github.com/facebook/jest/pull/6955))
- `[babel-jest]` Add support for `babel.config.js` added in Babel 7.0.0 ([#6911](https://github.com/facebook/jest/pull/6911))

### Fixes
Expand Down
47 changes: 24 additions & 23 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ _Before_ submitting a pull request, please make sure the following is done…

Open terminal (e.g. Terminal, iTerm, Git Bash or Git Shell) and type:

```sh
git clone https://github.com/<your_username>/jest
cd jest
git checkout -b my_branch
```sh-session
$ git clone https://github.com/<your_username>/jest
$ cd jest
$ git checkout -b my_branch
```

Note: Replace `<your_username>` with your GitHub username
Expand All @@ -37,7 +37,8 @@ _Before_ submitting a pull request, please make sure the following is done…
1. Make sure you have `python` installed (v2.7 is recommended, v3.x.x is not supported). Python is required by [node-gyp](https://github.com/nodejs/node-gyp) that is used when running `yarn install`.

To check your version of Python and ensure it's installed you can type:
```

```sh
python --version
```

Expand All @@ -64,9 +65,9 @@ _Before_ submitting a pull request, please make sure the following is done…

1. Ensure the test suite passes via `yarn test`. To run the test suite you may need to install [Mercurial](https://www.mercurial-scm.org/) (`hg`). On macOS, this can be done using [homebrew](http://brew.sh/): `brew install hg`.

```sh
brew install hg # maybe
yarn test
```sh-session
$ brew install hg # maybe
$ yarn test
```

1. If you haven't already, complete the CLA.
Expand All @@ -76,10 +77,10 @@ _Before_ submitting a pull request, please make sure the following is done…
If you are making changes to the website or documentation, test the website folder and run the server to check if your changes are being displayed accurately.

1. Locate to the website directory and install any website specific dependencies by typing in `yarn`. Following steps are to be followed for this purpose from the root directory.
```sh
cd website # Only needed if you are not already in the website directory
yarn
yarn start
```sh-session
$ cd website # Only needed if you are not already in the website directory
$ yarn
$ yarn start
```
1. You can run a development server to check if the changes you made are being displayed accurately by running `yarn start` in the website directory.

Expand All @@ -93,34 +94,34 @@ In order to accept your pull request, we need you to submit a CLA. You only need

To link `jest` on the command line to `jest-cli/bin/jest.js` in a development build:

```sh
cd /path/to/your/Jest_clone/packages/jest-cli
yarn link
```sh-session
$ cd /path/to/your/Jest_clone/packages/jest-cli
$ yarn link
```

To build Jest:

```sh
cd /path/to/your/Jest_clone
```sh-session
$ cd /path/to/your/Jest_clone

# Do one of the following:

# Check out a commit from another contributor, and then
yarn run build
$ yarn run build

# Or, save your changes to Jest, and then
yarn test # which also builds Jest
$ yarn test # which also builds Jest
```

To run tests in another project with the development build of Jest:

```sh
cd /path/to/another/project
```sh-session
$ cd /path/to/another/project

# link development build to the other project
yarn link jest-cli
$ yarn link jest-cli

jest [options] # run jest-cli/bin/jest.js in the development build
$ jest [options] # run jest-cli/bin/jest.js in the development build
```

- To decide whether to specify any options, see `test` under `scripts` in the `package.json` file of the other project.
Expand Down
36 changes: 10 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<a href="#backers"><img src="https://opencollective.com/jest/backers/badge.svg" alt="Backers on Open Collective"></a>
<a href="#sponsors"><img src="https://opencollective.com/jest/sponsors/badge.svg" alt="Sponsors on Open Collective"></a>
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a>
<a href="https://snyk.io/test/github/facebook/jest?targetFile=packages/jest/package.json"><img src="https://snyk.io/test/github/facebook/jest/badge.svg?targetFile=packages/jest/package.json" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/facebook/jest?targetFile=packages/jest/package.json" style="max-width:100%;"></a>
</p>

**👩🏻‍💻 Developer Ready**: Complete and ready to set-up JavaScript testing solution. Works out of the box for any React project.
Expand Down Expand Up @@ -99,39 +100,22 @@ If you'd like to learn more about running `jest` through the command line, take

### Using Babel

[Babel](http://babeljs.io/) is automatically handled by Jest using `babel-jest`. You don't need install anything extra for using Babel.
To use [Babel](http://babeljs.io/), install the `babel-jest` and `@babel/core` packages:

> Note: If you are using a babel version 7 then you need to install `babel-core@^7.0.0-0` and `@babel/core` with the following command:
>
> ```bash
> yarn add --dev 'babel-core@^7.0.0-0' @babel/core
> ```
```bash
yarn add --dev babel-jest @babel/core
```

Don't forget to add a [`.babelrc`](https://babeljs.io/docs/usage/babelrc/) file in your project's root folder. For example, if you are using ES6 and [React.js](https://reactjs.org) with the [`babel-preset-env`](https://babeljs.io/docs/plugins/preset-env/) and [`babel-preset-react`](https://babeljs.io/docs/plugins/preset-react/) presets:
Don't forget to add a [`babel.config.js`](https://babeljs.io/docs/en/config-files) file in your project's root folder. For example, if you are using ES6 and [React.js](https://reactjs.org) with the [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) and [`@babel/preset-react`](https://babeljs.io/docs/en/babel-preset-react) presets:

```json
{
"presets": ["env", "react"]
}
```js
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
};
```

You are now set up to use all ES6 features and React specific syntax.

> Note: If you are using a more complicated Babel configuration, using Babel's `env` option, keep in mind that Jest will automatically define `NODE_ENV` as `test`. It will not use `development` section like Babel does by default when no `NODE_ENV` is set.

> Note: If you've turned off transpilation of ES modules with the option `{ "modules": false }`, you have to make sure to turn this on in your test environment.

```json
{
"presets": [["env", {"modules": false}], "react"],
"env": {
"test": {
"presets": [["env"], "react"]
}
}
}
```

> Note: `babel-jest` is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the `transform` configuration option:

```json
Expand Down
22 changes: 22 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
// babelrcRoots: ['.', 'packages/*', 'examples/*'],
plugins: [
// Required by some examples, like react-native. Without this plugin,
// they will simply crash before relative configs can be found.
// '@babel/plugin-proposal-class-properties',
['@babel/plugin-transform-modules-commonjs', {allowTopLevelThis: true}],
],
presets: [
[
'@babel/preset-env',
{
shippedProposals: true,
targets: {node: '6'},
Copy link
Author

Choose a reason for hiding this comment

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

What's the lowest supported node version for Jest?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's 6 now.

useBuiltIns: 'usage',
},
],
'@babel/preset-flow',
// Some tests contain JSX
'@babel/preset-react',
],
};
2 changes: 1 addition & 1 deletion docs/Es6ClassMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ jest.mock('./sound-player', () => {
});
```

This will throw **_TypeError: \_soundPlayer2.default is not a constructor_**, unless the code is transpiled to ES5, e.g. by babel-preset-env. (ES5 doesn't have arrow functions nor classes, so both will be transpiled to plain functions.)
This will throw **_TypeError: \_soundPlayer2.default is not a constructor_**, unless the code is transpiled to ES5, e.g. by `@babel/preset-env`. (ES5 doesn't have arrow functions nor classes, so both will be transpiled to plain functions.)

## Keeping track of usage (spying on the mock)

Expand Down
18 changes: 15 additions & 3 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ test('rejects to octopus', async () => {

### `.toBe(value)`

`toBe` just checks that a value is what you expect. It uses `Object.is` to check exact equality.
Use `.toBe` to compare primitive values or to check referential identity of object instances. It calls `Object.is` to compare values, which is even better for testing than `===` strict equality operator.

For example, this code will validate some properties of the `can` object:

Expand All @@ -533,7 +533,12 @@ describe('the can', () => {
});
```

Don't use `toBe` with floating-point numbers. For example, due to rounding, in JavaScript `0.2 + 0.1` is not strictly equal to `0.3`. If you have floating point numbers, try `.toBeCloseTo` instead.
Don't use `.toBe` with floating-point numbers. For example, due to rounding, in JavaScript `0.2 + 0.1` is not strictly equal to `0.3`. If you have floating point numbers, try `.toBeCloseTo` instead.

Although the `.toBe` matcher **checks** referential identity, it **reports** a deep comparison of values if the assertion fails. If differences between properties do not help you to understand why a test fails, especially if the report is large, then you might move the comparison into the `expect` function. For example, to assert whether or not elements are the same instance:

- rewrite `expect(received).toBe(expected)` as `expect(Object.is(received, expected)).toBe(true)`
- rewrite `expect(received).not.toBe(expected)` as `expect(Object.is(received, expected)).toBe(false)`

### `.toHaveBeenCalled()`

Expand Down Expand Up @@ -906,7 +911,9 @@ describe('my beverage', () => {

### `.toEqual(value)`

Use `.toEqual` when you want to check that two objects have the same value. This matcher recursively checks the equality of all fields, rather than checking for object identity—this is also known as "deep equal". For example, `toEqual` and `toBe` behave differently in this test suite, so all the tests pass:
Use `.toEqual` to compare recursively all properties of object instances (also known as "deep" equality). It calls `Object.is` to compare primitive values, which is even better for testing than `===` strict equality operator.

For example, `.toEqual` and `.toBe` behave differently in this test suite, so all the tests pass:

```js
const can1 = {
Expand All @@ -930,6 +937,11 @@ describe('the La Croix cans on my desk', () => {

> Note: `.toEqual` won't perform a _deep equality_ check for two errors. Only the `message` property of an Error is considered for equality. It is recommended to use the `.toThrow` matcher for testing against errors.

If differences between properties do not help you to understand why a test fails, especially if the report is large, then you might move the comparison into the `expect` function. For example, use `equals` method of `Buffer` class to assert whether or not buffers contain the same content:

- rewrite `expect(received).toEqual(expected)` as `expect(received.equals(expected)).toBe(true)`
- rewrite `expect(received).not.toEqual(expected)` as `expect(received.equals(expected)).toBe(false)`

### `.toHaveLength(number)`

Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value.
Expand Down
37 changes: 7 additions & 30 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,45 +79,22 @@ jest --init

### Using Babel

To use [Babel](http://babeljs.io/), install the `babel-jest` and `regenerator-runtime` packages:
To use [Babel](http://babeljs.io/), install the `babel-jest` and `@babel/core` packages:

```bash
yarn add --dev babel-jest babel-core regenerator-runtime
yarn add --dev babel-jest @babel/core
```

> Note: If you are using a babel version 7 then you need to install `babel-jest` with the following command:
>
> ```bash
> yarn add --dev babel-jest 'babel-core@^7.0.0-0' @babel/core regenerator-runtime
> ```
Don't forget to add a [`babel.config.js`](https://babeljs.io/docs/en/config-files) file in your project's root folder. For example, if you are using ES6 and [React.js](https://reactjs.org) with the [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) and [`@babel/preset-react`](https://babeljs.io/docs/en/babel-preset-react) presets:

_Note: Explicitly installing `regenerator-runtime` is not needed if you use `npm` 3 or 4 or Yarn_

Don't forget to add a [`.babelrc`](https://babeljs.io/docs/usage/babelrc/) file in your project's root folder. For example, if you are using ES6 and [React.js](https://facebook.github.io/react/) with the [`babel-preset-env`](https://babeljs.io/docs/plugins/preset-env/) and [`babel-preset-react`](https://babeljs.io/docs/plugins/preset-react/) presets:

```json
{
"presets": ["env", "react"]
}
```js
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
};
```

You are now set up to use all ES6 features and React specific syntax.

> Note: If you are using a more complicated Babel configuration, using Babel's `env` option, keep in mind that Jest will automatically define `NODE_ENV` as `test`. It will not use `development` section like Babel does by default when no `NODE_ENV` is set.

> Note: If you've turned off transpilation of ES6 modules with the option `{ "modules": false }`, you have to make sure to turn this on in your test environment.

```json
{
"presets": [["env", {"modules": false}], "react"],
"env": {
"test": {
"presets": [["env"], "react"]
}
}
}
```

> Note: `babel-jest` is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the `transform` configuration option:

```json
Expand Down
2 changes: 1 addition & 1 deletion docs/TestingAsyncCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ test('the data is peanut butter', async () => {

test('the fetch fails with an error', async () => {
expect.assertions(1);
await expect(fetchData()).rejects.toMatch('error');
await expect(fetchData()).rejects.toThrow('error');
});
```

Expand Down
2 changes: 1 addition & 1 deletion docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ If you are using Facebook's [`create-react-app`](https://github.com/facebookincu

## Caching Issues

The transform script was changed or babel was updated and the changes aren't being recognized by Jest?
The transform script was changed or Babel was updated and the changes aren't being recognized by Jest?

Retry with [`--no-cache`](CLI.md#cache). Jest caches transformed module files to speed up test execution. If you are using your own custom transformer, consider adding a `getCacheKey` function to it: [getCacheKey in Relay](https://github.com/facebook/relay/blob/58cf36c73769690f0bbf90562707eadb062b029d/scripts/jest/preprocessor.js#L56-L61).

Expand Down
2 changes: 1 addition & 1 deletion docs/TutorialAsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ it('works with async/await and resolves', async () => {
});
```

To enable async/await in your project, install [`babel-preset-env`](http://babeljs.io/docs/plugins/preset-env/) and enable the feature in your `.babelrc` file.
To enable async/await in your project, install [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env) and enable the feature in your `babel.config.js` file.

## Error handling

Expand Down