Has anyone had any luck configuring React Testing Library for testing StencilJS React wrapped components?
I'm getting this error when running the tests:
Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'Component')
31 |
32 | const displayName = dashToPascalCase(tagName);
> 33 | const ReactComponent = class extends React.Component<StencilReactInternalProps<ElementType>> {
| ^
34 | componentEl!: ElementType;
35 |
36 | setComponentElRef = (element: ElementType) => {
I guess it's got something to do with calling defineCustomElements correctly:
import { defineCustomElements } from '@salable/stencil-library/loader';
defineCustomElements(window);
I have tried adding it to jest setup scripts, beforeAll and in the top of the test file.
This is my current test file.
import React from 'react';
import { render, screen } from '@testing-library/react';
import {MyComponent} from "../lib";
import '@testing-library/jest-dom';
import { waitFor } from '@testing-library/react';
import { defineCustomElements } from '@salable/stencil-library/loader';
defineCustomElements(window);
test('component loads and renders', async () => {
render(<MyComponent />);
await waitFor(() => {
expect(screen.getByText(/hello/i)).toBeInTheDocument();
});
});
jest.config.js
module.exports = {
transform: {
'^.+\\.(ts|tsx)$': ['ts-jest', {
tsconfig: 'tsconfig.test.json',
}],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
setupFilesAfterEnv: ['@testing-library/jest-dom', '<rootDir>/__tests__/jest.setup.ts'],
testEnvironment: 'jsdom',
clearMocks: true,
};