Skip to content

Commit

Permalink
Merge branch 'master' into require-after-teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Apr 9, 2018
2 parents 27aefaf + 89d98d3 commit 32c49f1
Show file tree
Hide file tree
Showing 40 changed files with 245 additions and 167 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### Features

* `[jest-config]` Export Jest's default options
([#5948](https://github.com/facebook/jest/pull/5948))
* `[jest-editor-support]` Move `coverage` to `ProjectWorkspace.collectCoverage`
([#5929](https://github.com/facebook/jest/pull/5929))
* `[jest-editor-support]` Add `coverage` option to runner
([#5836](https://github.com/facebook/jest/pull/5836))
* `[jest-haste-map]` Support extracting dynamic `import`s
Expand Down Expand Up @@ -42,12 +46,20 @@
([#5826](https://github.com/facebook/jest/pull/5826))
* `[jest-cli]` Interactive Snapshot Mode improvements
([#5864](https://github.com/facebook/jest/pull/5864))
* `[jest-editor-support]` Add `no-color` option to runner
([#5909](https://github.com/facebook/jest/pull/5909))

### Fixes

* `[jest-runtime]` Throw a more useful error when trying to require modules
after the test environment is torn down
([#5888](https://github.com/facebook/jest/pull/5888))
* `[jest-jasmine2]` Added assertion error handling inside `afterAll hook`
([#5884](https://github.com/facebook/jest/pull/5884))
* `[jest-cli]` Remove the notifier actions in case of failure when not in watch
mode. ([#5861](https://github.com/facebook/jest/pull/5861))
* `[jest-mock]` Extend .toHaveBeenCalled return message with outcome
([#5951](https://github.com/facebook/jest/pull/5951))
* `[jest-runner]` Assign `process.env.JEST_WORKER_ID="1"` when in runInBand mode
([#5860](https://github.com/facebook/jest/pull/5860))
* `[jest-cli]` Add descriptive error message when trying to use
Expand All @@ -74,13 +86,17 @@
([#5720](https://github.com/facebook/jest/pull/5720))
* `[pretty-format]` Handle React fragments better
([#5816](https://github.com/facebook/jest/pull/5816))
* `[jest-cli]` Switch collectCoverageFrom back to a string
([#5914](https://github.com/facebook/jest/pull/5914))
* `[jest-regex-util]` Fix handling regex symbols in tests path on Windows
([#5941](https://github.com/facebook/jest/pull/5941))

### Chore & Maintenance

* `[jest-jasmine2]` Simplify `Env.execute` and TreeProcessor to setup and clean
resources for the top suite the same way as for all of the children suites
([#5885](https://github.com/facebook/jest/pull/5885))
* `*` Run Prettier on compiled output
* `[*]` Run Prettier on compiled output
([#5858](https://github.com/facebook/jest/pull/3497))
* `[jest-cli]` Add fileChange hook for plugins
([#5708](https://github.com/facebook/jest/pull/5708))
Expand Down
14 changes: 8 additions & 6 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ directory. The default cache directory can be found by calling

### `--collectCoverageFrom=<glob>`

An array of glob patterns relative to <rootDir> matching the files that coverage
info needs to be collected from.
A glob pattern relative to <rootDir> matching the files that coverage info needs
to be collected from.

### `--colors`

Expand Down Expand Up @@ -280,10 +280,12 @@ Prevent tests from printing messages through the console.

### `--testNamePattern=<regex>`

Alias: `-t`. Run only tests and test suites with a name that matches the regex.
For example, suppose you want to run only tests related to authorization which
will have names like `"GET /api/posts with auth"`, then you can use
`jest -t=auth`.
Alias: `-t`. Run only tests with a name that matches the regex. For example,
suppose you want to run only tests related to authorization which will have
names like `"GET /api/posts with auth"`, then you can use `jest -t=auth`.

_Note: The regex is matched against the full name, which is a combination of the
test name and all its surrounding describe blocks._

### `--testLocationInResults`

Expand Down
16 changes: 15 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ These options let you control Jest's behavior in your `package.json` file. The
Jest philosophy is to work great by default, but sometimes you just need more
configuration power.

### Defaults

You can retrieve Jest's default options to expand them if needed:

```js
// jest.config.js
const {defaults} = require('jest-config');
module.exports = {
// ...
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
// ...
};
```

<AUTOGENERATED_TABLE_OF_CONTENTS>

---
Expand Down Expand Up @@ -192,7 +206,7 @@ different root directories. Example:
Default: `["json", "lcov", "text"]`

A list of reporter names that Jest uses when writing coverage reports. Any
[istanbul reporter](https://github.com/gotwarlost/istanbul/tree/master/lib/report)
[istanbul reporter](https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib)
can be used.

_Note: Setting this option overwrites the default values. Add `"text"` or
Expand Down
15 changes: 8 additions & 7 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,9 @@ test('throws on octopus', () => {
```

If you want to test that a specific error gets thrown, you can provide an
argument to `toThrow`. The argument can be a string for the error message, a
class for the error, or a regex that should match the error. For example, let's
say that `drinkFlavor` is coded like this:
argument to `toThrow`. The argument can be a string that should be contained in
the error message, a class for the error, or a regex that should match the error
message. For example, let's say that `drinkFlavor` is coded like this:

```js
function drinkFlavor(flavor) {
Expand All @@ -1101,11 +1101,12 @@ test('throws on octopus', () => {
drinkFlavor('octopus');
}

// Test the exact error message
expect(drinkOctopus).toThrowError('yuck, octopus flavor');

// Test that the error message says "yuck" somewhere
// Test that the error message says "yuck" somewhere: these are equivalent
expect(drinkOctopus).toThrowError(/yuck/);
expect(drinkOctopus).toThrowError('yuck');

// Test the exact error message
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);

// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
Expand Down
64 changes: 0 additions & 64 deletions docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,70 +234,6 @@ jest --maxWorkers=4
yarn test --maxWorkers=4
```

### Tests are slow when leveraging automocking

Whether via [`automock: true`](configuration.html#automock-boolean) in config or
lots of
[`jest.mock('my-module')`](jest-object.html#jestmockmodulename-factory-options)
calls in tests, automocking has a performance cost that can add up in large
projects. The more dependencies a module has, the more work Jest has to do to
mock it. Something that can offset this performance cost significantly is adding
a code transformer that moves `import` or `require` calls from the top of a
module, where they are always executed, down into the body of the module, where
they are usually not executed. This can lower the number of modules Jest has to
load when running your tests by a considerable amount.

To transform `import` statements, there is
[babel-plugin-transform-inline-imports-commonjs](https://github.com/zertosh/babel-plugin-transform-inline-imports-commonjs),
and to transform `require` statements, there is
[Facebook's `inline-requires` babel plugin](https://github.com/facebook/fbjs/blob/master/packages/babel-preset-fbjs/plugins/inline-requires.js),
which is part of the `babel-preset-fbjs` package.
### I'm using npm3 and my node_modules aren't properly loading.
Upgrade `jest-cli` to `0.9.0` or above.
### I'm using babel and my unmocked imports aren't working?
Upgrade `jest-cli` to `0.9.0` or above.
Explanation:
```js
jest.dontMock('foo');
import foo from './foo';
```
In ES6, import statements get hoisted before all other
```js
const foo = require('foo');
jest.dontMock('foo'); // Oops!
```
In Jest 0.9.0, a new API `jest.unmock` was introduced. Together with a plugin
for babel, this will now work properly when using `babel-jest`:
```js
jest.unmock('./foo'); // Use unmock!
import foo from './foo';
// foo is not mocked!
```
See the [Getting Started]GettingStarted.md#using-babel) guide on how to enable
babel support.
### I upgraded to Jest 0.9.0 and my tests are now failing?
Jest is now using Jasmine 2 by default. It should be easy to upgrade using the
Jasmine [upgrade guide](http://jasmine.github.io/2.0/introduction.html).
If you would like to continue using Jasmine 1, set the `testRunner` config
option to `jasmine1` or pass `--testRunner=jasmine1` as a command line option.
### Compatibility issues

Jest takes advantage of new features added to Node 6. We recommend that you
Expand Down
9 changes: 7 additions & 2 deletions docs/TutorialReact.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ If you are just getting started with React, we recommend using
[Create React App](https://github.com/facebookincubator/create-react-app). It is
ready to use and
[ships with Jest](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#running-tests)!
You don't need to do any extra steps for setup, and can head straight to the
next section.
You will only need to add `react-test-renderer` for rendering snapshots.

Run

```bash
yarn add --dev react-test-renderer
```

### Setup without Create React App

Expand Down
3 changes: 2 additions & 1 deletion integration-tests/__tests__/coverage_report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ test('collects coverage only from multiple specified files', () => {
const {stdout} = runJest(DIR, [
'--no-cache',
'--coverage',
'--collectCoverageFrom', // overwrites the one in package.json
'--collectCoverageFrom',
'setup.js',
'--collectCoverageFrom',
'OtherFile.js',
]);

Expand Down
18 changes: 18 additions & 0 deletions integration-tests/__tests__/lifecycles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

test('suite with invalid assertions in afterAll', () => {
const {stderr, status} = runJest('lifecycles');

expect(status).toBe(1);
expect(stderr).toMatch(/afterAll just failed!/);
});
3 changes: 0 additions & 3 deletions integration-tests/__tests__/regex_(char_in_path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
*/
'use strict';

const SkipOnWindows = require('../../scripts/SkipOnWindows');
const runJest = require('../runJest');

describe('Regex Char In Path', () => {
SkipOnWindows.suite();

it('parses paths containing regex chars correctly', () => {
const {json} = runJest.json('regex-(char-in-path', []);

Expand Down
12 changes: 12 additions & 0 deletions integration-tests/lifecycles/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*/

afterAll(() => {
throw new Error('afterAll just failed!');
});
test('one', () => {});
test('two', () => {});
9 changes: 9 additions & 0 deletions integration-tests/lifecycles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
*/

module.exports = () => {};
5 changes: 5 additions & 0 deletions integration-tests/lifecycles/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Got:
exports[`toBeCalled .not passes when called 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).toBeCalled(</><dim>)</>

Expected mock function to have been called."
Expected mock function to have been called, but it was not called."
`;

exports[`toBeCalled fails with any argument passed 1`] = `
Expand Down Expand Up @@ -541,7 +541,7 @@ Got:
exports[`toHaveBeenCalled .not passes when called 1`] = `
"<dim>expect(</><red>jest.fn()</><dim>).toHaveBeenCalled(</><dim>)</>

Expected mock function to have been called."
Expected mock function to have been called, but it was not called."
`;

exports[`toHaveBeenCalled fails with any argument passed 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/spy_matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const createToBeCalledMatcher = matcherName => (received, expected) => {
: () =>
matcherHint(matcherName, receivedName, '') +
'\n\n' +
`Expected ${type} to have been called.`;
`Expected ${type} to have been called, but it was not called.`;

return {message, pass};
};
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ export const options = {
},
collectCoverageFrom: {
description:
'An array of glob patterns relative to <rootDir> matching the files ' +
'that coverage info needs to be collected from.',
type: 'array',
'A glob pattern relative to <rootDir> matching the files that coverage ' +
'info needs to be collected from.',
type: 'string',
},
collectCoverageOnlyFrom: {
description: 'Explicit list of paths coverage will be restricted to.',
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-cli/src/lib/is_valid_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/

import type {GlobalConfig, ProjectConfig} from 'types/Config';

const SNAPSHOT_EXTENSION = 'snap';
import Snapshot from 'jest-snapshot';

export default function isValidPath(
globalConfig: GlobalConfig,
Expand All @@ -19,6 +18,6 @@ export default function isValidPath(
return (
!filePath.includes(globalConfig.coverageDirectory) &&
!config.watchPathIgnorePatterns.some(pattern => filePath.match(pattern)) &&
!filePath.endsWith(`.${SNAPSHOT_EXTENSION}`)
!filePath.endsWith(`.${Snapshot.EXTENSION}`)
);
}
Loading

0 comments on commit 32c49f1

Please sign in to comment.