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 Issue - ReferenceError: capacitorExports is not defined

See original GitHub issue

Describe the bug When I test against a module using this plugin, jest throws an error after the import statement:

  ● Test suite failed to run

    ReferenceError: capacitorExports is not defined

    > 5 | import {BarcodeScanner} from "@capacitor-community/barcode-scanner";
        | ^
      6 | import {Capacitor} from "@capacitor/core";

      at Object.<anonymous> (node_modules/@capacitor-community/barcode-scanner/dist/plugin.js:68:7)
      at Object.<anonymous> (src/views/myview.ts:5:1)
      at Object.<anonymous> (src/views/myview.test.ts:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)

To Reproduce Run jest against a module using this plugin

Expected behavior No error from jest, I don’t test this plugin using it.

Version 2.0.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
mike-caocommented, Sep 9, 2021

Is it just me or is anyone else experiencing this error when running their web app in general and not just with Jest? It used to be fine (as far as I know as it was working before) but perhaps some code updates I’ve recently made in my project seems to have caused my build (Ionic 5, Capacitor 3, Angular 12 with SSR) to get the “capacitorExports is not defined” message when compiled. As a temporary fix, I’ve commented out the lines referencing this plugin since I’m not using it in my PWA anyway, but I’d like to not have to always remember to uncomment when building the native versions. I’m thinking perhaps it’s a TSconfig issue? Is there a workaround similar to the one for jest as @thequailman shared? Thanks in advance!

1reaction
thegnuucommented, Feb 27, 2022

For me it looks like mocking the module would be the right solution for this issue, proper tests will require a mocked module anyway since it is not testable otherwise by jest.

I will add @leticiatomasio 's solution to the docs next week!

Read more comments on GitHub >

github_iconTop Results From Across the Web

capacitorExports is not defined" while running jest tests in a ...
I got this horrible "ReferenceError: capacitorExports is not defined" error while running the tests in my Ionic/Angular/Capacitor project ...
Read more >
'ReferenceError: jest is not defined' when running unit test
I'm confident that the issue has to do with the steps I followed to support ES6 modules. Prior to these changes, my test...
Read more >
Saninn Salas Diaz | Facebook
Method overloading in TypeScript is a great way to define dynamic call signatures. ... "ReferenceError: capacitorExports is not defined" while running jest ......
Read more >
Referenceerror: $State Is Not Defined - ADocLib
ReferenceError : capacitorExports is not defined while running jest tests in a part of building complete applications and software systems using Node. 1....
Read more >
[Solved]-Jest referenceError 'is not defined'-Reactjs
You should mock or define this class globally while testing. I've never faced this particular problem, but I think you can try this:...
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