I have tried by best to figure out the issue but everything I tried so far has not been working.
This is what my App.js looks like:
import './App.css';
import {ChakraProvider} from '@chakra-ui/react'
import Header from './components/Header'
import Hero from './components/Hero'
import Highlights from './components/Highlights.js'
import About from './components/About.js'
import Footer from './components/Footer.js'
import Reserve from './components/Reserve.js'
import '@fontsource/karla'
import '@fontsource/markazi-text'
import theme from './theme'
import { Routes, Route,} from "react-router-dom";
function Homepage(){
return(
<>
<Hero/>
<Highlights/>
<About/>
<Footer/>
</>
)
}
function App() {
return (
<ChakraProvider theme={theme}>
<Header/>
<main>
<Routes>
<Route path='/' element={<Homepage/>}/>
<Route path='/Reservations' element={<Reserve/>}/>
</Routes>
</main>
</ChakraProvider>
);
}
export default App;
This is my App.test.js:
import React from 'react'
import { render, screen } from '@testing-library/react';
import Reserve from './components/Reserve';
import { MemoryRouter } from 'react-router-dom';
test('render reservations page header', () => {
render(
<MemoryRouter>
<Reserve />
</MemoryRouter>
)
expect(screen.getByText('Reservations')).toBeInTheDocument()
})
This is a picture of the error messsage:
error
I tried using the history library, but that did not work. As well as the wrapper option and tried using BrowserRouter and nothing worked.
2
Answers
The source of the problem is actually in the Reserve.js file, it is using useMediaQuery from chakra-ui
This is a representation of the Reserve.js file:
The optimum solution was to mock this hook and pass it a value in the test file, however I tried several mocking methods and each produced a different type of error.
I was able to bypass the problem by giving the hook a constant value in the Reserve.js file during tests
If any onlooker knows how to correctly mock this hook your help will be much appreciated!
You should be wrapping your
Routes
withBrowserRouter
in your App. Your test should also have this import:import "@testing-library/jest-dom";
Using your code modified to test in this example sandbox I’m not having any issues.
App.js
Reserve.js
App.test.js
Also make sure you’re not using an outdated version of testing library with a new version of react.