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 >>>>>>>>>>>>>>>>>>>>>>>

jest.config.js

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

env-setup.ts

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

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

testSetupFile.ts

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

Leave a Comment