Skip to content

How to Test Custom Element in Jest #10

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

Open
sidharthramesh opened this issue Feb 3, 2021 · 3 comments
Open

How to Test Custom Element in Jest #10

sidharthramesh opened this issue Feb 3, 2021 · 3 comments

Comments

@sidharthramesh
Copy link

sidharthramesh commented Feb 3, 2021

Hey! Thank you for this very useful repo.
I see that you've mentioned in a TODO on the Readme that you'll add more documentation testing web components. I'm struggling with this myself. I am already using the Testing Library, but it doesn't see to work properly with custom elements.

How do I import a custom element in Jest and use it? I'm not exporting everything as custom elements, and am using this recipe to only expose things that I need as web components instead of having all child components also be custom elements.

Now how do I import this and set it up in Jest? Please give me an idea on how to get started.

Thank you again!

@just214
Copy link
Owner

just214 commented Feb 4, 2021

Hello and my pleasure! To be quite honest, I never got around to testing web components and don't anticipate that I will anytime soon.

I did some digging and I definitely see your troubles in finding resources around testing web components with Jest. I'm sorry I can't be of more help, and if you do find a solution, please feel free to post it here for others who might run into the same thing.

@singhtal
Copy link

You don't have to test svelte's custom-elements as a special case. Just write test cases like you write for the normal svelte component. Use import name instead of custom tag name in test file (only exception when the component in question has nested child custom-elements).

In the Below example, Foo is a web-component with custom name - "component-foo" but i am using the "Foo" name only.

import { render } from "@testing-library/svelte";
import Foo from "../src/foo.svelte";

describe("Foo component", () => {
  test("should render component correctly", () => {
    const { container } = render(Foo, {
      props: {
        background: 'red',
        name: 'Singh'
      }
  });

    expect(container).toContainHTML('<body><div><h2 style="background: red;">Hello i am <p>Singh</p></h2></div></body>');
  });
});

@just214
Copy link
Owner

just214 commented May 18, 2021

Thanks for providing this info @tal1992!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants