node.js - 运行 Jasmine 测试时如何查看“console.log”的输出
问题描述
我开发了一个 Node.js 模块,我正在使用 Jasmine 为其编写单元测试。该模块用于在使用设置为 trueconsole.log
的参数调用时打印出一些信息。verbose
假设模块如下所示:
foo.js
function foo(arg1, verbose){
var output = "Hello " + arg1;
if(verbose)console.log("Returning %s", output);
return output;
}
module.exports = foo;
假设我的测试如下所示:
foo-spec.js
const foo = require("path/to/foo")
describe("Foo suite", () => {
it( "Expect foo to greet", () => {
expect(foo("World", true)).toBe("Hello World");
});
});
jasmine
我通过在终端中输入来运行我的测试:
$ jasmine
一切都很好,除了我想看到详细的输出。
$ jasmine
Returning Hello World
有没有办法让 Jasmine 做到这一点?
解决方案
好的,我已经找到了解决方法。监视以console.log
某种方式修补了它。
foo-spec.js
const foo = require("path/to/foo")
describe("Foo suite", () => {
it( "Expect foo to greet", () => {
//This makes the log visible again from the command line.
spyOn(console, 'log').and.callThrough();
expect(foo("World", true)).toBe("Hello World");
});
});
不知道为什么,但是spyOn
使日志再次可见。尽管我并没有真正用它做任何事情,除了调用callThrough
. 我最好的猜测是,通过这样做,console.log
实际上是从 Jasmine 进程中调用的。
推荐阅读
- python - Wagtail 管理员:如何自定义选择/复选框选项的布局?
- sql - 使用 INTO 将 SQL 查询从 DBEAVER 导出为 CSV
- python - 从 IPyWidget 嵌入式 JS 到 python 共享变量
- node.js - iconv 模块问题 nodejs
- python - 如何在 Datatable(Dash/Plotly)内的下拉列表中选择值
- javascript - 运算符在 JavaScript 中是否以这种方式工作?
- azure-devops - 如何在部署时使用变量而不是 XML 转换来转换 Web.Config?
- azure-ad-b2c - 连接两个自定义策略
- java - Spring MVC 5.3.2 java.lang.NullPointerException:无法调用,因为 NULL
- google-apps-script - 从(Gmail)电子邮件正文中解析 HTML 表格并粘贴到 Google 表格