auth0/node-auth0

Tests failed when imported auth0: SyntaxError: Unexpected token 'export'

nosuchip opened this issue · 2 comments

Checklist

  • I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • I have looked into the API documentation and have not found a suitable solution or answer.
  • I have searched the issues and have not found a suitable solution or answer.
  • I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • I agree to the terms within the Auth0 Code of Conduct.

Description

When using auth0 package test cases failed due internal dependency syntax. Jest shows piece of code where it supposes error occurs but it is unrelated as by fact it is caused by something under the hood.

Reproduction

  1. Clone project from below
  2. Install dependencies via yarn
  3. Run yarn test

Test repo: https://github.com/nosuchip/auth0-jest-fail

Result:

yarn run v1.22.19
$ jest
 FAIL  src/utils/router.spec.ts
  ● 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:

    /home/alp/proj/ces/code/ts-auth0-test-fail/node_modules/jose/dist/browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { compactDecrypt } from './jwe/compact/decrypt.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

       9 |
      10 |   return management;
    > 11 | };
         |             ^
      12 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1496:14)
      at Object.<anonymous> (node_modules/jwks-rsa/src/utils.js:1:178)
      at Object.<anonymous> (node_modules/jwks-rsa/src/JwksClient.js:2:33)
      at Object.<anonymous> (node_modules/jwks-rsa/src/index.js:1:188)
      at Object.<anonymous> (node_modules/auth0/src/auth/OAUthWithIDTokenValidation.js:2:20)
      at Object.<anonymous> (node_modules/auth0/src/auth/OAuthAuthenticator.js:5:36)
      at Object.<anonymous> (node_modules/auth0/src/auth/index.js:7:28)
      at Object.<anonymous> (node_modules/auth0/src/management/ManagementTokenProvider.js:2:30)
      at Object.<anonymous> (node_modules/auth0/src/management/index.js:26:33)
      at Object.<anonymous> (node_modules/auth0/src/index.js:9:21)
      at Object.<anonymous> (src/utils/router.ts:11:16)
      at Object.<anonymous> (src/utils/router.spec.ts:5:17)

Additional context

I suspect it is somehow related to the jose that is dependency of jwks-rsa@^3.0.1.

node-auth0 version

3.5.0

Node.js version

18.13.0

Hi @nosuchip - thanks for raising this

I can't run your example (I get Module /home/alp/proj/ces/code/nop-member-ui/node_modules/next/dist/build/swc/jest-transformer.js in the transform option was not found.)

But you should use the node jest test environment to test node-auth0. Your error shows you're trying to bring in the browser version of jose (jose/dist/browser/index.js) because your test environment is emulating a browser.

Closing, as I believe #881 (comment) answers your question