Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected identifier error when running jest test with babel env #541

Closed
shrutika-s opened this issue Dec 24, 2019 · 2 comments
Closed
Labels
external-issue issues caused by external system ( (no fix needed)

Comments

@shrutika-s
Copy link

shrutika-s commented Dec 24, 2019

I am new to Jest framework. I have followed the steps for installation from following blog:
https://www.valentinog.com/blog/ui-testing-jest-puppetteer/#UI_testing_with_Jest_and_Puppeteer_setting_up_the_project

when I run the test with command 'npm run test' I get following error:

● Test suite failed to run

E:\Jest Tutorial\getting-started-with-jest\__tests__\test1.form.spec.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import faker from "faker";
                                                                                                ^^^^^

SyntaxError: Unexpected identifier

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)

My package.json looks like this:

{
"name": "getting-started-with-jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^24.9.0",
"babel-preset-env": "^1.7.0",
"faker": "^4.1.0",
"jest": "^24.9.0",
"puppeteer": "^2.0.0"
}
}

and .babelrc looks like below:
{
"presets": ["env"]
}

Can someone let me know what I am missing?

@boatcoder
Copy link

boatcoder commented Feb 27, 2020

Different setup for me, but the same problem..... I'm using the babel config in package.json (created before ejecting from CRA)

"dependencies": {
   "babel-jest": "^25.1.0",
    "jest": "25.1.0",
    "jest-environment-jsdom-fourteen": "1.0.1",
    "jest-resolve": "25.1.0",
    "jest-watch-typeahead": "0.4.2"
},
"babel": {
    "plugins": [
      "@babel/plugin-proposal-class-properties" . // Why do I need this when react-app was sufficient BEFORE ejecting....
    ],
    "presets": [
      "react-app",
      "@babel/env"
    ]
}

@jtbandes
Copy link

I ran into a similar error when trying to import a package from node_modules whose index.js is an ES Module. After a lot of painful debugging, I was able to resolve it by moving my babel configuration out of package.json, into babel.config.js, as described here: jestjs/jest#6053 (comment) Combined with a custom transformIgnorePatterns (see here), this allowed babel-jest to correctly transform the file in node_modules.

@connectdotz connectdotz added the external-issue issues caused by external system ( (no fix needed) label Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external-issue issues caused by external system ( (no fix needed)
Projects
None yet
Development

No branches or pull requests

4 participants