When should I use setupFiles rather than setupFilesAfterEnv?

We can see what different between setupFiles and setupFilesAfterEnv from the documentation.

The most important difference will probably be when it is run.

setupFiles will be executed

before the test framework is installed in the environment.

setupFilesAfterEnv will be executed

after the test framework has been installed in the environment.

That’s why the name has AfterEnv.

I actually use both of them in my actual project.

In my case, I use the setupFiles to set up fro .env values and use the setupFilesAfterEnv to set up jest configuration like jest.setTimeout(70000)

>> In my case >>>>>>>>>>>>>>>>>>>>>>>


  setupFiles: ['<rootDir>/tests/settings/env-setup.ts'],
  setupFilesAfterEnv: ['<rootDir>/testSetupFile.js'],


import dotenv from 'dotenv';
import path from 'path';

console.log(`============ env-setup Loaded ===========`);
dotenv.config({ path: path.resolve(process.cwd(), 'tests', 'settings', '.test.env') });


// Some of the `jest` tests are very slow and cause
// timeouts on bitbucket pipeline
console.log(`============ testSetupFile Loaded ===========`);

Leave a Comment