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 · 0 comments
Open

[Feature]: allow to customize JEST_GLOBALS_MODULE_NAME #4288

zanminkian opened this issue Apr 9, 2024 · 0 comments

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

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

No branches or pull requests

1 participant