Skip to content

Commit 108596d

Browse files
kolodnycalvinmetcalf
authored andcommitted
Fix bug with process.nextTick referencing the wrong function. (#10)
* Fix bug with process.nextTick referencing the wrong function. `lolex` and by extension `sinon` fake timers, change the value of `process.nextTick` in order to properly be added to the fake event loop stack. The previous way this module was written kept a reference to the original function and didn't look up the current value of `process.nextTick`. This commit makes each invocation of nextTick look up the current value of `process.nextTick`. * Revert polyfill and add a wrapper for the real nextTick. * Use wrapper that defaults to process on newish nodes.
1 parent 07f531b commit 108596d

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
if (!process.version ||
44
process.version.indexOf('v0.') === 0 ||
55
process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) {
6-
module.exports = nextTick;
6+
module.exports = { nextTick: nextTick };
77
} else {
8-
module.exports = process.nextTick;
8+
module.exports = process
99
}
1010

1111
function nextTick(fn, arg1, arg2, arg3) {
@@ -41,3 +41,4 @@ function nextTick(fn, arg1, arg2, arg3) {
4141
});
4242
}
4343
}
44+

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ npm install --save process-nextick-args
1010
Always be able to pass arguments to process.nextTick, no matter the platform
1111

1212
```js
13-
var nextTick = require('process-nextick-args');
13+
var pna = require('process-nextick-args');
1414

15-
nextTick(function (a, b, c) {
15+
pna.nextTick(function (a, b, c) {
1616
console.log(a, b, c);
1717
}, 'step', 3, 'profit');
1818
```

test.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
var test = require("tap").test;
2-
var nextTick = require('./');
2+
var pna = require('./');
33

44
test('should work', function (t) {
55
t.plan(5);
6-
nextTick(function (a) {
6+
pna.nextTick(function (a) {
77
t.ok(a);
8-
nextTick(function (thing) {
8+
pna.nextTick(function (thing) {
99
t.equals(thing, 7);
1010
}, 7);
1111
}, true);
12-
nextTick(function (a, b, c) {
12+
pna.nextTick(function (a, b, c) {
1313
t.equals(a, 'step');
1414
t.equals(b, 3);
1515
t.equals(c, 'profit');
@@ -18,7 +18,19 @@ test('should work', function (t) {
1818

1919
test('correct number of arguments', function (t) {
2020
t.plan(1);
21-
nextTick(function () {
21+
pna.nextTick(function () {
2222
t.equals(2, arguments.length, 'correct number');
2323
}, 1, 2);
2424
});
25+
26+
test('uses the current value of process.nextTick', function (t) {
27+
t.plan(1);
28+
var oldNextTick = process.nextTick;
29+
var called = false;
30+
process.nextTick = function() {
31+
called = true
32+
};
33+
pna.nextTick(function () {});
34+
process.nextTick = oldNextTick;
35+
t.ok(called);
36+
});

0 commit comments

Comments
 (0)