Wednesday, 15 March 2023

Use jest inside Jest Test Environment

I have created a Jest test environment in which I mock a specific module:

const NodeEnvironment = require('jest-environment-node');
const { lumigo } = require('../../src/services/lumigo');

class TestEnvironment extends NodeEnvironment {
  async setup() {
    await super.setup();
    jest.mock('../../src/services/lumigo');
    lumigo.trace = jest.fn((func) => func);
  }

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

  getVmContext() {
    return super.getVmContext();
  }
}

module.exports = TestEnvironment;

And I configure my test to use this test environment as follows:

/**
 * @jest-environment ./tests/test-environment.js
 */

... rest of the test

However when I run my test I get the following error:

ReferenceError: jest is not defined

If at the top of my test environment file I try to add:

const jest = require('jest');

I get a warning that Jest is automatically in scope. Do not import "jest", as Jest doesn't export anything and the test still fails.

How can I use jest (for examples invocations to jest.mock) inside my test environment?



from Use jest inside Jest Test Environment

No comments:

Post a Comment