2

I am importing some functions from the library which i have added as dependency but when I run test then jest is throwing error saying "jest encountered an unexpected token"

Below is the error details

Details:

C:\Users\sbhuyar\ami-ui-booking-analysis_angular\node_modules\@vana\ami-ui-authentication\src\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { USER_LOGGED_OUT_ERROR, DEV_ENV } from './constants';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

> 1 | import { initialize, login } from '@vana/ami-ui-authentication';
    | ^
  2 | const domainSuffix = '.travel-intelligence.com';
  3 | export const DEV_ENV = '__dev__';
  4 |

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (src/api/env.js:1:1)

FAIL src/app/login/loginController.test.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.

1 Answer 1

0

Since jest by default ignores node_modules and does not transform it with Babel, I believe the necessary solution is to manually ensure jest transforms this module with Babel since Jest does not natively support import or export.

I would refer to this answer: Unexpected token 'import' error while running Jest tests?

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – sallf
    Commented Jul 9 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.