question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on import module to test in Jest

See original GitHub issue
[X] Bug
[ ] Build issue
[ ] Feature request

Version info

Angular: 7
Angular CLI: 7.3.1
countup.js-angular2:  7.1.1
Jest: 24.5.0

Description

Hi, I’m having an issue to run tests with Jest in a component that I’m using the lib. The error log from Jest is described below. Can you help me? Thanks.

src/app/components/limit-bar/limit-bar.component.spec.ts ● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • 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.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

C:\Development\neon-pj\node_modules\countup.js\dist\countUp.min.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var i,a=1,s=arguments.length;a<s;a++)for(var n in i=arguments[a])Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n]);return t}).apply(this,arguments)},CountUp=function(){function t(t,i,a){var s=this;this.target=t,this.endVal=i,this.options=a,this.version="2.0.4",this.defaults={startVal:0,decimalPlaces:0,duration:2,useEasing:!0,useGrouping:!0,smartEasingThreshold:999,smartEasingAmount:333,separator:",",decimal:".",prefix:"",suffix:""},this.finalEndVal=null,this.useEasing=!0,this.countDown=!1,this.error="",this.startVal=0,this.paused=!0,this.count=function(t){s.startTime||(s.startTime=t);var i=t-s.startTime;s.remaining=s.duration-i,s.useEasing?s.countDown?s.frameVal=s.startVal-s.easingFn(i,0,s.startVal-s.endVal,s.duration):s.frameVal=s.easingFn(i,s.startVal,s.e

SyntaxError: Unexpected token export

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:451:17)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:493:19)
  at Object.<anonymous>.__assign (node_modules/countup.js-angular2/bundles/countup.js-angular2.umd.js:2:111)
  at Object.<anonymous> (node_modules/countup.js-angular2/bundles/countup.js-angular2.umd.js:5:2)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
inorganikcommented, Apr 17, 2019

Ok, I got it working. Also I pushed up my changes in the test app included in this repo, so it has a passing test with Jest, as well as a new section in the README.

For Jest, make sure you have the following in your jest.config.js:

transform: {
  '^.+\\.(js|ts|tsx)$': 'ts-jest'
},
transformIgnorePatterns: ['node_modules/(?!countup.js)']

Also! In your root tsconfig.json, add

    "allowJs": true,

under compilerOptions

3reactions
inorganikcommented, Apr 17, 2019

I was able to reproduce, I’ll see what I can figure out

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve "Cannot use import statement outside a ...
"SyntaxError: Cannot use import statement outside a module" error while testing React Native project with Jest and @testing-library/react-native ...
Read more >
Cannot use import statement outside a module · Issue # ...
Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest...
Read more >
jest cannot use import statement outside a module
Jest doesn't support ES6 module and hence throwing this error when you directly run the test with Jest. if you want to run...
Read more >
ECMAScript Modules
Since ESM evaluates static import statements before looking at the code, the hoisting of jest.mock calls that happens in CJS won't work for...
Read more >
import module problem when doing js unittest/debug using ...
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found