My app uses the window object to inject environmental variables at runtime for docker, however my tests are coming back with the error below:
url config:
declare const window: Window &
typeof globalThis & {
_env_: any
}
export const url = window._env_.REACT_APP_API_URL;
export const baseUrl = url;
error in tests as the handlers use the baseUrl, but even if I change the handlers to hardcoded values the below error comes:
● Test suite failed to run TypeError: Cannot read properties of undefined (reading 'REACT_APP_API_URL')
4 | }
5 |
> 6 | export const url = window._env_.REACT_APP_API_URL;
| ^
7 | export const baseUrl = url;
setupTests.tsx
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
import { setLogger } from 'react-query'
import { server } from './mocks/server'
declare const window: Window &
typeof globalThis & {
_env_: any
}
window._env_.REACT_APP_API_URL = "https://wwww.xxxxx.com"
beforeAll(() => server.listen())
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers())
// Clean up after the tests are finished.
afterAll(() => server.close())
// silence react-query errors
setLogger({
log: console.log,
warn: console.warn,
error: () => {},
})
Any idea's? So where ever there is an api call I get this error in the tests for jest/React.