I want to create lwc component which selectively renders svg from html template files. I wonder how to mock that templates for the needs of JEST test. Import the same templates for component and test is a proper way?





    import { default as desert } from "./desert.html";
    export { default as empty } from "./empty.html";

    export default {
       "no-data:desert": desert


    import { LightningElement, api } from "lwc";
    import { default as images, empty } from "c/images";

    export default class IllustrationImage extends LightningElement {

        @api imageName;

        render() {
            if (images[this.imageName]) {
                return images[this.imageName];
            return empty;


    import { createElement } from "lwc";
    import IllustrationImage from "c/illustrationImage";

    describe("c-illustration-image", () => {
        afterEach(() => {
            while (document.body.firstChild) {

        it("Renders image by name", () => {
            const element = createElement("c-illustration-image", {
                is: IllustrationImage
            element.imageName = "no-data:desert";


            const svg = element.shadowRoot.querySelector("svg");

            return Promise.resolve().then(() => {
                // check that svg is rendered correctly by component
  • Can you provide some sample code that illustrates what you mean by "selectively renders svg from html template files"? And maybe say a little bit about what you want the tests to be checking for? Mar 30, 2021 at 20:51
  • Yep. Sample code added. Mar 31, 2021 at 11:21
  • 1
    This seems like a good start. Is the test working so far? If you want to take it further (maybe you want to make sure it's the desert SVG and not another SVG?), you could start querying its child nodes and using expect statements about those nodes' properties. If you really want to make sure the rendered SVG is 100% identical to the "desert" SVG, I guess you could have the test import the "desert" SVG directly, and recursively check all the nodes. To me, that would be overkill, but it sort of depends on your testing philosophy. Mar 31, 2021 at 15:53


You must log in to answer this question.

Browse other questions tagged .