harunurhan / idlejs

Execute stuff when user is idle or interactive
MIT License
37 stars 6 forks source link

idlejs does not work with Jest (https://jestjs.io/) #10

Closed pyou10622 closed 4 years ago

pyou10622 commented 4 years ago

Our Angular application uses Jest for unit testing, and we found out that idlejs library does not work with Jest. See errors below:

● 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:\dev\src\trunk\my-apps\node_modules\idlejs\dist\index.js:1
export { Idle } from './idle';
^^^^^^

SyntaxError: Unexpected token 'export'

>  8 | import {Idle, NotIdle} from 'idlejs/dist';
     | ^
   9 | 
  10 | @Injectable({
  11 |   providedIn: 'root'

  at Runtime._execModule (../../node_modules/jest-runtime/build/index.js:1179:56)
pyou10622 commented 4 years ago

We tried each of the suggested workarounds and nothing worked.

harunurhan commented 4 years ago

You can fix it by playing with your config, to transform the library code prior to running your tests.

Although #7 will probably fix your issue too.

harunurhan commented 4 years ago

I just published a version with multiple builds (cjs and esm), you can try again with v3.