ts-jest
ts-jest copied to clipboard
[Feature]: allow to customize JEST_GLOBALS_MODULE_NAME
🚀 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