Skip to content

Commit

Permalink
Merge branch 'main' into patch
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 12, 2023
2 parents bf0e377 + bc26cd7 commit cddd949
Show file tree
Hide file tree
Showing 98 changed files with 5,397 additions and 4,698 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/close-stale.yml
@@ -1,7 +1,7 @@
name: 'Close stale issues and PRs'
on:
schedule:
- cron: '*/10 * * * *'
- cron: '0 * * * *'

permissions:
issues: write # to close stale issues (actions/stale)
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,22 +2,27 @@

### Features

- `[jest-cli]` Include type definitions to generated config files ([#14078](https://github.com/facebook/jest/pull/14078))
- `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070))
- `[jest-snapshot]` Support arrays as property matchers ([#14025](https://github.com/facebook/jest/pull/14025))

### Fixes

- `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054))
- `[jest-config]` Allow loading `jest.config.cts` files ([#14070](https://github.com/facebook/jest/pull/14070))
- `[jest-core]` Always use workers in watch mode to avoid crashes ([#14059](https://github.com/facebook/jest/pull/14059)).
- `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989))
- `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007))
- `[jest-mock]` Tweak typings to allow `jest.replaceProperty()` replace methods ([#14008](https://github.com/facebook/jest/pull/14008))
- `[jest-mock]` Improve user input validation and error messages of `spyOn` and `replaceProperty` methods ([#14087](https://github.com/facebook/jest/pull/14087))
- `[jest-runtime]` Bind `jest.isolateModulesAsync` to `this` ([#14083](https://github.com/facebook/jest/pull/14083))
- `[jest-snapshot]` Fix a potential bug when not using prettier and improve performance ([#14036](https://github.com/facebook/jest/pull/14036))
- `[@jest/transform]` Do not instrument `.json` modules ([#14048](https://github.com/facebook/jest/pull/14048))

### Chore & Maintenance

- `[docs]` Updated documentation for the `--runTestsByPath` CLI command ([#14004](https://github.com/facebook/jest/pull/14004))
- `[docs]` Updated documentation regarding the synchronous fallback when asynchronous code transforms are unavailable ([#14056](https://github.com/facebook/jest/pull/14056))
- `[docs]` Update jest statistics of use and downloads in website Index.

### Performance

Expand Down
22 changes: 17 additions & 5 deletions docs/JestObjectAPI.md
Expand Up @@ -1039,30 +1039,42 @@ Use the [`--showSeed`](CLI.md#--showseed) flag to print the seed in the test rep

Returns `true` if test environment has been torn down.

### `jest.retryTimes(numRetries, options)`
### `jest.retryTimes(numRetries, options?)`

Runs failed tests n-times until they pass or until the max number of retries is exhausted. `options` are optional. This only works with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner! This must live at the top-level of a test file or in a describe block. Retries _will not_ work if `jest.retryTimes()` is called in a `beforeEach` or a `test` block.

Example in a test:
Runs failed tests n-times until they pass or until the max number of retries is exhausted.

```js
jest.retryTimes(3);

test('will fail', () => {
expect(true).toBe(false);
});
```

If `logErrorsBeforeRetry` is enabled, Jest will log the error(s) that caused the test to fail to the console, providing visibility on why a retry occurred.
If `logErrorsBeforeRetry` option is enabled, error(s) that caused the test to fail will be logged to the console.

```js
jest.retryTimes(3, {logErrorsBeforeRetry: true});

test('will fail', () => {
expect(true).toBe(false);
});
```

Returns the `jest` object for chaining.

:::caution

`jest.retryTimes()` must be declared at the top level of a test file or in a `describe` block.

:::

:::info

This function is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.

:::

### `jest.setTimeout(timeout)`

Set the default timeout interval (in milliseconds) for all tests and before/after hooks in the test file. This only affects the test file from which this function is called. The default timeout interval is 5 seconds if this method is not called.
Expand Down
2 changes: 1 addition & 1 deletion docs/SnapshotTesting.md
Expand Up @@ -106,7 +106,7 @@ Once you're finished, Jest will give you a summary before returning back to watc

Inline snapshots behave identically to external snapshots (`.snap` files), except the snapshot values are written automatically back into the source code. This means you can get the benefits of automatically generated snapshots without having to switch to an external file to make sure the correct value was written.

**Example:**
Example:

First, you write a test, calling `.toMatchInlineSnapshot()` with no arguments:

Expand Down
35 changes: 2 additions & 33 deletions docs/TutorialReact.md
Expand Up @@ -209,9 +209,9 @@ React 16 triggers these warnings due to how it checks element types, and the moc

### DOM Testing

If you'd like to assert, and manipulate your rendered components you can use [react-testing-library](https://github.com/kentcdodds/react-testing-library), [Enzyme](https://enzymejs.github.io/enzyme/), or React's [TestUtils](https://reactjs.org/docs/test-utils.html). The following two examples use react-testing-library and Enzyme.
If you'd like to assert, and manipulate your rendered components you can use [@testing-library/react](https://github.com/testing-library/react-testing-library), [Enzyme](https://enzymejs.github.io/enzyme/), or React's [TestUtils](https://reactjs.org/docs/test-utils.html). The following example use `@testing-library/react`.

#### react-testing-library
#### @testing-library/react

```bash npm2yarn
npm install --save-dev @testing-library/react
Expand Down Expand Up @@ -261,37 +261,6 @@ it('CheckboxWithLabel changes the text after click', () => {

The code for this example is available at [examples/react-testing-library](https://github.com/facebook/jest/tree/main/examples/react-testing-library).

#### Enzyme

```bash npm2yarn
npm install --save-dev enzyme
```

If you are using a React version below 15.5.0, you will also need to install `react-addons-test-utils`.

Let's rewrite the test from above using Enzyme instead of react-testing-library. We use Enzyme's [shallow renderer](https://enzymejs.github.io/enzyme/docs/api/shallow.html) in this example.

```tsx title="__tests__/CheckboxWithLabel-test.js"
import Enzyme, {shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import CheckboxWithLabel from '../CheckboxWithLabel';

Enzyme.configure({adapter: new Adapter()});

it('CheckboxWithLabel changes the text after click', () => {
// Render a checkbox with label in the document
const checkbox = shallow(<CheckboxWithLabel labelOn="On" labelOff="Off" />);

expect(checkbox.text()).toBe('Off');

checkbox.find('input').simulate('change');

expect(checkbox.text()).toBe('On');
});
```

The code for this example is available at [examples/enzyme](https://github.com/facebook/jest/tree/main/examples/enzyme).

### Custom transformers

If you need more advanced functionality, you can also build your own transformer. Instead of using `babel-jest`, here is an example of using `@babel/core`:
Expand Down
Expand Up @@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
14 | });
15 | });
at eventHandler (../../packages/jest-circus/build/eventHandler.js:134:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:140:11)
at test (__tests__/asyncDefinition.test.js:12:5)
● Test suite failed to run
Expand All @@ -31,7 +31,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
15 | });
16 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:97:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:103:11)
at afterAll (__tests__/asyncDefinition.test.js:13:5)
● Test suite failed to run
Expand All @@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
20 | });
21 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:134:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:140:11)
at test (__tests__/asyncDefinition.test.js:18:3)
● Test suite failed to run
Expand All @@ -60,6 +60,6 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
20 | });
21 |
at eventHandler (../../packages/jest-circus/build/eventHandler.js:97:11)
at eventHandler (../../packages/jest-circus/build/eventHandler.js:103:11)
at afterAll (__tests__/asyncDefinition.test.js:19:3)"
`;
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/testFailingJasmine.test.ts.snap
Expand Up @@ -46,7 +46,7 @@ FAIL __tests__/worksWithConcurrentMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:166:11)
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:176:11)
at Suite.failing (__tests__/worksWithConcurrentMode.test.js:13:17)
at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)
Expand Down Expand Up @@ -80,7 +80,7 @@ FAIL __tests__/worksWithConcurrentOnlyMode.test.js
15 | });
16 |
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:166:11)
at Function.failing (../../packages/jest-jasmine2/build/jasmineAsyncInstall.js:176:11)
at Suite.failing (__tests__/worksWithConcurrentOnlyMode.test.js:13:22)
at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)
Expand Down
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`all 3 test files should complete 1`] = `
"Test Suites: 1 failed, 2 passed, 3 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
15 changes: 15 additions & 0 deletions e2e/__tests__/workerRestartBeforeSend.test.ts
@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {extractSummary} from '../Utils';
import runJest from '../runJest';

it('all 3 test files should complete', () => {
const result = runJest('worker-restart-before-send');
const {summary} = extractSummary(result.stderr);
expect(summary).toMatchSnapshot();
});

0 comments on commit cddd949

Please sign in to comment.