node.js - 如何在 Jest 测试中发送 Node 命令行参数

标签 node.js jestjs

我有一个 utility.js,它通过 process.argv 使用程序参数。

当我运行 jest -t 'utility-spec' --arg1 value1 --arg2 value2 时,它抛出异常。无法运行测试。

(node:8956) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ● Unrecognized CLI Parameters:

Following options were not recognized: ["arg1", "arg2"]

最佳答案

刚刚遇到同样的问题并找到了答案:

Jest doesn't accept node arguments unfortunately. I recommend testing your utility by making a programmatic interface. You can also overwrite process.argv in your test before requiring your module.

Issue 5089

好像不可能。

关于node.js - 如何在 Jest 测试中发送 Node 命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47610880/

相关文章:

node.js - 错误 : Cannot find module './lib/socket.io'

vue.js - 如何使用 jest 使测试覆盖显示 Vue-cli 3 中的所有 vue 文件

jestjs - 松散匹配 jest.toHaveBeenCalledWith 中的一个值

jestjs - Jest 24.0.0 Plugin/Preset 文件不允许导出对象,只能导出函数

node.js - 使用 Node.js 和 Express 在 Twilio 中请求未定义

node.js - λENOENT : no such file or directory

javascript - 无法从 handsontable 访问返回的 json 数据

javascript - date.toLocaleString ('en-us' , { 日期样式 : 'long' }) not working in jest test but working in browser

unit-testing - 如何用 Jest 模拟导入

Node.js mysql 模块中的 MySQL 用户定义变量