I have an error in my tests that flag up a window env variable as undefined. I understand the error as it is based on the runtime of the app using the variables and maybe they are undefined on running the app. But I don't know where in the setupTests.tsx, I would need to define it. So far the variable is used as so:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="%PUBLIC_URL%/config.js"></script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
config.js
window._env_ = {
REACT_APP_URL: "https://apiurl.com"
}
how it is used in the app:
declare const window: Window &
typeof globalThis & {
_env_: any
}
const url = window._env_.REACT_APP_URL;
export const apiUrl = url;
setupTests.tsx I did try adding it here but it's still not working
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_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())
The error that stops the tests:
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading 'REACT_APP_URL')
4 | }
5 |
> 6 | const url = window._env_.REACT_APP_URL;
| ^
7 | export const apiUrl = url;
8 |
9 |
at Object.<anonymous> (src/utils/Url.tsx:6:26)
at Object.<anonymous> (src/mocks/handlers.tsx:3:1)
at Object.<anonymous> (src/mocks/server.tsx:2:1)
at Object.<anonymous> (src/setupTests.tsx:7:1)