-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest has problems with the IIFE module of dist/capacitor.js #1542
Comments
@simonhaenisch you were using jest successfully, right? |
I suggested in the other issue (which is still on the Stencil repo), to try adding a file export const Plugins = {}; to mock it out. |
ah, sorry, didn't notice you already answered there, thanks! |
@simonhaenisch Thanks for the help! This did the trick! |
@jcesarmobile the mock is a way to get around it, however I think there's still some issue with capacitor and commonjs, because I don't think that mock should be necessary... it also just makes the tests pass because the object destructuring from I think the proper fix would be to make sure it exports properly for cjs, because then the proxy would be working when accessing plugins. But I'm not so experienced with this, maybe Manu can help. |
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) |
Made a few updates so browser/esm/cjs all work, which should allow Jest to pull in the cjs format: 814db1f Biggest change is the various entry points bundlers would check first: Lines 23 to 26 in 814db1f
“main” is what node/jest would use, and is in the commonjs format (which we didn’t have), "module" is esm for modern node and bundling, and browser/unpkg would be what bundlers would use for a browser build. @simonhaenisch would you be able to double check this and make sure it's the right solution and jest/browser/webpack all find the correct formats? Thanks |
Thanks @adamdbradley I'll give it a try |
@adamdbradley yup, seems to work fine! At least it still builds and all our tests pass (without the mock) (: |
thanks for testing, closing then. |
@jcesarmobile this hasn't actually been merged yet... I just opened a PR (#1575) because Adam didn't do it yet. Thanks! |
Oops, I thought I saw the commits, too much activity in the latest few days. |
For anyone finding this issue not resolved, I managed to fix the problem locally with the following lines: jest.config.js
|
It was fixed after the final release, so should be good on next release. |
Description of the problem:
Testing a compontent or service that uses a Plugin with Jest fails with following message:
Affected platform
OS of the development machine
Other information:
I am using a NX workspace.
Versions:
As mentioned in issue ionic-team/stencil#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:
npx create-nx-workspace capacitor-reproduce
cd capacitor-reproduce\
npm install --save @capacitor/core @capacitor/cli
npx cap init
ng g application example
apps/example/src/app/app.component.ts
a. Add
import { Plugins } from '@capacitor/core'
andconst { Device } = Plugins;
ng test
Link to sample project: https://github.com/WoMayr/reproduce-jest-capacitor
The text was updated successfully, but these errors were encountered: