public
Last active

Example of predictableNextTick

  • Download Gist
get-last-modified-fixed.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var fs = require('fs');
 
var getLastModified = function(files, done) {
var timestamps = new Array(files.length);
var pending = files.length;
 
files.forEach(function(file, idx) {
fs.stat(file, function(err, stat) {
timestamps[idx] = stat.mtime;
 
if (!--pending) {
done(null, timestamps);
}
});
});
};
get-last-modified.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var fs = require('fs');
 
var getLastModified = function(files, done) {
var timestamps = [];
 
files.forEach(function(file) {
fs.stat(file, function(err, stat) {
timestamps.push(stat.mtime);
 
if (timestamps.length === files.length) {
done(null, timestamps);
}
});
});
};
get-last-modified.spec.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Jasmine Syntax
//
// Install npm dependencies first:
// npm install mocks
//
// Then, run unit test:
// jasmine-node .
 
describe('getLastModified', function() {
 
var mocks = require('mocks');
var mockery = {
// reate a fake in-memory FS
fs: mocks.fs.create({
'one.js': mocks.fs.file('2012-01-01'),
'two.js': mocks.fs.file('2012-02-02'),
'three.js': mocks.fs.file('2012-02-02')
})
};
 
// load the module (using fake FS)
var getLastModified = mocks.loadFile('get-last-modified-fixed.js', mockery).getLastModified;
 
 
it('should return last modified timestamps for every file', function() {
var spy = jasmine.createSpy('done').andCallFake(function(err, timestamps) {
expect(timestamps).toEqual([
new Date('2012-01-01'), new Date('2012-02-02'), new Date('2012-02-02')
]);
});
 
mocks.predictableNextTick.pattern = [1, 0];
getLastModified(['/one.js', '/two.js', '/three.js'], spy);
 
// wait for done callback
waitsFor(function() {return spy.callCount;});
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.