admin管理员组

文章数量:1291786

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 ing 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?

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 ing 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?

Share Improve this question asked Oct 4, 2018 at 16:54 Explosion PillsExplosion Pills 192k55 gold badges340 silver badges416 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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.

In case you're experiencing this for

/node_modules/@nestjs/mon/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./nestjs/nest/issues/9930.

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

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

本文标签: javascriptUnexpected token export with jestStack Overflow