I am attempting to run a simple Jest test to render the App element. This is our App.test.tsx:
test('render application', () => {
render(<App />);
}
This is the error that is being produced:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of 'App'.
If I do the following, the test passes:
function App() {
return (
<AuthProvider>
<ApolloProvider client={apolloClient}>
<h1>Hello World</h1>
</ApolloProvider>
</AuthProvider>
);
}
However, if I do this, it fails on LocalizationProvider:
function App() {
return (
<AuthProvider>
<ApolloProvider client={apolloClient}>
<LocalizationProvider dateAdapter={AdapterLuxon}>
<h1>Hello World</h1>
</LocalizationProvider>
</ApolloProvider>
</AuthProvider>
);
}
This is the LocalizationProvider.d.ts:
export declare const LocalizationProvider: LocalizationProviderComponent;
export {};
Here is our App.tsx:
import React from "react";
import { ApolloProvider } from "@apollo/client";
import CssBaseline from "@mui/material/CssBaseline";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { AdapterLuxon } from "@mui/x-date-pickers/AdapterLuxon";
import { apolloClient } from "./apollo/client";
import AuthProvider from "./providers/AuthProvider";
function App() {
return (
<AuthProvider>
<ApolloProvider client={apolloClient}>
<LocalizationProvider dateAdapter={AdapterLuxon}>
<CssBaseline />
</LocalizationProvider>
</ApolloProvider>
</AuthProvider>
);
}
export default App;
Here is my setupTests.ts file which I attempted to include a lot of mocks:
import '@testing-library/jest-dom';
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: (query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: function () {},
removeListener: function () {},
addEventListener: function () {},
removeEventListener: function () {},
dispatchEvent: function () {
return false;
},
}),
});
jest.mock('@mui/x-date-pickers/LocalizationProvider', () => ({
__esModule: true,
default: (p: { children?: any }) => p.children,
}));
I am unable to change anything in App.tsx as that is on the developer side and I am on the testing side.