|
1 | 1 | const constants = require('./constants')
|
2 | 2 | const loadPartialConfig = require('@babel/core').loadPartialConfig
|
3 |
| -const { resolveSync: resolveTsConfigSync } = require('tsconfig') |
| 3 | +const { loadSync: loadTsConfigSync } = require('tsconfig') |
4 | 4 | const chalk = require('chalk')
|
5 | 5 | const path = require('path')
|
6 | 6 | const fs = require('fs')
|
@@ -68,24 +68,29 @@ const getBabelOptions = function loadBabelOptions(filename, options = {}) {
|
68 | 68 | return loadPartialConfig(opts).options
|
69 | 69 | }
|
70 | 70 |
|
71 |
| -const getTsJestConfig = function getTsJestConfig(config) { |
72 |
| - const tsConfigPath = getVueJestConfig(config).tsConfig || '' |
73 |
| - const isUsingTs = resolveTsConfigSync(process.cwd(), tsConfigPath) |
74 |
| - if (!isUsingTs) { |
| 71 | +/** |
| 72 | + * Load TypeScript config from tsconfig.json. |
| 73 | + * @param {string | undefined} path tsconfig.json file path (default: root) |
| 74 | + * @returns {import('typescript').TranspileOptions | null} TypeScript compilerOptions or null |
| 75 | + */ |
| 76 | +const getTypeScriptConfig = function getTypeScriptConfig(path) { |
| 77 | + const tsconfig = loadTsConfigSync(process.cwd(), path || '') |
| 78 | + if (!tsconfig.path) { |
| 79 | + warn(`Not found tsconfig.json.`) |
75 | 80 | return null
|
76 | 81 | }
|
| 82 | + const compilerOptions = |
| 83 | + (tsconfig.config && tsconfig.config.compilerOptions) || {} |
77 | 84 |
|
78 |
| - const { ConfigSet } = require('ts-jest/dist/legacy/config/config-set') |
79 |
| - const configSet = new ConfigSet(config.config) |
80 |
| - const tsConfig = configSet.typescript || configSet.parsedTsConfig |
81 | 85 | // Force es5 to prevent const vue_1 = require('vue') from conflicting
|
82 | 86 | return {
|
83 |
| - compilerOptions: { ...tsConfig.options, target: 'es5', module: 'commonjs' } |
| 87 | + compilerOptions: { ...compilerOptions, target: 'es5', module: 'commonjs' } |
84 | 88 | }
|
85 | 89 | }
|
86 | 90 |
|
87 | 91 | function isValidTransformer(transformer) {
|
88 | 92 | return (
|
| 93 | + isFunction(transformer.createTransformer) || |
89 | 94 | isFunction(transformer.process) ||
|
90 | 95 | isFunction(transformer.postprocess) ||
|
91 | 96 | isFunction(transformer.preprocess)
|
@@ -119,12 +124,13 @@ const getCustomTransformer = function getCustomTransformer(
|
119 | 124 |
|
120 | 125 | if (!isValidTransformer(transformer)) {
|
121 | 126 | throwError(
|
122 |
| - `transformer must contain at least one process, preprocess, or ` + |
123 |
| - `postprocess method` |
| 127 | + `transformer must contain at least one createTransformer(), process(), preprocess(), or postprocess() method` |
124 | 128 | )
|
125 | 129 | }
|
126 | 130 |
|
127 |
| - return transformer |
| 131 | + return isFunction(transformer.createTransformer) |
| 132 | + ? transformer.createTransformer() |
| 133 | + : transformer |
128 | 134 | }
|
129 | 135 |
|
130 | 136 | const throwError = function error(msg) {
|
@@ -161,7 +167,7 @@ module.exports = {
|
161 | 167 | throwError,
|
162 | 168 | logResultErrors,
|
163 | 169 | getCustomTransformer,
|
164 |
| - getTsJestConfig, |
| 170 | + getTypeScriptConfig, |
165 | 171 | getBabelOptions,
|
166 | 172 | getVueJestConfig,
|
167 | 173 | transformContent,
|
|
0 commit comments