Skip to content

Commit

Permalink
feat: set minimum support TypeScript version at 4.3 (#3428)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Minimum support TypeScript version is now 4.3 since Jest 28 requires it
  • Loading branch information
ahnpnl committed Apr 17, 2022
1 parent 41b4ae8 commit 3dba4ec
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 199 deletions.
25 changes: 6 additions & 19 deletions e2e/__tests__/ast-transformers.test.ts
Expand Up @@ -37,29 +37,16 @@ describe('transformer-options', () => {

describe('hoist-jest', () => {
const HOIST_JEST_DIR_NAME = 'hoist-jest'
const NON_TS_FACTORY_DIR_NAME = 'non-ts-factory'
const TS_FACTORY_DIR_NAME = 'ts-factory'
const DIR = path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, HOIST_JEST_DIR_NAME)

describe('non-ts-factory', () => {
const DIR = path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, HOIST_JEST_DIR_NAME, NON_TS_FACTORY_DIR_NAME)

beforeAll(() => {
runNpmInstall(DIR)
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', tsJestBundle], {
cwd: DIR,
})
beforeAll(() => {
runNpmInstall(DIR)
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', tsJestBundle], {
cwd: DIR,
})

executeTest(`${AST_TRANSFORMERS_DIR_NAME}/${HOIST_JEST_DIR_NAME}/${NON_TS_FACTORY_DIR_NAME}`)
})

describe('ts-factory', () => {
beforeAll(() => {
runNpmInstall(path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, HOIST_JEST_DIR_NAME, TS_FACTORY_DIR_NAME))
})

executeTest(`${AST_TRANSFORMERS_DIR_NAME}/${HOIST_JEST_DIR_NAME}/${TS_FACTORY_DIR_NAME}`)
})
executeTest(`${AST_TRANSFORMERS_DIR_NAME}/${HOIST_JEST_DIR_NAME}`)
})

