Skip to content

Commit

Permalink
feat: load TypeScript config from tsconfig package instead of `ts-j…
Browse files Browse the repository at this point in the history
…est` (#471)

* chore(deps): add `tsconfig` package on @vue/vue2-jest

* feat: add `getTypeScriptConfig` util function

* feat: use `getTypeScriptConfig` on typescript transformer

* feat: remove `getTsJestConfig` function

* chore(deps): remove ts-jest dependencies

* chore(deps-dev): remove ts-jest from e2e projects

* feat: remove info log

* feat: support `createTransformer()` on customTransformer
  • Loading branch information
nogic1008 authored Aug 15, 2022
1 parent 4530039 commit a84eafb
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 51 deletions.
1 change: 0 additions & 1 deletion e2e/2.x/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"coffeescript": "^2.3.2",
"jest": "28.x",
"jest-environment-jsdom": "28.0.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4"
},
"jest": {
Expand Down
5 changes: 2 additions & 3 deletions e2e/2.x/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/test-utils": "^1.1.0",
"@vue/vue2-jest": "^28.0.0",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-vue-jsx": "^3.7.0",
Expand All @@ -23,9 +24,7 @@
"jest-environment-jsdom": "28.0.2",
"pug": "^3.0.1",
"sass": "^1.23.7",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"@vue/vue2-jest": "^28.0.0"
"typescript": "^4.6.4"
},
"jest": {
"testEnvironment": "jsdom",
Expand Down
4 changes: 2 additions & 2 deletions e2e/3.x/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/vue3-jest": "^28.0.0",
"coffeescript": "^2.3.2",
"jest": "^28.0.0",
"jest-environment-jsdom": "28.0.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"@vue/vue3-jest": "^28.0.0"
"typescript": "^4.6.4"
},
"jest": {
"testEnvironment": "jsdom",
Expand Down
2 changes: 1 addition & 1 deletion e2e/3.x/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/vue3-jest": "^28.0.0",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-vue-jsx": "^3.7.0",
Expand All @@ -23,7 +24,6 @@
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"vue-class-component": "^8.0.0-beta.4",
"@vue/vue3-jest": "^28.0.0",
"vue-property-decorator": "^10.0.0-rc.3"
}
}
5 changes: 2 additions & 3 deletions e2e/3.x/typescript-with-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/vue3-jest": "^28.0.0",
"jest": "^28.0.2",
"jest-environment-jsdom": "28.0.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"@vue/vue3-jest": "^28.0.0"
"typescript": "^4.6.4"
},
"jest": {
"testEnvironment": "jsdom",
Expand Down
4 changes: 2 additions & 2 deletions e2e/3.x/typescript-with-compiler-options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"devDependencies": {
"@types/jest": "16.0.10",
"@vue/vue3-jest": "^28.0.0",
"jest": "^28.0.2",
"jest-environment-jsdom": "28.0.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"@vue/vue3-jest": "^28.0.0"
"typescript": "^4.6.4"
},
"jest": {
"testEnvironment": "jsdom",
Expand Down
4 changes: 2 additions & 2 deletions e2e/3.x/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"devDependencies": {
"@types/jest": "16.0.10",
"@vue/vue3-jest": "^28.0.0",
"jest": "^28.0.2",
"jest-environment-jsdom": "28.0.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"@vue/vue3-jest": "^28.0.0"
"typescript": "^4.6.4"
},
"jest": {
"testEnvironment": "jsdom",
Expand Down
4 changes: 2 additions & 2 deletions packages/vue2-jest/lib/transformers/typescript.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ensureRequire = require('../ensure-require')
const babelJest = require('babel-jest').default
const {
getTsJestConfig,
stripInlineSourceMap,
getCustomTransformer,
getTypeScriptConfig,
getVueJestConfig
} = require('../utils')

Expand All @@ -12,7 +12,7 @@ module.exports = scriptLang => ({
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)

const res = typescript.transpileModule(scriptContent, {
...tsconfig,
Expand Down
31 changes: 22 additions & 9 deletions packages/vue2-jest/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const constants = require('./constants')
const loadPartialConfig = require('@babel/core').loadPartialConfig
const { loadSync: loadTsConfigSync } = require('tsconfig')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')
Expand Down Expand Up @@ -67,17 +68,28 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
return loadPartialConfig(opts).options
}

const getTsJestConfig = function getTsJestConfig(config) {
const { ConfigSet } = require('ts-jest/dist/legacy/config/config-set')
const configSet = new ConfigSet(config.config)
var tsConfig = configSet.typescript || configSet.parsedTsConfig
/**
* Load TypeScript config from tsconfig.json.
* @param {string | undefined} path tsconfig.json file path (default: root)
* @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
*/
const getTypeScriptConfig = function getTypeScriptConfig(path) {
const tsconfig = loadTsConfigSync(process.cwd(), path || '')
if (!tsconfig.path) {
warn(`Not found tsconfig.json.`)
return null
}
const compilerOptions =
(tsconfig.config && tsconfig.config.compilerOptions) || {}

return {
compilerOptions: { ...tsConfig.options, module: 'commonjs' }
compilerOptions: { ...compilerOptions, module: 'commonjs' }
}
}

function isValidTransformer(transformer) {
return (
isFunction(transformer.createTransformer) ||
isFunction(transformer.process) ||
isFunction(transformer.postprocess) ||
isFunction(transformer.preprocess)
Expand Down Expand Up @@ -110,12 +122,13 @@ const getCustomTransformer = function getCustomTransformer(

if (!isValidTransformer(transformer)) {
throwError(
`transformer must contain at least one process, preprocess, or ` +
`postprocess method`
`transformer must contain at least one createTransformer(), process(), preprocess(), or postprocess() method`
)
}

return transformer
return isFunction(transformer.createTransformer)
? transformer.createTransformer()
: transformer
}

const throwError = function error(msg) {
Expand Down Expand Up @@ -152,7 +165,7 @@ module.exports = {
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getTypeScriptConfig,
getBabelOptions,
getVueJestConfig,
transformContent,
Expand Down
8 changes: 4 additions & 4 deletions packages/vue2-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"conventional-changelog": "^1.1.5",
"jest": "^28.0.2",
"semantic-release": "^15.13.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"vue": "^2.4.2",
"vue-template-compiler": "^2.4.2"
Expand All @@ -41,12 +40,12 @@
"@babel/core": "7.x",
"babel-jest": ">= 28 < 29",
"jest": "28.x",
"ts-jest": ">= 28 < 29",
"typescript": ">= 4.3",
"vue": "^2.x",
"vue-template-compiler": "^2.x"
},
"peerDependenciesMeta": {
"ts-jest": {
"typescript": {
"optional": true
}
},
Expand All @@ -55,7 +54,8 @@
"@vue/component-compiler-utils": "^3.1.0",
"chalk": "^2.1.0",
"css-tree": "^2.0.1",
"source-map": "0.5.6"
"source-map": "0.5.6",
"tsconfig": "^7.0.0"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/vue3-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const typescriptTransformer = require('./transformers/typescript')
const coffeescriptTransformer = require('./transformers/coffee')
const _processStyle = require('./process-style')
const processCustomBlocks = require('./process-custom-blocks')
const getTypeScriptConfig = require('./utils').getTypeScriptConfig
const getVueJestConfig = require('./utils').getVueJestConfig
const getTsJestConfig = require('./utils').getTsJestConfig
const logResultErrors = require('./utils').logResultErrors
const stripInlineSourceMap = require('./utils').stripInlineSourceMap
const getCustomTransformer = require('./utils').getCustomTransformer
Expand Down Expand Up @@ -118,7 +118,7 @@ function processTemplate(descriptor, filename, config) {

logResultErrors(result)

const tsconfig = getTsJestConfig(config)
const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)

if (tsconfig) {
// they are using TypeScript.
Expand Down
4 changes: 2 additions & 2 deletions packages/vue3-jest/lib/transformers/typescript.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ensureRequire = require('../ensure-require')
const babelJest = require('babel-jest').default
const {
getTsJestConfig,
stripInlineSourceMap,
getCustomTransformer,
getTypeScriptConfig,
getVueJestConfig
} = require('../utils')

Expand All @@ -12,7 +12,7 @@ module.exports = {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const vueJestConfig = getVueJestConfig(config)
const tsconfig = getTsJestConfig(config)
const tsconfig = getTypeScriptConfig(vueJestConfig.tsConfig)

const res = typescript.transpileModule(scriptContent, {
...tsconfig,
Expand Down
32 changes: 19 additions & 13 deletions packages/vue3-jest/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const constants = require('./constants')
const loadPartialConfig = require('@babel/core').loadPartialConfig
const { resolveSync: resolveTsConfigSync } = require('tsconfig')
const { loadSync: loadTsConfigSync } = require('tsconfig')
const chalk = require('chalk')
const path = require('path')
const fs = require('fs')
Expand Down Expand Up @@ -68,24 +68,29 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
return loadPartialConfig(opts).options
}

const getTsJestConfig = function getTsJestConfig(config) {
const tsConfigPath = getVueJestConfig(config).tsConfig || ''
const isUsingTs = resolveTsConfigSync(process.cwd(), tsConfigPath)
if (!isUsingTs) {
/**
* Load TypeScript config from tsconfig.json.
* @param {string | undefined} path tsconfig.json file path (default: root)
* @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null
*/
const getTypeScriptConfig = function getTypeScriptConfig(path) {
const tsconfig = loadTsConfigSync(process.cwd(), path || '')
if (!tsconfig.path) {
warn(`Not found tsconfig.json.`)
return null
}
const compilerOptions =
(tsconfig.config && tsconfig.config.compilerOptions) || {}

const { ConfigSet } = require('ts-jest/dist/legacy/config/config-set')
const configSet = new ConfigSet(config.config)
const tsConfig = configSet.typescript || configSet.parsedTsConfig
// Force es5 to prevent const vue_1 = require('vue') from conflicting
return {
compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' }
compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' }
}
}

function isValidTransformer(transformer) {
return (
isFunction(transformer.createTransformer) ||
isFunction(transformer.process) ||
isFunction(transformer.postprocess) ||
isFunction(transformer.preprocess)
Expand Down Expand Up @@ -119,12 +124,13 @@ const getCustomTransformer = function getCustomTransformer(

if (!isValidTransformer(transformer)) {
throwError(
`transformer must contain at least one process, preprocess, or ` +
`postprocess method`
`transformer must contain at least one createTransformer(), process(), preprocess(), or postprocess() method`
)
}

return transformer
return isFunction(transformer.createTransformer)
? transformer.createTransformer()
: transformer
}

const throwError = function error(msg) {
Expand Down Expand Up @@ -161,7 +167,7 @@ module.exports = {
throwError,
logResultErrors,
getCustomTransformer,
getTsJestConfig,
getTypeScriptConfig,
getBabelOptions,
getVueJestConfig,
transformContent,
Expand Down
5 changes: 0 additions & 5 deletions packages/vue3-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,17 @@
"jest": "^28.0.2",
"jest-cli": "^28.0.2",
"semantic-release": "^15.13.2",
"ts-jest": "^28.0.1",
"typescript": "^4.6.4",
"vue": "^3.2.22"
},
"peerDependencies": {
"@babel/core": "7.x",
"babel-jest": "28.x",
"jest": "28.x",
"ts-jest": "28.x",
"typescript": ">= 4.3",
"vue": "^3.0.0-0"
},
"peerDependenciesMeta": {
"ts-jest": {
"optional": true
},
"typescript": {
"optional": true
}
Expand Down

0 comments on commit a84eafb

Please sign in to comment.