Skip to content

Commit a750e31

Browse files
blainekastenFacebook Github Bot 3
authored andcommitted
Add note for scriptPreprocessor docs about --no-cache
Summary:Helps solve jestjs#843 by including info about developing a `scriptPreprocessor` in the docs. Closes jestjs#844 Differential Revision: D3106501 fb-gh-sync-id: 82d6a6aedc1384f0b259f6ba2bfe89c1b0ceab15 fbshipit-source-id: 82d6a6aedc1384f0b259f6ba2bfe89c1b0ceab15
1 parent 3ec2df5 commit a750e31

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ Example Output:
238238
- [`jest.currentTestPath()`](http://facebook.github.io/jest/docs/api.html#jest-currenttestpath)
239239
- [`jest.disableAutomock()`](http://facebook.github.io/jest/docs/api.html#jest-disableautomock)
240240
- [`jest.enableAutomock()`](http://facebook.github.io/jest/docs/api.html#jest-enableautomock)
241-
- [`jest.fn(implementation?)`](http://facebook.github.io/jest/docs/api.html#jest-fn-implementation)
241+
- [`jest.fn(?implementation)`](http://facebook.github.io/jest/docs/api.html#jest-fn-implementation)
242242
- [`jest.genMockFromModule(moduleName)`](http://facebook.github.io/jest/docs/api.html#jest-genmockfrommodule-modulename)
243-
- [`jest.mock(moduleName)`](http://facebook.github.io/jest/docs/api.html#jest-mock-modulename)
243+
- [`jest.mock(moduleName, ?factory)`](http://facebook.github.io/jest/docs/api.html#jest-mock-modulename-factory)
244244
- [`jest.runAllTicks()`](http://facebook.github.io/jest/docs/api.html#jest-runallticks)
245245
- [`jest.runAllTimers()`](http://facebook.github.io/jest/docs/api.html#jest-runalltimers)
246246
- [`jest.runOnlyPendingTimers()`](http://facebook.github.io/jest/docs/api.html#jest-runonlypendingtimers)
@@ -361,7 +361,7 @@ Re-enables automatic mocking in the module loader.
361361

362362
*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.*
363363

364-
### `jest.fn(implementation?)`
364+
### `jest.fn(?implementation)`
365365
Returns a new, unused [mock function](http://facebook.github.io/jest/docs/api.html#mock-functions). Optionally takes a mock
366366
implementation.
367367

@@ -380,10 +380,27 @@ Given the name of a module, use the automatic mocking system to generate a mocke
380380

381381
This is useful when you want to create a [manual mock](http://facebook.github.io/jest/docs/manual-mocks.html) that extends the automatic mock's behavior.
382382

383-
### `jest.mock(moduleName)`
383+
### `jest.mock(moduleName, ?factory)`
384384
Indicates that the module system should always return a mocked version of the specified module from `require()` (e.g. that it should never return the real module).
385385

386-
This is normally useful under the circumstances where you have called [`jest.autoMockOff()`](http://facebook.github.io/jest/docs/api.html#jest-automockoff), but still wish to specify that certain particular modules should be mocked by the module system.
386+
```js
387+
jest.mock('moduleName');
388+
389+
const moduleName = require('moduleName'); // moduleName will be explicitly mocked
390+
```
391+
392+
The second argument can be used to specify an explicit module factory that is being run instead of using Jest's automocking feature:
393+
394+
```js
395+
jest.mock('moduleName', () => {
396+
return jest.fn(() => 42);
397+
});
398+
399+
const moduleName = require('moduleName'); // This runs the function specified as second argument to `jest.mock`.
400+
moduleName(); // Will return "42";
401+
```
402+
403+
*Note: When using `babel-jest`, calls to `mock` will automatically be hoisted to the top of the code block. Use `doMock` if you want to explicitly avoid this behavior.*
387404

388405
### `jest.runAllTicks()`
389406
Exhausts the **micro**-task queue (usually interfaced in node via `process.nextTick`).
@@ -411,6 +428,8 @@ On occasion there are times where the automatically generated mock the module sy
411428

412429
In these rare scenarios you can use this API to manually fill the slot in the module system's mock-module registry.
413430

431+
*Note It is recommended to use [`jest.mock()`](http://facebook.github.io/jest/docs/api.html#jest-mock-modulename-factory) instead. The `jest.mock` API's second argument is a module factory instead of the expected exported module object.*
432+
414433
### `jest.unmock(moduleName)`
415434
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).
416435

@@ -626,11 +645,14 @@ A map from regular expressions to module names that allow to stub out resources,
626645

627646
Use `<rootDir>` string token to refer to [`config.rootDir`](http://facebook.github.io/jest/docs/api.html#config-rootdir-string) value if you want to use file paths.
628647

648+
Additionally, you can substitute captured regex groups using numbered backreferences.
649+
629650
Example:
630651
```js
631652
"moduleNameMapper": {
632653
"^image![a-zA-Z0-9$_-]+$": "GlobalImageStub",
633654
"^[./a-zA-Z0-9$_-]+\.png$": "<rootDir>/RelativeImageStub.js",
655+
"module_name_(.*)": "<rootDir>/substituted_module_$1.js"
634656
}
635657
```
636658

@@ -655,6 +677,8 @@ The path to a module that provides a synchronous function from pre-processing so
655677

656678
Examples of such compilers include [jstransform](http://github.com/facebook/jstransform), [recast](http://github.com/benjamn/recast), [regenerator](http://github.com/facebook/regenerator), and [traceur](https://github.com/google/traceur-compiler).
657679

680+
*Note: Jest's preprocessor is only ran once per file unless the file has changed. During development of a `scriptPreprocessor` it can be useful to run Jest with `--no-cache` or to frequently [delete Jest's cache](/jest/docs/troubleshooting.html#caching-issues).*
681+
658682
### `config.preprocessorIgnorePatterns` [array<string>]
659683
(default: `["/node_modules/"]`)
660684

docs/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ The path to a module that provides a synchronous function from pre-processing so
452452

453453
Examples of such compilers include [jstransform](http://github.com/facebook/jstransform), [recast](http://github.com/benjamn/recast), [regenerator](http://github.com/facebook/regenerator), and [traceur](https://github.com/google/traceur-compiler).
454454

455+
*Note: Jest's preprocessor is only ran once per file unless the file has changed. During development of a `scriptPreprocessor` it can be useful to run Jest with `--no-cache` or to frequently [delete Jest's cache](/jest/docs/troubleshooting.html#caching-issues).*
456+
455457
### `config.preprocessorIgnorePatterns` [array<string>]
456458
(default: `["/node_modules/"]`)
457459

0 commit comments

Comments
 (0)