Having trouble getting web components registered when testing Lit Element (lit-element) with @web/test-runner and @open-wc/testing-helpers?

Currently, I am working with Lit Element and Typescript for my project.

Here are the dependencies for my tests:

"@esm-bundle/chai": "^4.3.4-fix.0",
"@open-wc/chai-dom-equals": "^0.12.36",
"@open-wc/testing-helpers": "^2.1.2",
"@web/dev-server-esbuild": "^0.3.0",
"@web/test-runner": "^0.13.30",
"@web/test-runner-puppeteer": "^0.10.5",
"chai-a11y-axe": "^1.4.0",
"mocha": "^10.0.0"

This is how my web-test-runner.config.mjs configuration file looks like:

import { esbuildPlugin } from '@web/dev-server-esbuild';
import { fileURLToPath } from 'url';

export default {
    files: ['src/**/*.test.ts'],
    nodeResolve: true,
    plugins: [
        esbuildPlugin({
            ts: true,
            tsconfig: fileURLToPath(new URL('./tsconfig.json', import.meta.url))
        })
    ]
};

My test code snippet:

import { CustomComponent } from './custom.component';
import { CustomData } from '../../../models/data.model';
import { fixture, fixtureCleanup, html } from '@open-wc/testing-helpers';
import { expect } from '@esm-bundle/chai';

describe('CustomComponent', () => {
    let component: CustomComponent;

    beforeEach(async () => {
        component = await fixture<CustomComponent>(html`<my-custom-component></my-custom-component>`);
    });

    afterEach(() => {
        fixtureCleanup();
    });

    it('should set the default data', () => {
        expect(component.customData).to.equal({} as CustomData);
    });
});

When executing the test using the wtr command, the tests run successfully. However, I noticed that the component is not being instantiated. This observation is supported by the fact that none of the console logs within the constructor and lifecycle hooks of the lit element are triggered. The test also fails with an error message

AssertionError: expected undefined to equal {}
, despite properly initializing the property with a default value.

I suspect that the fixture function is merely placing the tags in the DOM without registering them as web components. It's unclear whether the test has access to the JavaScript code responsible for registering my Lit Element.

If you have any insights or guidance on this matter, please feel free to check out this related post on Stack Overflow.

Answer №1

Check out this answer to the same question you referenced.

Include a full import of the file or find a way to leverage your component instance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Removing the Shadow from Material UI's Dialog Box

I am currently struggling with the Material UI dialog component that displays information about a location marker. My issue is with disabling the unattractive box shadow that surrounds the component. Despite setting box-shadow: none and trying different so ...

An essential component of Chai assertion is the inclusion of "or"

While attempting to iterate through a list of elements, I am looking to verify that the body contains either of two values. However, the current assertion method only allows me to check for one value: expect(cellText).includes(toDateFormat); Is there a wa ...

JavaScript API for Tableau

Could you please clarify the functions described below? newViz = createTableauViz(containerDiv, url, options); function listenForMarkSelection() { newViz.addEventListener(tableau.TableauEventName.MARKS_SELECTION, handleMarksSelection); } funct ...

Interacting with objects in a loaded OBJ model using ThreeJS

I have an obj model representing a map with tree objects. After successfully loading the model, I am trying to access one specific tree object named Tree1. How can I achieve this? Currently, my code looks like this: loader.load( 'map.obj', fun ...

Navigating React: Learn the ins and outs of accessing and modifying state/attributes from a different component

Looking to update the attribute values of ComponentB from ComponentA (currently using an index). I attempted to call lower component functions to modify their state/values. import React from 'react' class TaskComp extends React.Component{ ...

Getting the http response content of a Slim PHP API with Angular JS: A step-by-step guide

My Slim PHP programmed API sends JSON responses in the following format: $response['some-text'] = 'blabla'; $app->response->setStatus(200); $app->response()->headers->set('Content-Type', 'application/json& ...

javascript download multiple PDF files on Internet Explorer

I am facing an issue with downloading multiple PDF files In my list, I have various a elements with links to different PDF files. I created a loop to go through each a element and generate an iframe using the value of the href as the source. This solutio ...

Mastering Angular Apollo Error Resolution Methods

Hey everyone, I'm facing a problem with apollo-angular and apollo-link-error that I just can't seem to figure out. I've experimented with different approaches but have had no luck catching errors on the client-side of my angular web app. Bel ...

Encountered an issue with mapping data from a controller to a view in Angular.js

Currently, my application consists of only three small parts: a service that makes an http call to a .json file, a controller that receives data from the service and sends it to a view. Everything was working fine when I hard coded the data in my service. ...

when passing a JavaScript symbol in Django's JSON objects, it does not display properly

When working with Django (2.2), my goal is to send a JSON response in this format: return JsonResponse(data, safe=False) Within the data object, there is a value that looks like this: "unit": "\u33A5" (cubic meters). However, ...

Is there a way to have two SVG files displayed on a single HTML page at the same time

Currently, I am facing an issue with my graphs. I have a line graph and a boxplot, but the boxplot is appearing below the line graph when I want it to be next to it. Any suggestions on how I can achieve this layout? Thank you! I attempted to use 2 differe ...

Submitting a form and using Ajax to upload an image

Is there a way to submit an image file to php using ajax without assigning the file to a variable with onchange event? I've tried triggering the request on submit click, but keep getting the error message: "cannot read property 0 of undefined." <ht ...

Angular: Implementing conditional HTTP requests within a loop

Currently, I am facing a challenge where I need to loop through an array of objects, check a specific property of each object, and if it meets certain criteria, make an HTTP request to fetch additional data for that object. The code snippet below represen ...

I have an empty array that needs to be filled with numbers

Currently, I am facing a challenge where I have an empty array and need to insert numbers from 1 to 20 into it. Once that is complete, the next step is to calculate the total sum of all these numbers. The stumbling block I am encountering lies in this sect ...

Mapping URLs to objects in JavaScript, TypeScript, and Angular4

I have implemented a class called SearchFilter class SearchFilter { constructor(bucket: string, pin: number, qty: number, category: string) { } } When the user performs a search, I populate the filter i ...

Hiding the header on a specific route in Angular 6

Attempting to hide the header for only one specific route Imagine having three different routes: route1, route2, and route3. In this scenario, there is a component named app-header. The goal is to make sure that the app-header component is hidden when t ...

Leverage VueJS and VueX to seamlessly integrate firebase 9 functions within my application

I am currently in the process of developing a blog application using Firebase version 9, VueJs version 3, and VueX. All functionalities such as user registration and authentication are working smoothly. However, I encountered an error when attempting to a ...

What is the best way to fill in references that are nested within an array object using mongoose?

How do I go about populating the product fields within the cart array provided below? { "_id": "5bbd1c6e2c48f512fcd733b4", "cart": [ { "count": 1, "_id": "5bbd30ed9417363f345b15fc", "product": "5ba93a6d ...

Having trouble retrieving the input value using JavaScript

I have been attempting to retrieve the value of an input tag using JavaScript and display it on the console, but my code seems to be not functioning properly. Can someone help me identify the issue in the following code snippet? const userTextValue = doc ...

Having trouble with the functionality of JQuery drop feature?

As I delve into implementing drag and drop functionality with JQuery, I encounter a peculiar issue. I have set up 3 'draggable' divs and corresponding 3 'droppable' divs. The intended behavior is for each draggable element to be accepte ...