I am trying to mock something that imports @env so that I can test different combinations of the values. However, I don't want to change the existing method which reads from the @env to pass in variables.
// env.d.ts
declare module '@env' {
export const APP_ROUTE_NAME: string;
}
Here's my attempt
import env from '@env';
jest.mock('@env');
describe("Determine initial routes", ()=> {
it("defaults",()=>{
// does not work because it is read only
(env as jest.Mocked<typeof env>).APP_ROUTE_NAME = null
})
})
If you are testing you probably don't need to care about fake secrets being included in your code. So just set the environment variables manually. You could put this is the the a global setup file:
const isDev = process.env.NODE_ENV !== 'production'
module.exports = async () => {
if (isDev) {
process.env.API_KEY = 'xxx'
process.env.OTHER_STUFF = 'xxx'
}
}