Jest has problems with the IIFE module of dist/capacitor.js
See original GitHub issueDescription of the problem: Testing a compontent or service that uses a Plugin with Jest fails with following message:
● Test suite failed to run
TypeError: Cannot read property 'App' of undefined
22 | import { StorageSelectors } from "../store/storage.selectors";
23 |
> 24 | const { App, Device, Browser } = Plugins;
| ^
25 |
26 | @Injectable({
27 | providedIn: "root"
Affected platform
- Android
- iOS
- electron
- web
- testing with jest
OS of the development machine
- Windows
- macOS
- linux
Other information: I am using a NX workspace. Versions:
- @nrwl/schematics: 7.8.6
- jest: 24.8.0
- jest-preset-angular: 7.1.1
As mentioned in issue https://github.com/ionic-team/stencil/issues/1234 the problem seems to be that the capacitor.js module (the IIFE module in the dist folder) doesn’t export anything. (module.exports
never gets written)
Capacitor version: 1.0.0-beta.25
node version: 6.8.0
npm version: 10.15.1
CocoaPods version: N/A
Steps to reproduce:
- Run
npx create-nx-workspace capacitor-reproduce
- SCSS
- some npm scope
- an empty workspace
- Run
cd capacitor-reproduce\
- Run
npm install --save @capacitor/core @capacitor/cli
- Run
npx cap init
- App
- com.example.app
- npm
- Run
ng g application example
- Angular
- default folder (just press enter)
- Jest
- Protractor
- app
- Edit the file
apps/example/src/app/app.component.ts
a. Addimport { Plugins } from '@capacitor/core'
andconst { Device } = Plugins;
- Run
ng test
- The before mentioned error happens here
Link to sample project: https://github.com/WoMayr/reproduce-jest-capacitor
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (11 by maintainers)
Top Results From Across the Web
How to mock an IIFE while importing in jest test - Stack Overflow
I have a JS file which contains multiple functions which I want to test except that IIFE. While importing the JS file in...
Read more >Troubleshooting - Jest
Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why. Try using the debugging support built into...
Read more >Testing Non-Exported Functions in JavaScript - Samantha Ming
Testing Non-Exported Functions in JavaScript. Recently, I finally integrated unit testing into my startup project. I've settled with Jest , I'll speak more ......
Read more >Mocking functions and modules with Jest - pawelgrzybek.com
There is plenty of JavaScript mocking libraries out there. Today I am going to review a few methods of creating functions and modules...
Read more >1 - Salesforce Stack Exchange
You're correct. If the stub is not provided for a standard module in the sfdx-lwc-jest repo, you'll need to create your own.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If I understood the rollup config correctly, @capacitor/core only gets compiled for IIFE which is not a CommonJS format and not usable in NodeJS applications (like Jest). An alternative would be to bundle capacitor as an UMD module which works with Jest (I tested this with the current master) and should theoretically also work in a browser. (http://rollupjs.org/guide/en/#output-format)
It was fixed after the final release, so should be good on next release.