1

I am trying to write a test for a function with @aws-sdk and Node.js. However, I came across this error.

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

I have googled for some recommendations; however, none of them worked. The Jest is "jest": "^29.6.2", The was-sdk is "@aws-sdk/client-s3": "^3.354.0",

The babel config in package.json is:

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

The jest config in package.json is:

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

The code about requiring aws-sdk is:

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

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

Anyone has an idea on potential solutions?

I tried to modify

"transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!(@aws-sdk/client-s3))"
    ]

But none of them worked

Aaron Zhao
  • 11
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 10 '23 at 06:21

0 Answers0