I have a mono repo of front & backend code so I have my tests split up now, /api
and /app
for example.
I added simple Jest unit test into the /api
directory. When I run the test in the terminal, if I am also in the /api
directory, the tests run fine. However, if I cd
up a level to the root, the test fails because the process.env.MY_ENV
is not found in the now lower down directory.
In my /api
directory, I have added:
jest.config which has the setupFiles
to load EnvVars:
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ['<rootDir>/jestEnvVars.ts', '<rootDir>/jestSetup.ts'],
};
and jestEnvVars.ts:
require('dotenv').config(); // I have tried adding { path: path.resolve(__dirname, './.env') } inside the config().
Now, on my precommit hook or VSCode, the tests fail because the env is not found.
What can I do?