Skip to content

Commit

Permalink
Merge branch 'master' into jest-each-heading-validation
Browse files Browse the repository at this point in the history
* master: (30 commits)
  chore: verify TS project references are correct (jestjs#10941)
  chore(deps): bump actions/setup-node from v2.1.2 to v2.1.3 (jestjs#10940)
  docs: Rectify typo in tutorialReactNative (jestjs#10930)
  chore: patch react-native jest preprocessor to avoid warning
  Ensure `toContain` only accepts strings when `received` is a string (jestjs#10929)
  chore: update lockfile after publish
  v27.0.0-next.2
  Document and test custom, async, inline snapshot matcher (jestjs#10922)
  feat(transform): pass config options through to transformer (jestjs#10926)
  chore: bump eslint-config-prettier
  chore: run prettier using eslint
  chore: update lockfile after publish
  v27.0.0-next.1
  fix: move binary file declaration from runtime to repl (jestjs#10925)
  chore(test-result): remove deprecated `sourcemap` property (jestjs#10355)
  chore: remove mapCoverage remainings; remove deprecated CLI options test (jestjs#9968)
  refactor(jest-runtime,jest-transform): add readonly for some class fields (jestjs#10918)
  chore: ensure single environment package as well
  chore: fix failing tests (jestjs#10924)
  chore: fix lint warning
  ...
  • Loading branch information
jeysal committed Dec 13, 2020
2 parents 61bb0d0 + 15ca29c commit 3b674d4
Show file tree
Hide file tree
Showing 131 changed files with 1,314 additions and 847 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.js
Expand Up @@ -245,7 +245,7 @@ module.exports = {
'**/__tests__/**',
'**/__performance_tests__/**',
'packages/diff-sequences/perf/index.js',
'packages/pretty-format/perf/test.js'
'packages/pretty-format/perf/test.js',
],
rules: {
'import/no-unresolved': 'off',
Expand All @@ -258,7 +258,7 @@ module.exports = {
parserOptions: {
sourceType: 'module',
},
plugins: ['markdown', 'import', 'jest'],
plugins: ['markdown', 'import', 'jest', 'prettier'],
rules: {
'accessor-pairs': ['warn', {setWithoutGet: true}],
'block-scoped-var': 'off',
Expand Down Expand Up @@ -442,6 +442,7 @@ module.exports = {
'prefer-arrow-callback': ['error', {allowNamedFunctions: true}],
'prefer-const': 'error',
'prefer-template': 'off',
'prettier/prettier': 'error',
quotes: [
'error',
'single',
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Expand Up @@ -32,7 +32,7 @@ jobs:
key: ubuntu-latest-node-12.x-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
ubuntu-latest-node-12.x-yarn-
- uses: actions/setup-node@v2.1.2
- uses: actions/setup-node@v2.1.3
with:
node-version: 12.x
- name: install
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}-yarn-
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2.1.2
uses: actions/setup-node@v2.1.3
with:
node-version: ${{ matrix.node-version }}
- name: install
Expand Down Expand Up @@ -117,7 +117,7 @@ jobs:
restore-keys: |
${{ runner.os }}-node-14.x-yarn-
- name: Use Node.js 14.x
uses: actions/setup-node@v2.1.2
uses: actions/setup-node@v2.1.3
with:
node-version: 14.x
- name: install
Expand Down
11 changes: 10 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,17 +5,21 @@
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
- `[jest-config, jest-runtime]` [**BREAKING**] Use "modern" implementation as default for fake timers ([#10874](https://github.com/facebook/jest/pull/10874))
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
- `[jest-runner]` [**BREAKING**] Run transforms over `runnner` ([#8823](https://github.com/facebook/jest/pull/8823))
- `[jest-runner]` [**BREAKING**] Run transforms over `testRunnner` ([#8823](https://github.com/facebook/jest/pull/8823))
- `[jest-runtime, jest-transform]` share `cacheFS` between runtime and transformer ([#10901](https://github.com/facebook/jest/pull/10901))
- `[jest-transform]` Pass config options defined in Jest's config to transformer's `process` and `getCacheKey` functions ([#10926](https://github.com/facebook/jest/pull/10926))

### Fixes

- `[babel-plugin-jest-hoist]` Add `__dirname` and `__filename` to whitelisted globals ([#10903](https://github.com/facebook/jest/pull/10903))
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
- `[expect]` [**BREAKING**] Make `toContain` more strict with the received type ([#10119](https://github.com/facebook/jest/pull/10119) & [#10929](https://github.com/facebook/jest/pull/10929))
- `[jest-circus]` Fixed the issue of beforeAll & afterAll hooks getting executed even if it is inside a skipped `describe` block [#10451](https://github.com/facebook/jest/issues/10451)
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
Expand All @@ -35,24 +39,29 @@
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
- `[jest-transform]` [**BREAKING**] Refactor API of transformers to pass an options bag rather than separate `config` and other options ([#10834](https://github.com/facebook/jest/pull/10834))
- `[jest-worker]` [**BREAKING**] Use named exports ([#10623] (https://github.com/facebook/jest/pull/10623))
- `[pretty-format]` [**BREAKING**] Convert to ES Modules ([#10515](https://github.com/facebook/jest/pull/10515))

### Chore & Maintenance

- `[*]` [**BREAKING**] Only support Node LTS releases and Node 15 ([#10685](https://github.com/facebook/jest/pull/10685))
- `[*]` [**BREAKING**] Add `exports` field to all `package.json`s ([#9921](https://github.com/facebook/jest/pull/9921))
- `[*]` Make it easier for Jest's packages to use the VM escape hatch ([#10824](https://github.com/facebook/jest/pull/10824))
- `[*]` [**BREAKING**] Remove deprecated `mapCoverage` ([#9968](https://github.com/facebook/jest/pull/9968))
- `[jest-config]` [**BREAKING**] Remove `enabledTestsMap` config, use `filter` instead ([#10787](https://github.com/facebook/jest/pull/10787))
- `[jest-console]` [**BREAKING**] Move `root` into `config` and take `GlobalConfig` as mandatory parameter for `getConsoleOutput` ([#10126](https://github.com/facebook/jest/pull/10126))
- `[jest-fake-timers]` Clarify global behavior of `jest.useFakeTimers` and `jest.useRealTimers` ([#10867](https://github.com/facebook/jest/pull/10867))
- `[jest-haste-map]` [**BREAKING**] Migrate to ESM ([#10875](https://github.com/facebook/jest/pull/10875))
- `[jest-haste-map]` [**BREAKING**] Remove support for deprecated option `ignorePattern` as function ([#10348](https://github.com/facebook/jest/pull/10348))
- `[jest-jasmine2]` [**BREAKING**] Migrate to ESM ([#10906](https://github.com/facebook/jest/pull/10906))
- `[jest-repl, jest-runtime]` [**BREAKING**] Move the `jest-runtime` CLI into `jest-repl` ([#10016](https://github.com/facebook/jest/pull/10016))
- `[jest-repl, jest-runtime]` [**BREAKING**] Move the `jest-runtime` CLI into `jest-repl` ([#10016](https://github.com/facebook/jest/pull/10016) & [#10925](https://github.com/facebook/jest/pull/10925))
- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))
- `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876))
- `[jest-mock]` [**BREAKING**] Migrate to ESM ([#10887](https://github.com/facebook/jest/pull/10887))
- `[jest-runner]` [**BREAKING**] Migrate to ESM ([#10900](https://github.com/facebook/jest/pull/10900))
- `[jest-runtime]` [**BREAKING**] Remove deprecated and unnused `getSourceMapInfo` from Runtime ([#9969](https://github.com/facebook/jest/pull/9969))
- `[jest-util]` No longer checking `enumerable` when adding `process.domain` ([#10862](https://github.com/facebook/jest/pull/10862))
- `[jest-validate]` [**BREAKING**] Remove `recursiveBlacklist ` option in favor of previously introduced `recursiveDenylist` ([#10650](https://github.com/facebook/jest/pull/10650))

### Performance

Expand Down
8 changes: 5 additions & 3 deletions docs/CodeTransformation.md
Expand Up @@ -35,17 +35,17 @@ interface Transformer<OptionType = unknown> {
getCacheKey?: (
sourceText: string,
sourcePath: string,
options: TransformOptions,
options: TransformOptions<OptionType>,
) => string;

process: (
sourceText: string,
sourcePath: string,
options: TransformOptions,
options: TransformOptions<OptionType>,
) => TransformedSource;
}

interface TransformOptions {
interface TransformOptions<OptionType> {
/**
* If a transformer does module resolution and reads files, it should populate `cacheFS` so that
* Jest avoids reading the same files again, improving performance. `cacheFS` stores entries of
Expand All @@ -61,6 +61,8 @@ interface TransformOptions {
supportsExportNamespaceFrom: boolean;
supportsStaticESM: boolean;
supportsTopLevelAwait: boolean;
/** the options passed through Jest's config by the user */
transformerConfig: OptionType;
}

type TransformedSource =
Expand Down
35 changes: 35 additions & 0 deletions docs/ExpectAPI.md
Expand Up @@ -255,6 +255,41 @@ it('stores only 10 characters', () => {
});
```

#### async

If your custom inline snapshot matcher is async i.e. uses `async`-`await` you might encounter an error like "Multiple inline snapshots for the same call are not supported". Jest needs additional context information to find where the custom inline snapshot matcher was used to update the snapshots properly.

```js
const {toMatchInlineSnapshot} = require('jest-snapshot');

expect.extend({
async toMatchObservationInlineSnapshot(fn, ...rest) {
// The error (and its stacktrace) must be created before any `await`
this.error = new Error();

// The implementation of `observe` doesn't matter.
// It only matters that the custom snapshot matcher is async.
const observation = await observe(async () => {
await fn();
});

return toMatchInlineSnapshot.call(this, recording, ...rest);
},
});

it('observes something', async () => {
await expect(async () => {
return 'async action';
}).toMatchTrimmedInlineSnapshot();
/*
The snapshot will be added inline like
await expect(async () => {
return 'async action';
}).toMatchTrimmedInlineSnapshot(`"async action"`);
*/
});
```

### `expect.anything()`

`expect.anything()` matches anything but `null` or `undefined`. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a non-null argument:
Expand Down
4 changes: 2 additions & 2 deletions docs/JestObjectAPI.md
Expand Up @@ -582,9 +582,9 @@ Restores all mocks back to their original value. Equivalent to calling [`.mockRe

### `jest.useFakeTimers(implementation?: 'modern' | 'legacy')`

Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate`).
Instructs Jest to use fake versions of the standard timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`, `nextTick`, `setImmediate` and `clearImmediate` as well as `Date`).

If you pass `'modern'` as an argument, [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) will be used as implementation instead of Jest's own fake timers. This also mocks additional timers like `Date`. `'modern'` will be the default behavior in Jest 27.
If you pass `'legacy'` as an argument, Jest's legacy implementation will be used rather than one based on [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers).

Returns the `jest` object for chaining.

Expand Down
2 changes: 1 addition & 1 deletion docs/TutorialReactNative.md
Expand Up @@ -192,7 +192,7 @@ Or if you'd like to create your own manual mock, you can do something like this:
```js
jest.mock('Text', () => {
const RealComponent = jest.requireActual('Text');
const React = require('React');
const React = require('react');
class Text extends React.Component {
render() {
return React.createElement('Text', this.props, this.props.children);
Expand Down
@@ -0,0 +1,93 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`works with custom inline snapshot matchers 1`] = `
FAIL __tests__/asynchronous.test.js
✕ new async, inline snapshots
✕ mismatching async, inline snapshots
● new async, inline snapshots
expect(received).toMatchInlineSnapshot()
Snapshot name: \`new async, inline snapshots 1\`
New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.
This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.
Received: "result #1"
20 |
21 | test('new async, inline snapshots', async () => {
> 22 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot();
| ^
23 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot();
24 | });
25 |
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:22:41)
● new async, inline snapshots
expect(received).toMatchInlineSnapshot()
Snapshot name: \`new async, inline snapshots 2\`
New snapshot was not written. The update flag must be explicitly passed to write a new snapshot.
This is likely because this test is run in a continuous integration (CI) environment in which snapshots are not written by default.
Received: "result #2"
21 | test('new async, inline snapshots', async () => {
22 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot();
> 23 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot();
| ^
24 | });
25 |
26 | test('mismatching async, inline snapshots', async () => {
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:23:41)
mismatching async, inline snapshots
expect(received).toMatchInlineSnapshot(snapshot)
Snapshot name: \`mismatching async, inline snapshots 1\`
Snapshot: "result #?"
Received: "result #1"
25 |
26 | test('mismatching async, inline snapshots', async () => {
> 27 | await expect(async () => 'result #1').toMatchObservationInlineSnapshot(
| ^
28 | \`"result #?"\`,
29 | );
30 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot(
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:27:41)
● mismatching async, inline snapshots
expect(received).toMatchInlineSnapshot(snapshot)
Snapshot name: \`mismatching async, inline snapshots 2\`
Snapshot: "result #?"
Received: "result #2"
28 | \`"result #?"\`,
29 | );
> 30 | await expect(async () => 'result #2').toMatchObservationInlineSnapshot(
| ^
31 | \`"result #?"\`,
32 | );
33 | });
at Object.toMatchObservationInlineSnapshot (__tests__/asynchronous.test.js:30:41)
› 4 snapshots failed.
Snapshot Summary
› 4 snapshots failed from 1 test suite. Inspect your code changes or re-run jest with \`-u\` to update them.
`;
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/transform.test.ts.snap
Expand Up @@ -6,7 +6,7 @@ FAIL __tests__/ignoredFile.test.js
babel-jest: Babel ignores __tests__/ignoredFile.test.js - make sure to include the file in Jest's transformIgnorePatterns as well.
at loadBabelConfig (../../../packages/babel-jest/build/index.js:201:13)
at loadBabelConfig (../../../packages/babel-jest/build/index.js:190:13)
`;

exports[`babel-jest instruments only specific files and collects coverage 1`] = `
Expand Down
27 changes: 27 additions & 0 deletions e2e/__tests__/customInlineSnapshotMatchers.test.ts
@@ -0,0 +1,27 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {wrap} from 'jest-snapshot-serializer-raw';
import {extractSummary} from '../Utils';
import runJest from '../runJest';

test('works with custom inline snapshot matchers', () => {
const {stderr} = runJest('custom-inline-snapshot-matchers', [
// Prevent adding new snapshots or rather changing the test.
'--ci',
'asynchronous.test.js',
]);

let {rest} = extractSummary(stderr);

rest = rest
.split('\n')
.filter(line => line.indexOf('at Error (native)') < 0)
.join('\n');

expect(wrap(rest)).toMatchSnapshot();
});
25 changes: 0 additions & 25 deletions e2e/__tests__/deprecatedCliOptions.test.ts

This file was deleted.

33 changes: 33 additions & 0 deletions e2e/custom-inline-snapshot-matchers/__tests__/asynchronous.test.js
@@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const {toMatchInlineSnapshot} = require('jest-snapshot');

expect.extend({
async toMatchObservationInlineSnapshot(fn, ...args) {
this.error = new Error();
// This specific behavior can be implemented without a custom matcher.
// In a real example one might want to observe some global value that `fn()` is affecting.
// The difference between before and after `fn()` might then be persisted as a snapshot.
const result = await fn();

return toMatchInlineSnapshot.call(this, result, ...args);
},
});

test('new async, inline snapshots', async () => {
await expect(async () => 'result #1').toMatchObservationInlineSnapshot();
await expect(async () => 'result #2').toMatchObservationInlineSnapshot();
});

test('mismatching async, inline snapshots', async () => {
await expect(async () => 'result #1').toMatchObservationInlineSnapshot(
`"result #?"`,
);
await expect(async () => 'result #2').toMatchObservationInlineSnapshot(
`"result #?"`,
);
});
5 changes: 5 additions & 0 deletions e2e/custom-inline-snapshot-matchers/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}

0 comments on commit 3b674d4

Please sign in to comment.