Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token export with jest

I've seen some other answers and GitHub issues that describe this, but I haven't been able to find a solution in my case. I'm getting the dreaded SyntaxError: Unexpected token export when trying to run jest.

project/node_modules/@agm/core/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './directives';

The code where these tests are being run works okay. The error seems to be coming from within jest itself. It seems like jest is transforming the file, but I could be wrong.

My jest configuration is

"jest": {
  "preset": "jest-preset-angular",
  "setupTestFrameworkScriptFile": "<rootDir>/setup-jest.ts",
  "transformIgnorePatterns": [
    "node_modules"
  ]
}

I've tried updating the transformIgnorePatterns to "<rootDir>/node_modules/(?!@agm)" and "node_modules/(?!@agm)", "node_modules/(?!@agm/core)", but none of those seem to make any difference.

How can I get jest to properly handle the files imported from @agm/core?

like image 233
Explosion Pills Avatar asked Oct 04 '18 16:10

Explosion Pills


1 Answers

There are several changes that I needed to get this to work. This should fix the issue when using @agm/core for an Angular 6 app is being used with jest.

yarn add --dev babel-preset-env

You should already have babel-jest.

Then update your jest configuration:

"transform": {
  "^.+\\.js": "<rootDir>/node_modules/babel-jest"
},
"transformIgnorePatterns": [
  "<rootDir>/node_modules/(?!@agm)"
]

You will also need to add a .babelrc to use if you don't have one:

{
  "presets": ["babel-preset-env"]
}

This may work for other libraries that need a babel transformation as-installed.

like image 150
Explosion Pills Avatar answered Oct 13 '22 07:10

Explosion Pills