You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add note for scriptPreprocessor docs about --no-cache
Summary:Helps solve jestjs#843 by including info about developing a `scriptPreprocessor` in the docs.
Closesjestjs#844
Differential Revision: D3106501
fb-gh-sync-id: 82d6a6aedc1384f0b259f6ba2bfe89c1b0ceab15
fbshipit-source-id: 82d6a6aedc1384f0b259f6ba2bfe89c1b0ceab15
@@ -361,7 +361,7 @@ Re-enables automatic mocking in the module loader.
361
361
362
362
*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.*
363
363
364
-
### `jest.fn(implementation?)`
364
+
### `jest.fn(?implementation)`
365
365
Returns a new, unused [mock function](http://facebook.github.io/jest/docs/api.html#mock-functions). Optionally takes a mock
366
366
implementation.
367
367
@@ -380,10 +380,27 @@ Given the name of a module, use the automatic mocking system to generate a mocke
380
380
381
381
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.
382
382
383
-
### `jest.mock(moduleName)`
383
+
### `jest.mock(moduleName, ?factory)`
384
384
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).
385
385
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
+
constmoduleName=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
+
returnjest.fn(() =>42);
397
+
});
398
+
399
+
constmoduleName=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.*
387
404
388
405
### `jest.runAllTicks()`
389
406
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
411
428
412
429
In these rare scenarios you can use this API to manually fill the slot in the module system's mock-module registry.
413
430
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
+
414
433
### `jest.unmock(moduleName)`
415
434
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).
416
435
@@ -626,11 +645,14 @@ A map from regular expressions to module names that allow to stub out resources,
626
645
627
646
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.
628
647
648
+
Additionally, you can substitute captured regex groups using numbered backreferences.
@@ -655,6 +677,8 @@ The path to a module that provides a synchronous function from pre-processing so
655
677
656
678
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).
657
679
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).*
Copy file name to clipboardExpand all lines: docs/API.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -452,6 +452,8 @@ The path to a module that provides a synchronous function from pre-processing so
452
452
453
453
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).
454
454
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).*
0 commit comments