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

使用@aws-sdk与Node.js编写Jest测试时遇意外token错误求助

问题:Jest测试@aws-sdk相关Node.js函数时出现Unexpected token 'export'错误

错误日志

● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/Z/Documents/projects/TrampingClub/node_modules/@aws-sdk/middleware-retry/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      4 |   // GetObjectCommand,
      5 |   DeleteObjectCommand,
    > 6 | } = require('@aws-sdk/client-s3')
        |     ^
      7 |
      8 | const {
      9 |   CloudFrontClient,

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1496:14)
      at Object.require (node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js:7:16)
      at Object.require (node_modules/@aws-sdk/middleware-retry/dist-cjs/AdaptiveRetryStrategy.js:5:33)
      at Object.require (node_modules/@aws-sdk/middleware-retry/dist-cjs/index.js:4:22)
      at Object.require (node_modules/@aws-sdk/client-s3/dist-cjs/S3Client.js:12:28)
      at Object.require (node_modules/@aws-sdk/client-s3/dist-cjs/index.js:5:22)
      at Object.require (server/s3.js:6:5)
      at Object.require (server/routes/albumRoutes.js:6:64)
      at Object.require (server/server.js:5:21)
      at Object.require (server/routes/__test__/userRoutes.test.js:2:16)

当前环境版本

  • Jest: ^29.6.2
  • @aws-sdk/client-s3: ^3.354.0

现有配置

package.json中的Babel配置

"babel": {
    "presets": [
      "@babel/preset-typescript",
      "@babel/preset-env",
      "@babel/preset-react"
    ]
  },

package.json中的Jest配置

"jest": {
    "testEnvironment": "jsdom",
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!(@aws-sdk/client-s3))"
    ]
  },

引入@aws-sdk的代码

const {
  S3Client,
  PutObjectCommand,
  // GetObjectCommand,
  DeleteObjectCommand,
} = require('@aws-sdk/client-s3')

const {
  CloudFrontClient,
  CreateInvalidationCommand,
} = require('@aws-sdk/client-cloudfront')

已尝试的操作

修改transformIgnorePatterns仅包含@aws-sdk/client-s3,但问题未解决。

解决方案

1. 扩展Jest的转译忽略规则

错误根源是@aws-sdk/middleware-retry依赖的uuid模块使用了ES模块语法,而Jest默认未转译这些嵌套依赖。修改transformIgnorePatterns,让所有@aws-sdk子包和uuid都被转译:

"jest": {
  "testEnvironment": "jsdom",
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  },
  "transformIgnorePatterns": [
    "<rootDir>/node_modules/(?!(@aws-sdk/|uuid/))"
  ]
}

2. 优化Babel预设配置

确保@babel/preset-env针对当前Node.js环境进行转译,避免不必要的兼容性处理:

"babel": {
  "presets": [
    "@babel/preset-typescript",
    ["@babel/preset-env", {
      "targets": {
        "node": "current"
      }
    }],
    "@babel/preset-react"
  ]
}

3. 调整测试环境(可选)

如果测试的是Node.js后端函数,无需浏览器环境,可将testEnvironment改为node,减少环境冲突:

"jest": {
  "testEnvironment": "node",
  "transform": {
    "^.+\\.jsx?$": "babel-jest"
  },
  "transformIgnorePatterns": [
    "<rootDir>/node_modules/(?!(@aws-sdk/|uuid/))"
  ]
}

内容的提问来源于stack exchange,提问作者Aaron Zhao

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 21:15:39