Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: allow to customize JEST_GLOBALS_MODULE_NAME #4288

Open
zanminkian opened this issue Apr 9, 2024 · 2 comments
Open

[Feature]: allow to customize JEST_GLOBALS_MODULE_NAME #4288

zanminkian opened this issue Apr 9, 2024 · 2 comments
Labels

Comments

@zanminkian
Copy link

馃殌 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

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 19, 2024

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

@zanminkian
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants