kulshekhar/ts-jest

[Feature]: allow to customize JEST_GLOBALS_MODULE_NAME

zanminkian opened this issue ยท 2 comments

๐Ÿš€ Feature Proposal

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  // [...]
  transform: {
    // '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
    // '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        jestGlobalsModuleName: 'my-jest-wrapper' // defaults to '@jest/globals'
      },
    ],
  },
}

Please consider supporting the option below to customize JEST_GLOBALS_MODULE_NAME.

Motivation

ts-jest will hoist statements like jest.mock.

import {jest} from '@jest/globals';
import {app} from './app';

// ts-jest hoist `jest.mock` to the top of the file
jest.mock('./app', () => ({
  app: 'foo'
}));

// so that `app` will be 'foo'
console.log(app);

I have a wrapper which bundle the best pratices into one package. Just like the development experience of vitest:

import {jest, it, expect} from 'my-test-framework';
import {app} from './app';

// ts-jest will not hoist `jest.mock`
jest.mock('./app', () => ({
  app: 'foo'
}));

it('test', () => {
  expect(app).toBe('foo') // fail! because ts-jest won't hoist jest.mock
})
npx my-test-framework

Supporting to customize JEST_GLOBALS_MODULE_NAME will help me.

Here is the example usage of vitest:

import { expect, test, vi } from 'vitest'
import { sum } from './sum'

vi.mock('./sum', () => ({
  default: {
    sum: (x, y) => x + y
  }
}));

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3)
})

Example

No response

Does babel-jest support this too? If yes we can do. Our hoisting works similarly like babel-jest

I've read the babel plugin babel-plugin-jest-hoist source code. They do not support to customize JEST_GLOBALS_MODULE_NAME.

const JEST_GLOBALS_MODULE_NAME = '@jest/globals';

I will submit a feature request issue later to jest repository for supporting.