0

How to write own Jest preset with common beforeAll and afterAll?

I'm quite confused, seems that there is no related documentation.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tomas Randus
  • 2,127
  • 4
  • 29
  • 38
  • Does this answer your question? [Jest beforeAll() share between multiple test files](https://stackoverflow.com/questions/47997652/jest-beforeall-share-between-multiple-test-files) – blex Dec 11 '20 at 11:56

1 Answers1

0

There is the environment that you can extend this way:

JEST config:

module.exports = {
    testEnvironment: './suites/future/jest.myEnv.js',
};

The jest.myEnv.js:

const NodeEnvironment = require('jest-environment-node');

const start = async () => {
    console.log('START');
};

const stop = async () => {
    console.log('STOP');
};

class TestEnvironment extends NodeEnvironment {
    constructor(config) {
        super(config);
    }

    async setup() {
        await super.setup();
        await start();
    }

    async teardown() {
        await stop();
        await super.teardown();
    }

    runScript(script) {
        return super.runScript(script);
    }
}

module.exports = TestEnvironment;

Tomas Randus
  • 2,127
  • 4
  • 29
  • 38