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.

Jest has problems with the IIFE module of dist/capacitor.js

See original GitHub issue

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

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:

  1. Run npx create-nx-workspace capacitor-reproduce
    • SCSS
    • some npm scope
    • an empty workspace
  2. Run cd capacitor-reproduce\
  3. Run npm install --save @capacitor/core @capacitor/cli
  4. Run npx cap init
    • App
    • com.example.app
    • npm
  5. Run ng g application example
    • Angular
    • default folder (just press enter)
    • Jest
    • Protractor
    • app
  6. Edit the file apps/example/src/app/app.component.ts a. Add import { Plugins } from '@capacitor/core' and const { Device } = Plugins;
  7. Run ng test
    • The before mentioned error happens here

Link to sample project: https://github.com/WoMayr/reproduce-jest-capacitor

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
WoMayrcommented, May 22, 2019

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)

0reactions
jcesarmobilecommented, Jun 17, 2019

It was fixed after the final release, so should be good on next release.

Read more comments on GitHub >

github_iconTop 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 >

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