Javascript 如何在NodeJS上更改流程日期,而不是直接更改系统日期?
我想做一些测试,在使用date类时需要更改进程日期:Javascript 如何在NodeJS上更改流程日期,而不是直接更改系统日期?,javascript,node.js,date,process,environment-variables,Javascript,Node.js,Date,Process,Environment Variables,我想做一些测试,在使用date类时需要更改进程日期: console.log(Date.now()) 正常运行我的程序进程将获得机器当前时间: "scripts": { "start": "node testDate.js" } 是否有类似cross env的方法来更改初始流程日期 "scripts": { "start": "cross-env CURRENT_DATE=<future-date> node testDate.js" } “脚本”:{ “开始”:“
console.log(Date.now())
正常运行我的程序进程将获得机器当前时间:
"scripts": {
"start": "node testDate.js"
}
是否有类似cross env的方法来更改初始流程日期
"scripts": {
"start": "cross-env CURRENT_DATE=<future-date> node testDate.js"
}
“脚本”:{
“开始”:“交叉环境当前_日期=节点testDate.js”
}
或者在运行时有解决方案吗
NodeJS可以像这样更改进程日期吗
process.DATE_CLOCK=Date("<future-date>")
console.log(Date.now())// will print the future date + time passed after previous line has been executed
process.DATE\u CLOCK=DATE(“”)
console.log(Date.now())//将打印执行前一行后经过的未来日期+时间
我认为通过现有的节点env-var不可能做到这一点,但您应该这样做
签出包裹
下面是一个在单元测试中使用的示例
// mut.js
module.exports = {
getCurrentDate: () => Date.now()
}
// mut.test.js
const mut = require('./mut');
const mockdate = require('mockdate');
describe('mut', () => {
const mockedTime = 1609865118363;
it('should return adjusted date', () => {
mockdate.set(mockedTime);
expect(mut.getCurrentDate())
.toEqual(mockedTime);
});
it('should return current date/time', () => {
mockdate.reset();
expect(mut.getCurrentDate())
.not.toEqual(mockedTime);
});
});
如果您坚持通过节点环境添加它,您可以添加一个类似于Sengupta Amit comment的流程环境检查,并包装您的应用程序入口点。这将设置与上述要求类似的时间
//testDate.js
// CURRENT_DATE=1609865118363 node ./testDate.js
const mockdate = require('mockdate');
const mut = require('./mut');
const currentDate = process.env.CURRENT_DATE;
if (currentDate) mockdate.set(parseInt(currentDate));
console.log(mut.getCurrentDate()); // will log 1609865118363 to console
编辑:
我们还使用Express和Node Http服务器对此进行了测试。它仍然有效,并将返回日期标题和通过mockdate设置的时间
// CURRENT_DATE=1609865118363 node testDate.js
const app = require('express')();
const mockdate = require('mockdate');
const currentDate = process.env.CURRENT_DATE;
if (currentDate) mockdate.set(parseInt(currentDate));
app.get('/', (req, res) => res.send());
app.listen(3000, (err) => {
if(err) console.log(err);
})
您可以在node.js调用后向节点脚本发送参数。例如:
$node testDate.js 1520978956795
在节点中,您需要将此时间戳字符串解析为数字到日期构造函数:var\u Date=new Date(parseFloat(process.argv[2])代码>和新的日期对象将带有作为参数发送的时间戳。对于将来的日期,您可以发送时间戳偏移量,您可以将其添加到日期中。现在()是的,我知道!但我想要一个全局解决方案来模拟具有未来值或任何其他值的日期!而且代码的每一部分都不会改变!您找到解决方案了吗?可以在文件顶部添加if(process.env.testFlag==true))Date.now=()=>{return new Date().getTime()-60*60*1000;}
对于Node.js内部设置的日期不起作用,例如http服务器设置的日期头,因为这些日期由节点内部维护,而不是由用户外部维护。(mockdate是@sinon/fake timers的一个不太流行的版本,jest也使用了它,我帮助维护它,它采用了类似的方法)已经测试了Express和plain vanilla http服务器,它将覆盖响应头中的时间。我可能没有正确解释您评论的上下文,您是对的,可能有些情况下这可能不起作用。在modern Node(>15.2.0)上尝试一下,我添加了赏金,以查看是否有人可以解决此问题
const mockdate = require('mockdate');
const currentDate = process.env.CURRENT_DATE;
if (currentDate) mockdate.set(parseInt(currentDate));
const http = require('http');
http.createServer(function (req, res) {
res.writeHead(200);
res.end();
}).listen(8080);