You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Jest使用指定Node版本?Node14 Lambda测试失败排查

问题

我有一个基于Node.js 14.x Runtime的JS Lambda函数,使用Jest 24.9版本进行测试,函数依赖activedirectory 0.7.2包。

当我在本地使用Node v12.16.1运行测试时,出现如下语法错误:

Details:

    C:\...\...\...\...\...\node_modules\@ldapjs\messages\lib\ldap-message.js:27
        this.#messageId = parseInt(options.messageId ?? options.messageID ?? '1', 10)
                                                      ^

    SyntaxError: Unexpected token '?'

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (node_modules/@ldapjs/messages/index.js:5:16)
      at Object.<anonymous> (node_modules/ldapjs/lib/messages/index.js:3:18)
      at Object.<anonymous> (node_modules/ldapjs/lib/errors/index.js:6:20)
      at Object.<anonymous> (node_modules/ldapjs/lib/client/request-queue/purge.js:3:26)
      at Object.<anonymous> (node_modules/ldapjs/lib/client/request-queue/index.js:5:15)
      at Object.<anonymous> (node_modules/ldapjs/lib/client/client.js:3:29)
      at Object.<anonymous> (node_modules/ldapjs/lib/client/index.js:4:16)
      at Object.<anonymous> (node_modules/ldapjs/lib/index.js:5:16)
      at Object.<anonymous> (node_modules/activedirectory/lib/activedirectory.js:3:12)

但使用Node v14.16.1运行测试时一切正常。我的Serverless配置中provider runtime已设置为nodejs14.x,但在Jenkins上部署时测试仍出现相同错误。

我已尝试在Jest配置中添加如下规则,但问题未解决:

"transformIgnorePatterns": [
      "/node_modules/(?!activedirectory)"
    ]

请问我该如何操作才能成功运行测试?

解决方案

问题核心是**空值合并运算符(??)**属于Node.js 14及以上支持的语法,测试环境(本地Node12、Jenkins)的Node版本不兼容,或Jest未正确转译依赖包中的新语法。以下是可行解决步骤:

  • 统一测试环境Node版本
    在Jenkins中配置使用Node.js 14.x版本执行测试,与Lambda Runtime保持一致。可通过nvm指定版本:

    nvm install 14.16.1
    nvm use 14.16.1
    

    本地开发也建议固定使用Node14,消除版本差异问题。

  • 调整Jest转译规则覆盖报错依赖
    之前的transformIgnorePatterns仅排除了activedirectory,但实际报错的是@ldapjs/messages,需要将该包加入转译白名单:

    "transformIgnorePatterns": [
      "/node_modules/(?!activedirectory|@ldapjs/messages)"
    ]
    

    同时确保Babel配置适配Node14,在babel.config.js中设置:

    module.exports = {
      presets: [
        ['@babel/preset-env', { targets: { node: '14' } }]
      ]
    };
    
  • 锁定依赖版本避免不兼容升级
    检查锁文件(package-lock.json/yarn.lock),确认@ldapjs/messages版本是否存在兼容性问题。如果activedirectory 0.7.2依赖的ldapjs版本引入了高版本@ldapjs/messages,可尝试降级该包或锁定ldapjs版本:

    "dependencies": {
      "activedirectory": "0.7.2",
      "ldapjs": "2.3.3" // 选择兼容Node12的稳定版本
    }
    
  • 临时方案:Mock依赖包
    若上述方法无法快速解决,可在Jest中Mock activedirectory,避免加载真实依赖:

    "moduleNameMapper": {
      "^activedirectory$": "<rootDir>/__mocks__/activedirectory.js"
    }
    

    在__mocks__/activedirectory.js中编写模拟实现,跳过真实包的加载逻辑。

内容的提问来源于stack exchange,提问作者GiovannyRamírez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 22:25:03