Explosion Pills
Explosion Pills

Reputation: 191799

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?

Upvotes: 9

Views: 11354

Answers (3)

Kalle Richter
Kalle Richter

Reputation: 8778

In case you're experiencing this for

/node_modules/@nestjs/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';
                                                                                  ^^^^^^

the fix is to add

moduleNameMapper: {
  '^uuid$': require.resolve('uuid'),
}

to the jest configuration, e.g. to jest.config.js.

Thanks to contributors at https://github.com/nestjs/nest/issues/9930.

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191799

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.

Upvotes: 4

sadface.png
sadface.png

Reputation: 17

i would like to add that for babel 7+ i guess, instead of .babelrc use babel.config.js:

module.exports = {
    presets: ['babel-preset-env'],
}

Upvotes: -1

Related Questions