How can I use "before" instead of "beforeAll"?

Viewed 115

I am migrating existing tests to Jest.

Can I use before instead of beforeAll in Jest?

What I'd do is in jest.config.js,

module.exports = {
  ...
  globals: {
    before: beforeAll,
  },
};

But how can I import beforeAll? require('@jest/globals') raises:

Error: Do not import `@jest/globals` outside of the Jest test environment
1 Answers

beforeAll cannot be accessed in Jest config file because it and tests run in different processes. globals option is for serializable JSON values.

A correct place for this is a file specified in setupFilesAfterEnv config option:

global.before = beforeAll;
Related