Merge pull request #547 from CartoDB/mockdate

Properly mock Date.now() for tests
This commit is contained in:
Rafa de la Torre 2018-12-11 12:46:49 +01:00 committed by GitHub
commit 2773c6d23e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 10 deletions

6
package-lock.json generated
View File

@ -1649,6 +1649,12 @@
}
}
},
"mockdate": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/mockdate/-/mockdate-2.0.2.tgz",
"integrity": "sha1-WuDA6vj+I+AJzQH5iJtCxPY0rxI=",
"dev": true
},
"moment": {
"version": "2.22.2",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz",

View File

@ -50,6 +50,7 @@
"jshint": "~2.6.0",
"libxmljs": "0.19.5",
"mocha": "3.2.0",
"mockdate": "^2.0.2",
"shapefile": "0.3.0",
"sqlite3": "4.0.0",
"zipfile": "0.5.11"

View File

@ -5,6 +5,7 @@ require('../helper');
var server = require('../../app/server')();
var assert = require('../support/assert');
var qs = require('querystring');
var MockDate = require('mockdate');
describe('last modified header', function() {
@ -64,10 +65,7 @@ describe('last modified header', function() {
api_key: 1234
});
var fixedDateNow = Date.now();
var dateNowFn = Date.now;
Date.now = function() {
return fixedDateNow;
};
MockDate.set(fixedDateNow);
assert.response(server,
{
url: '/api/v1/sql?' + query,
@ -80,7 +78,7 @@ describe('last modified header', function() {
statusCode: 200
},
function(err, res) {
Date.now = dateNowFn;
MockDate.reset();
assert.equal(res.headers['last-modified'], new Date(fixedDateNow).toUTCString());
done();
}
@ -93,10 +91,7 @@ describe('last modified header', function() {
api_key: 1234
});
var fixedDateNow = Date.now();
var dateNowFn = Date.now;
Date.now = function() {
return fixedDateNow;
};
MockDate.set(fixedDateNow);
assert.response(server,
{
url: '/api/v1/sql?' + query,
@ -109,7 +104,7 @@ describe('last modified header', function() {
statusCode: 200
},
function(err, res) {
Date.now = dateNowFn;
MockDate.reset();
assert.equal(res.headers['last-modified'], new Date(fixedDateNow).toUTCString());
done();
}