Change Jest Version

Run the following on a component that uses your env, to get the Jest version currently being used:

$bit
Copiedcopy

The out is similar to the following:

teambit.defender/formatter

configured tester: prettier-formatter (Prettier @ 2.7.1)

To provide your Jest tester with a different version of Jest, implement the tester env handler and replace your Jest build task:

/* @filename: my-env.bit-env.ts */

/* your base env */
import { ReactEnv } from '@teambit/react.react-env';
import type { ReactEnvInterface } from '@teambit/react.react-env';
import type { EnvHandler } from '@teambit/envs';

import { JestTask, JestTester } from '@teambit/defender.jest-tester';
import { Tester } from '@teambit/tester';

export class MyEnv extends ReactEnv implements ReactEnvInterface {
  constructor() {
    super();
  }

  name = 'my-env';

  /**
   * replace the env's default tester with this instance of jest-tester
   */
  tester(): EnvHandler<Tester> {
    return JestTester.from({
      /* the path to the jest instance used by this env */
      jest: require.resolve('jest'),
    });
  }

  build() {
    return super.build().from([
      /**
       * replace the env's default jest task with this task
       * (add a jest task if one is not already registered to this pipeline)
       *  **/
      JestTask.from({
        /* the path to the jest instance used by this env */
        jest: require.resolve('jest'),
      }),
    ]);
  }
}

export default new MyEnv();
CopiedCopy

Jest-tester requires a path to the jest instance, not an instance of jest. Because of that, Jest is not imported by the env, which leads to Bit failing to detect Jest as a dependency of that env.

To address that, manually add Jest as a dependency of that env:

$bit
Copiedcopy
See command synopsis

Install the new version in your workspace:

$bit
Copiedcopy
See command synopsis

Run the following to compile your env:

$bit
Copiedcopy
See command synopsis