describe('transformer-in-ts', () => {
Expand Down
@@ -1,4 +1,4 @@
/** @type {import('../../../../dist').InitialOptionsTsJest} */
/** @type {import('../../../dist').InitialOptionsTsJest} */
module.exports = {
automock: true,
globals: {
Expand All @@ -9,11 +9,10 @@ module.exports = {
},
},
},
roots: ['<rootDir>', '<rootDir>/../__tests__'],
moduleNameMapper: {
react$: '<rootDir>/node_modules/react',
},
transform: {
'^.+.[tj]sx?$': 'ts-jest',
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
},
}
95 changes: 0 additions & 95 deletions e2e/ast-transformers/hoist-jest/non-ts-factory/package-lock.json

This file was deleted.

24 changes: 0 additions & 24 deletions e2e/ast-transformers/hoist-jest/non-ts-factory/package.json

This file was deleted.

Expand Up @@ -12,12 +12,11 @@
}
}
},
"roots": ["<rootDir>", "<rootDir>/../__tests__"],
"moduleNameMapper": {
"react$": "<rootDir>/node_modules/react"
},
"transform": {
"^.+\\.[tj]sx?$": "<rootDir>/../../../../dist/index.js"
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
}
}
}
19 changes: 0 additions & 19 deletions e2e/ast-transformers/hoist-jest/ts-factory/jest-isolated.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -65,7 +65,7 @@
"@types/jest": "^27.0.0",
"babel-jest": ">=27.0.0 <28",
"jest": "^27.0.0",
"typescript": ">=3.8 <5.0"
"typescript": ">=4.3"
},
"peerDependenciesMeta": {
"@babel/core": {
Expand Down
12 changes: 6 additions & 6 deletions src/config/__snapshots__/config-set.spec.ts.snap
Expand Up @@ -60,7 +60,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -74,7 +74,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand All @@ -93,7 +93,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand All @@ -118,7 +118,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -138,7 +138,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -152,7 +152,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/hoist-jest.spec.ts
Expand Up @@ -168,7 +168,7 @@ const printer = ts.createPrinter()
describe('hoist-jest', () => {
test('should have correct transformer name and version', () => {
expect(name).toBe('hoist-jest')
expect(version).toBe(3)
expect(version).toBe(4)
})

test('should hoist correctly when not using @jest/globals', () => {
Expand Down
38 changes: 14 additions & 24 deletions src/transformers/hoist-jest.ts
Expand Up @@ -8,7 +8,7 @@ import type { TsCompilerInstance } from '../types'
* Remember to increase the version whenever transformer's content is changed. This is to inform Jest to not reuse
* the previous cache which contains old transformer's content
*/
export const version = 3
export const version = 4
// Used for constructing cache key
export const name = 'hoist-jest'

Expand All @@ -21,7 +21,6 @@ const JEST_GLOBAL_NAME = 'jest'
export function factory({ configSet }: TsCompilerInstance) {
const logger = configSet.logger.child({ namespace: name })
const ts = configSet.compilerModule
const tsFactory = ts.factory ? ts.factory : ts
const importNamesOfJestObj: string[] = []

const isJestGlobalImport = (node: _ts.Node): node is _ts.ImportDeclaration => {
Expand Down Expand Up @@ -87,6 +86,7 @@ export function factory({ configSet }: TsCompilerInstance) {
if (statements.length <= 1) {
return statements
}

const pivot = statements[0]
const leftPart: _ts.Statement[] = []
const rightPart: _ts.Statement[] = []
Expand All @@ -109,11 +109,11 @@ export function factory({ configSet }: TsCompilerInstance) {
const resultNode = ts.visitEachChild(node, visitor, ctx)
// Since we use `visitEachChild`, we go upwards tree so all children node elements are checked first
if (ts.isBlock(resultNode) && canHoistInBlockScope(resultNode)) {
const newNodeArrayStatements = tsFactory.createNodeArray(
const newNodeArrayStatements = ts.factory.createNodeArray(
sortStatements(resultNode.statements as unknown as _ts.Statement[]),
)

return tsFactory.updateBlock(resultNode, newNodeArrayStatements)
return ts.factory.updateBlock(resultNode, newNodeArrayStatements)
} else {
if (ts.isSourceFile(resultNode)) {
resultNode.statements.forEach((stmt) => {
Expand Down Expand Up @@ -141,30 +141,20 @@ export function factory({ configSet }: TsCompilerInstance) {
}
}
})
const newNodeArrayStatements = tsFactory.createNodeArray(
const newNodeArrayStatements = ts.factory.createNodeArray(
sortStatements(resultNode.statements as unknown as _ts.Statement[]),
)
importNamesOfJestObj.length = 0

return ts.factory
? ts.factory.updateSourceFile(
resultNode,
newNodeArrayStatements,
resultNode.isDeclarationFile,
resultNode.referencedFiles,
resultNode.typeReferenceDirectives,
resultNode.hasNoDefaultLib,
resultNode.libReferenceDirectives,
)
: ts.updateSourceFileNode(
resultNode,
newNodeArrayStatements,
resultNode.isDeclarationFile,
resultNode.referencedFiles,
resultNode.typeReferenceDirectives,
resultNode.hasNoDefaultLib,
resultNode.libReferenceDirectives,
)
return ts.factory.updateSourceFile(
resultNode,
newNodeArrayStatements,
resultNode.isDeclarationFile,
resultNode.referencedFiles,
resultNode.typeReferenceDirectives,
resultNode.hasNoDefaultLib,
resultNode.libReferenceDirectives,
)
}

return resultNode
Expand Down
7 changes: 3 additions & 4 deletions src/utils/version-checkers.spec.ts
Expand Up @@ -16,22 +16,21 @@ const pv = jest.mocked(_pv)
describeChecker(VersionCheckers.jest, 'jest', ['27.0.0'], [undefined, '23.6.0', '24.1.0', '28.0.0'])
describeChecker(VersionCheckers.babelJest, 'babel-jest', ['27.0.0'], [undefined, '23.6.0', '24.1.0', '28.0.0'])
describeChecker(VersionCheckers.babelCore, '@babel/core', ['7.0.0'], [undefined, '6.0.0', '8.0.0'])
describeChecker(VersionCheckers.typescript, 'typescript', ['3.8.0', '3.8.3'], [undefined, '3.3.0', '5.0.0'])
describeChecker(VersionCheckers.typescript, 'typescript', ['4.3.0', '4.3.5'], [undefined, '4.2.0', '5.0.0'])

function describeChecker(
checker: VersionChecker,
moduleName: string,
supportedVersions: string[],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
unsupportedVersions: any[],
unsupportedVersions: Array<string | undefined>,
) {
// eslint-disable-next-line jest/valid-title
describe(moduleName, () => {
beforeEach(() => {
checker.forget()
})

unsupportedVersions.forEach((testVersion: string) => {
unsupportedVersions.forEach((testVersion) => {
describe(`unsupported version (${testVersion})`, () => {
beforeEach(() => {
pv.getPackageVersion.mockImplementation((name) => (name === moduleName ? testVersion : undefined))
Expand Down
2 changes: 1 addition & 1 deletion src/utils/version-checkers.ts
Expand Up @@ -13,7 +13,7 @@ const logger = rootLogger.child({ namespace: 'versions' })
*/
const enum ExpectedVersions {
Jest = '>=27.0.0 <28',
TypeScript = '>=3.8 <5',
TypeScript = '>=4.3 <5',
BabelJest = '>=27.0.0 <28',
BabelCore = '>=7.0.0-beta.0 <8',
}
Expand Down

0 comments on commit 3dba4ec

Please sign in to comment.