小编mat*_*teo的帖子

有没有办法用Jest模拟私有函数?

我要测试的ES6模块如下所示:

function privateFunction() {
   ...
}
export function publicFunction() {
   ... does something ...
   privateFunction()
   ... does something else ...
}
Run Code Online (Sandbox Code Playgroud)

我正在使用JEST进行单元测试,我正试图找到一种方法来测试publicFunction并通过模拟它来避免执行privateFunction,但我无法在模拟尝试中成功.任何的想法?

javascript unit-testing jestjs es6-modules

23
推荐指数
4
解决办法
1万
查看次数

在Express节点中处理异常的任何其他方法?

以下是我用于处理/记录Express应用程序中所有可能异常的方法.还有其他方法吗?

处理错误是一个微妙的问题,尤其是在处理Express时.让我们从最简单的场景开始.想象一下,我们的应用程序如下:

console.log(ciao.ciao);    // EEERRROOORRR
Run Code Online (Sandbox Code Playgroud)

从控制台启动应用程序,我们得到以下内容:

console.log(ciao.ciao);
            ^
ReferenceError: ciao is not defined
Run Code Online (Sandbox Code Playgroud)

请注意,在上述情况下,它是un Uncaught Exception的一个示例,因为我们没有执行由于错误而执行的代码.要处理上面的错误,我们可以执行以下操作:

process.on('uncaughtException', function (err) {    
    console.log('error', ' * we did it, we handled the error * ', err.stack.trim());    
    process.exit(1);    
});

console.log(ciao.ciao);       // EEERRROOORRR
Run Code Online (Sandbox Code Playgroud)

如果我们现在启动我们得到的应用程序:

error  * we did it, we handled the error *  ReferenceError: ciao is not defined
Run Code Online (Sandbox Code Playgroud)

请注意!!! "process.on('uncaughtException'..."将被删除!!!

为了在弃用发生时做好准备,我们最好使用我们最喜欢的记录器的异常处理程序,它是功能强大且超级流行的winston.我们的申请成为:

var winston = require('winston');

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({ handleExceptions: true }),
  ]
});

console.log(ciao.ciao);         // EEERRROOORRR
Run Code Online (Sandbox Code Playgroud)

它的输出如下:

error: …
Run Code Online (Sandbox Code Playgroud)

error-handling node.js uncaught-exception express winston

5
推荐指数
1
解决办法
2549
查看次数

如何使用节点检索PayPal REST Api访问令牌

如何通过使用节点获得利用REST Api所需的PayPal访问令牌?

paypal node.js npm-request

3
推荐指数
3
解决办法
2716
查看次数