Skip to content

Commit

Permalink
setTestTimeout/setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Jun 8, 2017
1 parent 41beefa commit d458736
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/en/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The `jest` object is automatically in scope within every test file. The methods
- [`jest.runTimersToTime(msToRun)`](#jestruntimerstotimemstorun)
- [`jest.runOnlyPendingTimers()`](#jestrunonlypendingtimers)
- [`jest.setMock(moduleName, moduleExports)`](#jestsetmockmodulename-moduleexports)
- [`jest.setTestTimeout(timeout)`](#jestsettesttimeouttimeout)
- [`jest.setTimeout(timeout)`](#jestsettimeouttimeout)
- [`jest.unmock(moduleName)`](#jestunmockmodulename)
- [`jest.useFakeTimers()`](#jestusefaketimers)
- [`jest.useRealTimers()`](#jestuserealtimers)
Expand Down Expand Up @@ -205,16 +205,16 @@ Returns the `jest` object for chaining.

*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.setTestTimeout(timeout)`
### `jest.setTimeout(timeout)`

Set the default timeout interval for tests in milliseconds.
Set the default timeout interval for tests and before/after hooks in milliseconds.

*Note: The default timeout interval is 5 seconds if this method is not called.*

Example:

```js
jest.setTestTimeout(1000); // 1 second
jest.setTimeout(1000); // 1 second
```

### `jest.unmock(moduleName)`
Expand Down
4 changes: 2 additions & 2 deletions docs/en/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ Consider replacing the global promise implementation with your own, for example
used Promise libraries to a single one.

If your test is long running, you may want to consider to increase the timeout
by calling `jest.setTestTimeout`
by calling `jest.setTimeout`

```
jest.setTestTimeout(10000); // 10 second timeout
jest.setTimeout(10000); // 10 second timeout
```

### Watchman Issues
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/__tests__/timeouts-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ afterAll(() => cleanup(DIR));
test('exceeds the timeout', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
jest.setTestTimeout(20);
jest.setTimeout(20);
test('banana', () => {
return new Promise(resolve => {
Expand All @@ -44,7 +44,7 @@ test('exceeds the timeout', () => {
test('does not exceed the timeout', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
jest.setTestTimeout(100);
jest.setTimeout(100);
test('banana', () => {
return new Promise(resolve => {
Expand Down
1 change: 0 additions & 1 deletion packages/jest-circus/src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import type {Event, State, EventHandler} from '../types';


const {makeDescribe} = require('./utils');
const eventHandler = require('./eventHandler');

Expand Down
14 changes: 7 additions & 7 deletions packages/jest-circus/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ const getEachHooksForTest = (
return result;
};

const _makeTimeoutMessage = (timeout, isHook) => {
const message = `Exceeded timeout of ${timeout}ms for a ${isHook ? 'hook' : 'test'}.`;
return new Error(message);
};
const _makeTimeoutMessage = (timeout, isHook) =>
new Error(
`Exceeded timeout of ${timeout}ms for a ${isHook ? 'hook' : 'test'}.\nUse jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test.`,
);

const callAsyncFn = (
fn: AsyncFn,
testContext: ?TestContext,
{
isHook,
timeout,
test,
}: {isHook?: ?boolean, timeout: number, test?: TestEntry},
timeout,
}: {isHook?: ?boolean, test?: TestEntry, timeout: number},
): Promise<any> => {
return new Promise((resolve, reject) => {
setTimeout(() => reject(_makeTimeoutMessage(timeout, isHook)), timeout);
Expand All @@ -145,7 +145,7 @@ const callAsyncFn = (

// If it's a Promise, return it.
if (returnedValue instanceof Promise) {
return returnedValue.then(resolve).catch(reject);
return returnedValue.then(resolve, reject);
}

if (!isHook && returnedValue !== void 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/__tests__/SearchSource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
'use strict';

jest.setTestTimeout(15000);
jest.setTimeout(15000);

const path = require('path');
const skipOnWindows = require('skipOnWindows');
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class Runtime {
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);

const setTestTimeout = (timeout: number) => {
const setTimeout = (timeout: number) => {
this._environment.global.jasmine
? (this._environment.global.jasmine.DEFAULT_TIMEOUT_INTERVAL = timeout)
: (this._environment.global[
Expand Down Expand Up @@ -758,7 +758,7 @@ class Runtime {

setMock: (moduleName: string, mock: Object) =>
setMockFactory(moduleName, () => mock),
setTestTimeout,
setTimeout,
spyOn,

unmock,
Expand Down
2 changes: 1 addition & 1 deletion testSetupFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const jasmineReporters = require('jasmine-reporters');

// Some of the `jest-runtime` tests are very slow and cause
// timeouts on travis
jest.setTestTimeout(70000);
jest.setTimeout(70000);

if (global.jasmine && process.env.APPVEYOR_API_URL) {
// Running on AppVeyor, add the custom reporter.
Expand Down

0 comments on commit d458736

Please sign in to comment.