node.js - 如何使用 jest 正确测试需要 newrelic 的模块

标签 node.js unit-testing newrelic jestjs

我正在努力将一堆单元测试从 mock 迁移到 Jest 。当我开 Jest 一个需要新遗物代理的模块时,如下所示:require('newrelic'),我收到如下下游错误:

  - TypeError: Cannot convert undefined or null to object
    at Object.<anonymous> (node_modules/newrelic/lib/config.js:165:33)
    at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
    at Object.<anonymous> (node_modules/newrelic/lib/logger.js:18:14)
    at Object.<anonymous> (node_modules/newrelic/index.js:3:14)

处理像 newrelic 这样的 Jest 很难 mock 的模块的最佳方法是什么?当其他人同时拥有 jest 和 newrelic 时,他们做了什么?

最佳答案

我最终采取的路线是在我的 __mocks__ 文件夹中为 newrelic 创建一个模拟模块:

module.exports = {
    addCustomParameter: jest.fn()
};

稍后我可能需要添加更多功能,但现在这已经足够了。我仍然想知道是否有一种方法可以让 Jest 自动模拟 newrelic 库而不出错。

关于node.js - 如何使用 jest 正确测试需要 newrelic 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37557331/

相关文章:

angular - 类型错误 : Cannot read property 'prototype' of undefined using jest

node.js - 在 cmd 中执行时,“webdriver-manager start”步骤卡住了

node.js - 未传递回调参数 (Node.js)

unit-testing - 错误 : Action may not be undefined in async action testing in Redux app with nock and mock-store

HTTP header "X-NewRelic-ID"... 这是什么?

涉及 New Relic 扩展的 Azure 部署错误?

javascript - NewRelic - 它有什么用?

sql-server - 在 SQL Server 2012 中使用 Node.js 进行身份验证

node.js - Node NV : command not found while trying to create virtualenv in node

php - 使用 Eclipse SimpleTest 插件 - SimpleTest 不工作