Skip to content

Commit

Permalink
docs(jest): improve docs about fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 5, 2019
1 parent e778e0b commit 09dd3cf
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions docs/jest.jade
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ block content
## Timer Mocks

Absolutely do **not** use [timer mocks](https://jestjs.io/docs/en/timer-mocks.html)
when testing Mongoose apps. Mongoose devs have already refactored out code
when testing Mongoose apps. Fake timers stub out global functions like
`setTimeout()` and `setInterval()`, which causes problems when an underlying
library uses these functions. The MongoDB Node.js driver uses these functions
for deferring work until the next tick of the event loop and for monitoring
connections to the MongoDB server.

Mongoose devs have already refactored out code
to [avoid using `setImmediate()`](https:/Automattic/mongoose/issues/6074)
to defer work to the next tick of the event loop, but we can't reasonably
ensure that every library Mongoose depends on doesn't use `setImmediate()`.

Mongoose uses `nextTick()`, which [Jest's underlying dependency explicitly doesn't stub by default](https://sinonjs.org/releases/latest/fake-timers/).
But, `process.nextTick()` isn't the same as `setImmediate()` because of
[microtasks vs macrotasks](https://abc.danch.me/microtasks-macrotasks-more-on-the-event-loop-881557d7af6f#2261),
Expand Down

0 comments on commit 09dd3cf

Please sign in to comment.