Jest 在使用 @aws-sdk 编写测试时遇到意外标记

问题描述 投票:0回答:1

我正在尝试使用 @aws-sdk 和 Node.js 编写函数测试。但是,我遇到了这个错误。

● 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)

我在谷歌上搜索了一些建议;然而,它们都不起作用。 笑话是“笑话”:“^29.6.2”, was-sdk 是“@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": [
      "<rootDir>/node_modules/(?!(@aws-sdk/client-s3))"
    ]

但是没有一个起作用

jestjs aws-sdk unexpected-token
1个回答
0
投票

在您的 jest.config.(js|ts) 文件中

moduleNameMapper: {
  '^uuid$': require.resolve('uuid'),
},
© www.soinside.com 2019 - 2024. All rights reserved.