googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.26k stars 1.91k forks source link

Jest encountered an unexpected token - Unexpected token 'export' #3501

Open loralameh opened 2 weeks ago

loralameh commented 2 weeks ago

I am trying to run a jest test on my functions that are using "googleapis" and get the following error ` 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:

/app/node_modules/googleapis-common/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'

`

I have tried adding

  transform: {
    '^.+\\.(t)s$': 'ts-jest',
    '^.+\\.(js|jsx)$': 'babel-jest'
  }
  transformIgnorePatterns: ['/node_modules/(?!googleapis|googleapis-common/)']

but this did not solve the problem

I am using

"googleapis": "105",
"jest": "^29.7.0",
"babel-jest": "^29.7.0",
"ts-jest": "^29.1.4"

so mainly from what i understand from the problem and what i have researched that googleapis is using googleapis-common node module and they have not pre-compiled their node module into standard JS with module.exports instead of export {} syntax,

NOTE: I do not want to mock the 'googleapis'