Skip to content

Commit

Permalink
Merge branch 'master' into drop-polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee authored Feb 4, 2017
2 parents 418b8c7 + 91ef29d commit 581f442
Show file tree
Hide file tree
Showing 56 changed files with 578 additions and 138 deletions.
Binary file removed .github/jest-logo.png
Binary file not shown.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ npm-debug.log
coverage
lerna-debug.log
npm-debug.log*
website/core/metadata*.js
website/src/jest/blog
website/src/jest/docs
/danger/node_modules/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Jest logo](.github/jest-logo.png?raw=true)
# Jest

[![Build Status](https://travis-ci.org/facebook/jest.svg?branch=master)](https://travis-ci.org/facebook/jest) [![Windows Build Status](https://ci.appveyor.com/api/projects/status/8n38o44k585hhvhd/branch/master?svg=true)](https://ci.appveyor.com/project/Daniel15/jest/branch/master) [![npm version](https://badge.fury.io/js/jest-cli.svg)](http://badge.fury.io/js/jest-cli)

Expand Down Expand Up @@ -90,7 +90,7 @@ Jest can be used in projects that use [webpack](https://webpack.github.io/) to m
To use TypeScript in your tests, install the `ts-jest` package:

```
npm install --save-dev test-jest
npm install --save-dev ts-jest
```

then modify your `package.json` so the `jest` section looks something like:
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ deployment:
- git config --global user.email "jest-bot@users.noreply.github.com"
- git config --global user.name "Website Deployment Script"
- echo "machine github.com login jest-bot password $GITHUB_TOKEN" > ~/.netrc
- cd website && GIT_USER=jest-bot DEPLOY_USER=jest-bot npm run gh-pages
- cd website && GIT_USER=jest-bot npm run gh-pages
21 changes: 19 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Default: `[]`
A list of paths to snapshot serializer modules Jest should use for snapshot
testing.

Jest has default serializers for built-in javascript types and for react
Jest has default serializers for built-in JavaScript types and for React
elements. See [snapshot test tutorial](/jest/docs/tutorial-react-native.html#snapshot-test) for more information.

Example serializer module:
Expand Down Expand Up @@ -304,13 +304,29 @@ Pretty foo: Object {
}
```

To make a dependency explicit instead of implicit, you can call [`expect.addSnapshotSerializer`](/jest/docs/expect.html#expectaddsnapshotserializerserializer) to add a module for an individual test file instead of adding its path to `snapshotSerializers` in Jest configuration.

### `testEnvironment` [string]
Default: `"jsdom"`

The test environment that will be used for testing. The default environment in Jest is a browser-like environment through [jsdom](https://github.com/tmpvar/jsdom). If you are building a node service, you can use the `node` option to use a node-like environment instead. Combining the test environments is currently not possible but the `jsdom` environment can be seen as a superset of the `node` one.

You can create your own module that will be used for setting up the test environment. The module must export a class with `runScript` and `dispose` methods. See the [node](https://github.com/facebook/jest/blob/master/packages/jest-environment-node/src/index.js) or [jsdom](https://github.com/facebook/jest/blob/master/packages/jest-environment-jsdom/src/index.js) environments as examples.

### `testMatch` [array<string>]
(default: `[ '**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)' ]`)

The glob patterns Jest uses to detect test files. By default it looks for `.js` and `.jsx` files
inside of `__tests__` folders, as well as any files with a suffix of `.test` or `.spec`
(e.g. `Component.test.js` or `Component.spec.js`). It will also find files called `test.js`
or `spec.js`.

See the [micromatch](https://github.com/jonschlinkert/micromatch) package
for details of the patterns you can specify.

See also [`testRegex` [string]](#testregex-string), but note that you
cannot specify both options.

### `testPathDirs` [array<string>]
Default: `["<rootDir>"]`

Expand All @@ -331,7 +347,8 @@ Default: `(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$`
The pattern Jest uses to detect test files. By default it looks for `.js` and `.jsx` files
inside of `__tests__` folders, as well as any files with a suffix of `.test` or `.spec`
(e.g. `Component.test.js` or `Component.spec.js`). It will also find files called `test.js`
or `spec.js`.
or `spec.js`. See also [`testMatch` [array<string>]](#testmatch-array-string), but note
that you cannot specify both options.

### `testResultsProcessor` [string]
Default: `undefined`
Expand Down
20 changes: 20 additions & 0 deletions docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,26 @@ test('onPress gets called with the right thing', () => {
})
```

### `expect.addSnapshotSerializer(serializer)`

You can call `expect.addSnapshotSerializer` to add a module that formats application-specific data structures.

For an individual test file, an added module precedes any modules from `snapshotSerializers` configuration, which precede the default snapshot serializers for built-in JavaScript types and for React elements. The last module added is the first module tested.

```js
import serializer from 'my-serializer-module';
expect.addSnapshotSerializer(serializer);

// affects expect(value).toMatchSnapshot() assertions in the test file
```

If you add a snapshot serializer in individual test files instead of to adding it to `snapshotSerializers` configuration:

* You make the dependency explicit instead of implicit.
* You avoid limits to configuration that might cause you to eject from [create-react-app](https://github.com/facebookincubator/create-react-app).

See [configuring package.json](/jest/docs/configuration.html#snapshotserializers-array-string) for more information.

### `.not`

If you know how to test something, `.not` lets you test its opposite. For example, this code tests that the best La Croix flavor is not coconut:
Expand Down
4 changes: 2 additions & 2 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Returns the `jest` object for chaining.
*Note: this method was previously called `autoMockOn`. When using `babel-jest`, calls to `enableAutomock` will automatically be hoisted to the top of the code block. Use `autoMockOn` if you want to explicitly avoid this behavior.*

### `jest.fn(implementation)`
Returns a new, unused [mock function](#mock-functions). Optionally takes a mock implementation.
Returns a new, unused [mock function](/jest/docs/mock-function-api.html). Optionally takes a mock implementation.

```js
const mockFn = jest.fn();
Expand Down Expand Up @@ -187,7 +187,7 @@ In these rare scenarios you can use this API to manually fill the slot in the mo

Returns the `jest` object for chaining.

*Note It is recommended to use [`jest.mock()`](#jest-mock-modulename-factory) instead. The `jest.mock` API's second argument is a module factory instead of the expected exported module object.*
*Note It is recommended to use [`jest.mock()`](#jestmockmodulename-factory-options) instead. The `jest.mock` API's second argument is a module factory instead of the expected exported module object.*

### `jest.unmock(moduleName)`
Indicates that the module system should never return a mocked version of the specified module from `require()` (e.g. that it should always return the real module).
Expand Down
4 changes: 2 additions & 2 deletions docs/SnapshotTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ exports[`Link renders correctly 1`] = `
<a
className="normal"
href="http://www.facebook.com"
onMouseEnter={[Function bound _onMouseEnter]}
onMouseLeave={[Function bound _onMouseLeave]}>
onMouseEnter={[Function]}
onMouseLeave={[Function]}>
Facebook
</a>
`;
Expand Down
4 changes: 3 additions & 1 deletion docs/TutorialAsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ and enable the feature in your `.babelrc` file.

Errors can be handled in the standard JavaScript way: Either using `.catch()`
directly on a Promise or through `try-catch` when using async/await. Note that
if a Promise throws and the error is not handled, the test will fail.
if a Promise throws and the error is not handled, the test will fail. `expect.assertion(1)` makes sure that expectation was checked once. In this example it will fail if promise was resolved without throwing.

```js
// Testing for async errors can be done using `catch`.
it('tests error with promises', () => {
expect.assertions(1)
return user.getUserName(3)
.catch(e => expect(e).toEqual({
error: 'User with 3 not found.',
Expand All @@ -121,6 +122,7 @@ it('tests error with promises', () => {

// Or try-catch.
it('tests error with async/await', async () => {
expect.assertions(1)
try {
await user.getUserName(2);
} catch (object) {
Expand Down
22 changes: 21 additions & 1 deletion docs/Webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ thus requires ES modules to be transpiled to CommonJS modules. As such, if you
are using webpack 2, you most likely will want to configure Babel to transpile
ES modules to CommonJS modules only in the `test` environment.

```js
```json
// .babelrc
{
"presets": [
Expand All @@ -226,3 +226,23 @@ ES modules to CommonJS modules only in the `test` environment.
}
}
```

If you use dynamic imports (`import('some-file.js').then(module => ...)`), you
need to enable the `dynamic-import-node` plugin.

```json
// .babelrc
{
"presets": [
["es2015", {"modules": false}]
],

"plugins": ["syntax-dynamic-import"],

"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
}
}
```
2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"transform": {
"^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
},
"testRegex": "/__tests__/.*\\.(ts|tsx|js)$"
"testMatch": ["**/__tests__/*.(ts|tsx|js)"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ Object {
}
}
bProp={foo: 8} />
",
"snapshot serializers works with prepended plugins from expect method called once 1": "
<div
aProp={
Object {
\\"a\\": 6,
}
}
bProp={Foo: 8} />
",
"snapshot serializers works with prepended plugins from expect method called twice 1": "
<div
aProp={
Object {
\\"a\\": 6,
}
}
bProp={FOO: 8} />
",
"snapshot serializers works with second plugin 1": "bar: 2",
}
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/__tests__/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('config as JSON', () => {
const result = runJest('verbose_reporter', [
'--config=' + JSON.stringify({
testEnvironment: 'node',
testRegex: 'banana strawbery kiwi',
testMatch: ['banana strawbery kiwi'],
}),
]);
const stdout = result.stdout.toString();
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/__tests__/snapshot-serializers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const snapshotPath = path.resolve(snapshotsDir, 'snapshot-test.js.snap');
const runAndAssert = () => {
const result = runJest.json('snapshot-serializers');
const json = result.json;
expect(json.numTotalTests).toBe(5);
expect(json.numPassedTests).toBe(5);
expect(json.numTotalTests).toBe(7);
expect(json.numPassedTests).toBe(7);
expect(json.numFailedTests).toBe(0);
expect(json.numPendingTests).toBe(0);
expect(result.status).toBe(0);
Expand Down
36 changes: 36 additions & 0 deletions integration_tests/snapshot-serializers/__tests__/snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,40 @@ describe('snapshot serializers', () => {
};
expect(test).toMatchSnapshot();
});

it('works with prepended plugins from expect method called once', () => {
const test = {
$$typeof: Symbol.for('react.test.json'),
children: null,
props: {
aProp: {a: 6},
bProp: {foo: 8},
},
type: 'div',
};
// Add plugin that overrides foo specified by Jest config in package.json
expect.addSnapshotSerializer({
print: (val, serialize) => `Foo: ${serialize(val.foo)}`,
test: val => val && val.hasOwnProperty('foo'),
});
expect(test).toMatchSnapshot();
});

it('works with prepended plugins from expect method called twice', () => {
const test = {
$$typeof: Symbol.for('react.test.json'),
children: null,
props: {
aProp: {a: 6},
bProp: {foo: 8},
},
type: 'div',
};
// Add plugin that overrides preceding added plugin
expect.addSnapshotSerializer({
print: (val, serialize) => `FOO: ${serialize(val.foo)}`,
test: val => val && val.hasOwnProperty('foo'),
});
expect(test).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion integration_tests/typescript-coverage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"transform": {
"^.+\\.(ts|js)$": "<rootDir>/typescript-preprocessor.js"
},
"testRegex": "/__tests__/.*\\.(ts|tsx|js)$",
"testMatch": ["**/__tests__/*.(ts|tsx|js)"],
"testEnvironment": "node",
"moduleFileExtensions": [
"ts",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@
"\\.snap$",
"/packages/.*/build"
],
"testRegex": ".*-test\\.js"
"testMatch": ["**/*-test.js"]
}
}
11 changes: 11 additions & 0 deletions packages/jest-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Jest

🃏 Painless JavaScript Testing

- **👩🏻‍💻 Easy Setup**: Jest is a complete and easy to set up JavaScript testing solution. In fact, Jest works out of the box for any React project.

- **🏃🏽 Instant Feedback**: Failed tests run first. Fast interactive mode can switch between running all tests or only test files related to changed files.

- **📸 Snapshot Testing**: Jest can [capture snapshots](http://facebook.github.io/jest/docs/snapshot-testing.html) of React trees or other serializable values to simplify UI testing.

Read More: http://facebook.github.io/jest/
30 changes: 24 additions & 6 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"jest-snapshot": "^18.1.0",
"jest-util": "^18.1.0",
"node-notifier": "^5.0.1",
"micromatch": "^2.3.11",
"string-length": "^1.0.1",
"strip-ansi": "^3.0.1",
"throat": "^3.0.0",
"which": "^1.1.1",
"worker-farm": "^1.3.1",
Expand All @@ -44,13 +44,31 @@
"url": "https://github.com/facebook/jest/issues"
},
"homepage": "http://facebook.github.io/jest/",
"license": "BSD-3-Clause",
"keywords": [
"ava",
"babel",
"coverage",
"easy",
"expect",
"facebook",
"immersive",
"instant",
"jasmine",
"jest",
"jsdom",
"mocha",
"mocking",
"painless",
"qunit",
"runner",
"sandboxed",
"snapshot",
"tap",
"tape",
"test",
"unit",
"jasmine",
"mock"
],
"license": "BSD-3-Clause"
"testing",
"typescript",
"watch"
]
}
Loading

0 comments on commit 581f442

Please sign in to comment.