Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Jest has issues with this package. #29

Closed
sta55en opened this issue Jul 7, 2020 · 3 comments
Closed

Jest has issues with this package. #29

sta55en opened this issue Jul 7, 2020 · 3 comments

Comments

@sta55en
Copy link

sta55en commented Jul 7, 2020

Describe the bug

When I try to test code that references a service, which in turn uses this package, I get the following error:

● 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:

/Users/xxxxx/Code/xxxxxx/node_modules/@capacitor-community/http/dist/esm/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './web';
                                                                                         ^^^^^^

SyntaxError: Unexpected token export

  1 | import { Plugins, FilesystemDirectory } from '@capacitor/core';
> 2 | import "@capacitor-community/http";
    | ^
  3 | import pako from 'pako';
  4 | 
  5 | const ManifestDownloadService = () => {

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
  at Object.<anonymous> (src/services/manifestDownloadService.ts:2:1)

Expected behavior
I would expect this to work without failing the tests.

Desktop (please complete the following information):

  • OS: OSX
  • Browser N/A

Additional context

Ionic:

Ionic CLI : 6.8.0 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/react 5.2.2

Capacitor:

Capacitor CLI : 2.2.0
@capacitor/core : 2.2.0

Utility:

cordova-res (update available: 0.15.1) : 0.6.0
native-run (update available: 1.0.0) : 0.2.7

System:

NodeJS : v10.16.3 (/usr/local/bin/node)
npm : 6.13.4
OS : macOS Catalina

@RTK
Copy link

RTK commented Jul 7, 2020

Try the following: In your test tsconfig add this line "allowJs": true to the compiler options. Next up add the plugin ref to your jest blacklist:

    transformIgnorePatterns: [
        '/node_modules/(?!capacitor-community).+\\.js$'
    ],

(in this case all capacitor-community plugins will be added to the blacklist)

For more information regarding this error: jestjs/jest#2550

@thomasvidas
Copy link
Contributor

thomasvidas commented Jan 19, 2021

This is due to the fact that this plugin is distributed as an ES Module. You should use transformIgnorePatterns as described above to let jest do its thing. It is described in the jest docs here: https://jestjs.io/docs/en/configuration#transformignorepatterns-arraystring

@imhoffd
Copy link
Contributor

imhoffd commented Jan 19, 2021

Our latest plugin template changes also ship CommonJS now. It looks like the broader JavaScript community isn't quite ready for ESM-only yet. 😞

See the following for reference:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants