Skip to content
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

bug: "CustomElements not defined" while executing the unit tests. #24149

Closed
4 of 6 tasks
anantsharma1310 opened this issue Nov 2, 2021 · 9 comments · Fixed by #24156
Closed
4 of 6 tasks

bug: "CustomElements not defined" while executing the unit tests. #24149

anantsharma1310 opened this issue Nov 2, 2021 · 9 comments · Fixed by #24156
Labels
package: react @ionic/react package type: bug a confirmed bug report
Milestone

Comments

@anantsharma1310
Copy link
Contributor

anantsharma1310 commented Nov 2, 2021

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x

Current Behavior

We were executing our unit tests using Mocha test runner but after importing @ionic/react (version: 5.8.4) in our components, mocha started to throw error before getting to any test case. Everything else is working fine on web and IOS/Android devices, but we are not able to get a workaround for our unit tests. We have debugged ourselves and we like to share our findings. customElements is a method available in a window object. Since we are using a jsdom, we do get window.customElements in our test cases. However, in ionic code, we found that there was a piece of code which is accessing customElements method using window in if condition and inside if block, it is using this reference to access customElements method which will definitely throw an error since this is not equal to window in the test runner env.
Attaching the buggy code, we can also post a PR for the fix if it's too much work but if possible, please expedite it.

Thank you

if (typeof window !== 'undefined' && window.customElements) {
    const element = customElements.get('ion-tabs'); // should be window.customElements
    if (!element) {
        customElements.define('ion-tabs', IonTabsElement); / should be window.customElements
    }
}

Expected Behavior

Should execute in test env as well.

Steps to Reproduce

You can use any test runner to run any dummy test case, it should throw an error right if you are testing component has imported @ionic/react.

Code Reproduction URL

No response

Ionic Info

@ionic/react": 5.8.4
jsdom: 16.6.0
mocha: 8.4.0

Additional Information

No response

@ionitron-bot ionitron-bot bot added the triage label Nov 2, 2021
@liamdebeasi liamdebeasi added the ionitron: needs reproduction a code reproduction is needed from the issue author label Nov 2, 2021
@ionitron-bot
Copy link

ionitron-bot bot commented Nov 2, 2021

Thanks for the issue! This issue has been labeled as needs reproduction. This label is added to issues that need a code reproduction.

Please reproduce this issue in an Ionic starter application and provide a way for us to access it (GitHub repo, StackBlitz, etc). Without a reliable code reproduction, it is unlikely we will be able to resolve the issue, leading to it being closed.

If you have already provided a code snippet and are seeing this message, it is likely that the code snippet was not enough for our team to reproduce the issue.

For a guide on how to create a good reproduction, see our Contributing Guide.

@ionitron-bot ionitron-bot bot removed the triage label Nov 2, 2021
@anantsharma1310
Copy link
Contributor Author

Submitted the pull request for the fix. Please take a look, these are the small changes but unblock us big time.

#24156

Let us know if we still need to provide the code reproduction url.

@liamdebeasi
Copy link
Member

Hi there,

Yes, we still need a code reproduction so we can verify the fix. Can you please send over a GitHub repo?

@anantsharma1310
Copy link
Contributor Author

Hey

I appreciate your prompt response.
I have created a sample repo on Github with instructions to replicate the issue.

https://github.com/anantsharma1310/ionic-react-unit-test

Do let us know if you guys need something else.

Thank you

@liamdebeasi liamdebeasi added triage and removed ionitron: needs reproduction a code reproduction is needed from the issue author labels Nov 15, 2021
@liamdebeasi
Copy link
Member

Can you double check your repo? I get the following error when running npm test:

SyntaxError: /Users/liamdebeasi/Ionic/ionic-react-unit-test/src/App.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (16:29):

  14 | describe("Testing <App/> Component", () => {
  15 |    it("App renders a message", () => {
> 16 |       const wrapper = mount(<App />);
     |                             ^
  17 |       expect(wrapper.find('p[data-testid="message"]')).to.be.exist;
  18 |    });
  19 | });

@liamdebeasi liamdebeasi added the needs: reply the issue needs a response from the user label Nov 15, 2021
@ionitron-bot ionitron-bot bot removed the triage label Nov 15, 2021
@rish-dario
Copy link

rish-dario commented Nov 16, 2021 via email

@ionitron-bot ionitron-bot bot added triage and removed needs: reply the issue needs a response from the user labels Nov 16, 2021
@liamdebeasi
Copy link
Member

Thanks! I can reproduce this behavior. This indeed looks like a bug.

@liamdebeasi
Copy link
Member

Thanks for the issue. This has been resolved via #24156, and a fix will be available in an upcoming release of Ionic Framework. Thank you for the PR!

@liamdebeasi liamdebeasi modified the milestones: 6.0.0-rc.4, 5.9.2 Dec 6, 2021
@ionitron-bot
Copy link

ionitron-bot bot commented Jan 5, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Jan 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: react @ionic/react package type: bug a confirmed bug report
Projects
None yet
3 participants